Validator, captcha, form, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@141 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
17
validator/EmailValidator.php
Normal file
17
validator/EmailValidator.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-26
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class EmailValidator extends RegexValidator
|
||||
{
|
||||
protected $regex = '/^([a-z0-9._-]{2,23})\@([a-z0-9-]{2,22}\.)+\w{2,5}$/i';
|
||||
|
||||
public function __construct(){}
|
||||
}
|
32
validator/NotEmptyValidator.php
Normal file
32
validator/NotEmptyValidator.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-26
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class NotEmptyValidator extends Validator
|
||||
{
|
||||
|
||||
const IS_EMPTY = 'is_empty';
|
||||
|
||||
protected $templates = array(self::IS_EMPTY => 'Value is required and can\'t be empty');
|
||||
|
||||
public function isValid($value, $context = null)
|
||||
{
|
||||
$this->setValue($value);
|
||||
|
||||
if (is_string($value) && $value === '') {
|
||||
$this->error();
|
||||
return false;
|
||||
} elseif (!is_string($value) && empty($value)) {
|
||||
$this->error();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
41
validator/RegexValidator.php
Normal file
41
validator/RegexValidator.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-26
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class RegexValidator extends Validator
|
||||
{
|
||||
|
||||
const NOT_MATCH = 'regex_not_match';
|
||||
|
||||
protected $vars = array('regex');
|
||||
protected $templates = array(self::NOT_MATCH => '"%value%" does not match against regex "%regex%"');
|
||||
|
||||
protected $regex;
|
||||
|
||||
public function __construct($regex)
|
||||
{
|
||||
$this->regex = $regex;
|
||||
}
|
||||
|
||||
public function isValid($value, $context = null)
|
||||
{
|
||||
$this->setValue($value);
|
||||
|
||||
$status = preg_match($this->regex, $value);
|
||||
if ($status === false) {
|
||||
throw new Exception('Internal error matching regex "' . $this->regex . ' against value "' . $value . '"');
|
||||
}
|
||||
if (!$status) {
|
||||
$this->error();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
65
validator/Validator.php
Normal file
65
validator/Validator.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-24
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Validator implements iValidator
|
||||
{
|
||||
|
||||
protected $value;
|
||||
protected $message;
|
||||
protected $vars = array();
|
||||
protected $templates = array();
|
||||
|
||||
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
protected function setValue($value)
|
||||
{
|
||||
$this->value = (string) $value;
|
||||
$this->message = null;
|
||||
}
|
||||
|
||||
public function setMessage($key, $message)
|
||||
{
|
||||
$this->templates[$key] = $message;
|
||||
}
|
||||
|
||||
protected function error($template = null, $value = null)
|
||||
{
|
||||
if ($template === null) {
|
||||
$template = current(array_keys($this->templates));
|
||||
}
|
||||
if ($value === null) {
|
||||
$value = $this->value;
|
||||
}
|
||||
$this->message = $this->createMessage($template, $value);
|
||||
}
|
||||
|
||||
protected function createMessage($template, $value)
|
||||
{
|
||||
if (!isset($this->templates[$template])) {
|
||||
throw new Exception('Message template "' . $template . '" unknown.');
|
||||
}
|
||||
|
||||
$message = $this->templates[$template];
|
||||
if (strpos($message, '%') !== false) {
|
||||
$message = str_replace('%value%', (string) $value, $message);
|
||||
foreach ($this->vars as $property) {
|
||||
if (property_exists($this, $property)) {
|
||||
$message = str_replace("%$property%", (string) $this->$property, $message);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $message;
|
||||
}
|
||||
}
|
16
validator/iValidator.php
Normal file
16
validator/iValidator.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
interface iValidator
|
||||
{
|
||||
public function isValid($value, $context = null);
|
||||
public function getMessage();
|
||||
}
|
Reference in New Issue
Block a user