Add namespace.
This commit is contained in:
20
Validator/EmailValidator.php
Normal file
20
Validator/EmailValidator.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php namespace Majestic\Validator;
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-26
|
||||
*/
|
||||
|
||||
class EmailValidator extends RegexValidator
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
* https://tools.ietf.org/html/rfc5322#section-3.4.1
|
||||
* https://tools.ietf.org/html/rfc5321#section-4.5.3
|
||||
*/
|
||||
protected $regex = '/^([a-z0-9._-]{1,64})\@([a-z0-9-]{2,250}\.)+\w{2,5}$/i';
|
||||
|
||||
public function __construct(){}
|
||||
}
|
37
Validator/EqualValidator.php
Normal file
37
Validator/EqualValidator.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php namespace Majestic\Validator;
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-26
|
||||
*/
|
||||
|
||||
class EqualValidator extends \Majestic\Validator
|
||||
{
|
||||
|
||||
const NOT_EQUAL = 'not_match';
|
||||
|
||||
protected $templates = array(self::NOT_EQUAL => 'Tokens do not match');
|
||||
|
||||
protected $token;
|
||||
|
||||
public function __construct($token)
|
||||
{
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
public function isValid($value, $context = null)
|
||||
{
|
||||
$this->setValue($value);
|
||||
if ($this->token === null) {
|
||||
throw new InitializationException('Token not defined.');
|
||||
}
|
||||
|
||||
if ($value !== $this->token) {
|
||||
$this->error();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
39
Validator/MbStrlenValidator.php
Normal file
39
Validator/MbStrlenValidator.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php namespace Majestic\Validator;
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage forms
|
||||
* @since 17.05.12
|
||||
* @author Aleksander Demidov
|
||||
*
|
||||
*/
|
||||
|
||||
class MbStrlenValidator extends \Majestic\Validator
|
||||
{
|
||||
|
||||
const GREATHER_THAN = 'not_match';
|
||||
|
||||
protected $templates = array(self::GREATHER_THAN => 'String length greather than.');
|
||||
|
||||
protected $token;
|
||||
|
||||
public function __construct($token)
|
||||
{
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
public function isValid($value, $context = null)
|
||||
{
|
||||
$this->setValue($value);
|
||||
if ($this->token === null) {
|
||||
throw new InitializationException('Token not defined.');
|
||||
}
|
||||
|
||||
if (mb_strlen($value) > $this->token) {
|
||||
$this->error();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
30
Validator/NotEmptyValidator.php
Normal file
30
Validator/NotEmptyValidator.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php namespace Majestic\Validator;
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-26
|
||||
*/
|
||||
|
||||
class NotEmptyValidator extends \Majestic\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;
|
||||
}
|
||||
}
|
39
Validator/RegexValidator.php
Normal file
39
Validator/RegexValidator.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php namespace Majestic\Validator;
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-26
|
||||
*/
|
||||
|
||||
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 \Majestic\Exception\GeneralException('Internal error matching regex "' . $this->regex . ' against value "' . $value . '"');
|
||||
}
|
||||
if (!$status) {
|
||||
$this->error();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
39
Validator/StrlenValidator.php
Normal file
39
Validator/StrlenValidator.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php namespace Majestic\Validator;
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage forms
|
||||
* @since 17.05.12
|
||||
* @author Aleksander Demidov
|
||||
*
|
||||
*/
|
||||
|
||||
class StrlenValidator extends \Majestic\Validator
|
||||
{
|
||||
|
||||
const GREATHER_THAN = 'not_match';
|
||||
|
||||
protected $templates = array(self::GREATHER_THAN => 'String length greather than.');
|
||||
|
||||
protected $token;
|
||||
|
||||
public function __construct($token)
|
||||
{
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
public function isValid($value, $context = null)
|
||||
{
|
||||
$this->setValue($value);
|
||||
if ($this->token === null) {
|
||||
throw new InitializationException('Token not defined.');
|
||||
}
|
||||
|
||||
if (strlen($value) > $this->token) {
|
||||
$this->error();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
66
Validator/Validator.php
Normal file
66
Validator/Validator.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php namespace Majestic\Validator;
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-24
|
||||
*/
|
||||
|
||||
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($message, $key = null)
|
||||
{
|
||||
if ($key === null) {
|
||||
$key = current(array_keys($this->templates));
|
||||
}
|
||||
$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 GeneralException('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;
|
||||
}
|
||||
}
|
14
Validator/iValidator.php
Normal file
14
Validator/iValidator.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php namespace Majestic\Validator;
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage validator
|
||||
* @since 2010-04-25
|
||||
*/
|
||||
|
||||
interface iValidator
|
||||
{
|
||||
public function isValid($value, $context = null);
|
||||
public function getMessage();
|
||||
}
|
Reference in New Issue
Block a user