class Validate{
protected $rules;
private $errors = array();
private $data = array();
protected $text_error = array(
'notempty' => 'Поле не может быть пустым',
'length' => 'Поле содержит неверное кол-во символов',
'email' => 'Поле заполнено не корректно',
'regexp' => 'Поле заполнено не корректно',
'code' => 'Введён не правильный защитный код',
'confirm'=> 'Введённые пароли не совпадают',
'id'=>'Поле заполнено не верно, ожидаются только цифры'
);
public function __construct(){
}
protected function addRules($rules){
$this->rules[] = $rules;
return $this;
}
protected function _empty($nameTag){
if(empty($_REQUEST[$nameTag]))
$this->errors[$nameTag]['notempty'] = $this->text_error['notempty'];
else
$this->insertData($nameTag);
}
protected function _id($nameTag){
if(!is_numeric($_REQUEST[$nameTag])){
$this->errors[$nameTag]['id'] = $this->text_error['id'];
}
else
$this->insertData($nameTag);
}
protected function _float($nameTag){
$_REQUEST[$nameTag] = (float)$_REQUEST[$nameTag];
$this->insertData($nameTag);
}
protected function _sha1($nameTag){
$_REQUEST[$nameTag] = sha1($_REQUEST[$nameTag]);
$this->insertData($nameTag);
}
protected function _string($nameTag){
$_REQUEST[$nameTag] = (string)$_REQUEST[$nameTag];
$this->insertData($nameTag);
}
protected function _length($nameTag, $tv){
$limit = rtrim($tv, ']');
if(empty($limit[0])) return;
$t = explode(',',$limit[0]);
$min = (int)$t[0];
$max = isset($t[1]) ? (int)$t[1] : 2000;
$length = mb_strlen(trim($_REQUEST[$nameTag]), 'utf-8');
if($length<$min || $length>$max)
$this->errors[$nameTag]['length'] = $this->text_error['length'];
else
$this->insertData($nameTag);
}
protected function _trim($nameTag){
$_REQUEST[$nameTag] = trim($_REQUEST[$nameTag]);
$this->insertData($nameTag);
}
protected function _html($nameTag){
$_REQUEST[$nameTag] = strip_tags($_REQUEST[$nameTag]);
$this->insertData($nameTag);
}
protected function _email($nameTag){
if(!filter_var($_REQUEST[$nameTag], FILTER_VALIDATE_EMAIL))
$this->errors[$nameTag]['email'] = $this->text_error['email'];
else
$this->insertData($nameTag);
}
protected function _url($nameTag){
if(!filter_var($_REQUEST[$nameTag], FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED))
$this->errors[$nameTag]['url'] = $this->text_error['url'];
else
$this->insertData($nameTag);
}
protected function _confirm($nameTag, $tv){
$confirm = rtrim($tv, ']');
if(!isset($this->errors[$tv])){
if($_REQUEST[$nameTag] != $_REQUEST[$confirm]){
unset($this->errors[$nameTag]['errors']);
$this->errors[$nameTag]['confirm'] = $this->text_error['confirm'];
}
}
}
protected function _regexp($nameTag, $tv){
if(!preg_match($tv, $_REQUEST[$nameTag])){
$this->errors[$nameTag]['regexp'] = $this->text_error['regexp'];
}
else
$this->insertData($nameTag);
}
protected function _code($nameTag, $tv){
if($_SESSION[$tv] != $_REQUEST[$nameTag])
$this->errors[$nameTag]['code'] = $this->text_error['code'];
else
$this->insertData($nameTag);
}
private function parseRules($target){
$this->target = $target;
foreach($target as $nameTag=>$l){
$tempRules = explode('|',$l);
foreach($tempRules as $k=>$v){
if(preg_match("#code#", $v)){
$tv = rtrim(strtr($v, array('code['=>'')),']');
$v = 'code';
}
elseif(preg_match("#regexp#", $v)){
$tv = rtrim(strtr($v, array('regexp['=>'')),']');
$v = 'regexp';
}
else{
$v = explode("[", $v);
$tv = isset($v[1]) ? $v[1] : null;
$v = $v[0];
}
switch($v){
case 'notempty' :
$this->_empty($nameTag);
break;
case 'length':
if($tv != null)
$this->_length($nameTag, $tv);
break;
case 'trim' :
$this->_trim($nameTag);
break;
case 'html' :
$this->_html($nameTag);
break;
case 'email' :
$this->_email($nameTag);
break;
case 'confirm' :
$this->_confirm($nameTag,$tv);
break;
case 'regexp' :
$this->_regexp($nameTag,$tv);
break;
case 'code' :
$this->_code($nameTag,$tv);
break;
case 'id' :
$this->_id($nameTag);
break;
case 'float' :
$this->_int($nameTag);
break;
case 'string' :
$this->_int($nameTag);
break;
case 'sha1' :
$this->_int($nameTag);
break;
}
}
}
}
private function insertData($nameTag){
$this->data[$nameTag] = $_REQUEST[$nameTag];
}
public function getData(){
return $this->data;
}
public function getAllErrors(){
return $this->errors;
}
public function getErrors(){
$firstErrors = array();
foreach($this->errors as $k=>$err){
$firstErrors[$k] = array_shift($err);
}
return $firstErrors;
}
protected function validate(){
if(is_array($this->rules)){
foreach($this->rules as $k=>$v){
$this->parseRules($v);
}
if(count($this->errors)>0){
$success = false;
}
else{
$success = true;
}
return $success;
}
}
}
class HTMLForm extends Validate{
public $action;
public $method;
public $nameForm;
public $paramsForm;
const EndTagInput = ' rel="i"/>';
const EndTagSelectInside = ' rel="s"';
const EndTagSelect = '</select>';
const EndTagOption = ' rel="o">';
const EndTagTextarea = ' rel="t">';
protected $elems = array(
'input'=>'<input rel="i"/>',
'select'=>'<select rel="s"></select>',
'option'=>'<option rel="o"></option>',
'textarea'=>'<textarea rel="t"></textarea>'
);
public function __construct(){
return parent::__construct();
}
public function setRules($rules){
parent::addRules(array($this->name=>$rules));
return $this;
}
public function createProperty($name, $value){
$this->$name = $value;
}
private function checkProperty($name){
if(!property_exists(__CLASS__, $name)){
$this->createProperty($name,$name);
$this->name = $name;
}
else
$this->name = $name;
}
public function open($nameForm, $action, $method, $arg = null){
$this->action = $action;
$this->method = $method;
$this->arg = $arg;
if(is_array($this->arg)){
$this->paramsForm = null;
foreach($this->arg as $attr=>$val){
if($val == null)
$this->paramsForm .= $attr;
else
$this->paramsForm .= ' '.$attr.'="'.$val.'" ';
}
}
$this->form[$this->nameForm] = array();
$this->nameForm = $nameForm;
$this->form[$this->nameForm]['begin'] = '<form name="'.$this->nameForm.'" method="'
.$this->method.'" action="'.$this->action.'" '.$this->paramsForm.'>';
}
public function close(){
$this->form[$this->nameForm]['end'] = '</form>';
return $this->form[$this->nameForm];
}
private function collectElem($name, $elem, $endTag){
$this->elem = $elem;
$this->checkProperty($name);
$this->parentMethodName = __CLASS__.'::set'.ucfirst($this->elem);
$this->parentMethod = strtr($this->elems[$this->elem], array($endTag=>' name="'.$this->$name.'"'.$endTag));
}
public function setTextarea($name){
$this->collectElem($name, 'textarea', self::EndTagTextarea);
return $this;
}
public function setInput($name){
$this->collectElem($name, 'input', self::EndTagInput);
return $this;
}
public function setSelect($name){
$this->collectElem($name, 'select', self::EndTagSelectInside);
return $this;
}
public function setOption($name){
$this->checkProperty($name);
$this->parentMethodName = __METHOD__;
$this->parentMethod = strtr($this->elems['option'], array(self::EndTagOption =>' value="'.$this->$name.'"'.self::EndTagOption));
return $this;
}
public function setAttr($attr){
$this->attr = explode('|', $attr);
if(is_array($this->attr)){
$this->parentMethod = strtr($this->parentMethod,
array($this->getEndTag() => ' '.$this->attr[0].'="'.$this->attr[1].'"'.$this->getEndTag()));
}
return $this;
}
public function setText($text){
if(!property_exists(__CLASS__, $text)){
$this->createProperty($text,$text);
$this->text = $text;
}
else
$this->text = $text;
if(is_array($this->attr)){
$this->parentMethod = strtr($this->parentMethod,
array($this->getEndTag() => $this->getEndTag().$this->$text));
}
return $this;
}
private function saveInArray($sub = false,$innerNameSub = null){
if($sub)
$this->form[$this->nameForm][$this->name][] = $this->parentMethod;
else if(!$sub)
$this->form[$this->nameForm][$this->name] = $this->parentMethod;
else if($innerNameSub != null){
$this->form[$this->nameForm][$innerNameSub.'_options'][$this->name] = $this->parentMethod;
return $this->form[$this->nameForm][$innerNameSub.'_options'];
}
}
public function addInput(){
if(preg_match('#type="radio"|type="checkbox"#', $this->parentMethod))
$this->saveInArray(true);
else
$this->saveInArray();
$this->parentMethod = '<input rel="i"/>';
}
public function addSelect(){
$this->saveInArray();
$this->parentMethod = '<select rel="s"></select>';
}
public function addTextarea(){
$this->saveInArray();
$this->parentMethod = '<textarea rel="t"></textarea>';
}
public function addOption($select){
$this->form[$this->nameForm][$select.'_options'][$this->name] = $this->parentMethod;
$temp = $this->form[$this->nameForm][$select.'_options'];
$this->form[$this->nameForm][$select] = strtr($this->form[$this->nameForm][$select],
array(self::EndTagSelect =>
$temp[$this->name].PHP_EOL.self::EndTagSelect)
);
$this->parentMethod = '<option rel="o"></option>';
return $this;
}
private function getEndTag(){
switch($this->parentMethodName){
case __CLASS__.'::setTextarea' : $this->endTag = self::EndTagTextarea; break;
case __CLASS__.'::setInput' : $this->endTag = self::EndTagInput; break;
case __CLASS__.'::setSelect' : $this->endTag = self::EndTagSelectInside; break;
case __CLASS__.'::setOption' : $this->endTag = self::EndTagOption; break;
}
return $this->endTag;
}
public function sendForm($submit){
foreach($this->form[$this->nameForm] as $k=>$elem){
if(strpos($elem, 'type="submit"') !== false){
$t = preg_match('#name="(.*)"#uUs', $elem, $s);
if(isset($_REQUEST[$submit])){
return $this->validate();
}
}
}
}
}