Move custom libs to the source tree
This commit is contained in:
15
libs/SimpleValidator/Validators/Alpha.php
Normal file
15
libs/SimpleValidator/Validators/Alpha.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class Alpha extends Base
|
||||
{
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
return ctype_alpha($data[$this->field]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
15
libs/SimpleValidator/Validators/AlphaNumeric.php
Normal file
15
libs/SimpleValidator/Validators/AlphaNumeric.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class AlphaNumeric extends Base
|
||||
{
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
return ctype_alnum($data[$this->field]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
37
libs/SimpleValidator/Validators/Base.php
Normal file
37
libs/SimpleValidator/Validators/Base.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
abstract class Base
|
||||
{
|
||||
protected $field = '';
|
||||
protected $error_message = '';
|
||||
protected $data = array();
|
||||
|
||||
abstract public function execute(array $data);
|
||||
|
||||
public function __construct($field, $error_message)
|
||||
{
|
||||
$this->field = $field;
|
||||
$this->error_message = $error_message;
|
||||
}
|
||||
|
||||
public function getErrorMessage()
|
||||
{
|
||||
return $this->error_message;
|
||||
}
|
||||
|
||||
public function getField()
|
||||
{
|
||||
if (is_array($this->field)) {
|
||||
return $this->field[0];
|
||||
}
|
||||
|
||||
return $this->field;
|
||||
}
|
||||
|
||||
public function isFieldNotEmpty(array $data)
|
||||
{
|
||||
return isset($data[$this->field]) && $data[$this->field] !== '';
|
||||
}
|
||||
}
|
||||
45
libs/SimpleValidator/Validators/Date.php
Normal file
45
libs/SimpleValidator/Validators/Date.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class Date extends Base
|
||||
{
|
||||
private $formats = array();
|
||||
|
||||
public function __construct($field, $error_message, array $formats)
|
||||
{
|
||||
parent::__construct($field, $error_message);
|
||||
$this->formats = $formats;
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
foreach ($this->formats as $format) {
|
||||
if ($this->isValidDate($data[$this->field], $format)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isValidDate($value, $format)
|
||||
{
|
||||
$date = DateTime::createFromFormat($format, $value);
|
||||
|
||||
if ($date !== false) {
|
||||
$errors = DateTime::getLastErrors();
|
||||
if ($errors['error_count'] === 0 && $errors['warning_count'] === 0) {
|
||||
return $date->getTimestamp() > 0;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
67
libs/SimpleValidator/Validators/Email.php
Normal file
67
libs/SimpleValidator/Validators/Email.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class Email extends Base
|
||||
{
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
|
||||
// I use the same validation method as Firefox
|
||||
// http://hg.mozilla.org/mozilla-central/file/cf5da681d577/content/html/content/src/nsHTMLInputElement.cpp#l3967
|
||||
|
||||
$value = $data[$this->field];
|
||||
$length = strlen($value);
|
||||
|
||||
// If the email address begins with a '@' or ends with a '.',
|
||||
// we know it's invalid.
|
||||
if ($value[0] === '@' || $value[$length - 1] === '.') {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check the username
|
||||
for ($i = 0; $i < $length && $value[$i] !== '@'; ++$i) {
|
||||
|
||||
$c = $value[$i];
|
||||
|
||||
if (! (ctype_alnum($c) || $c === '.' || $c === '!' || $c === '#' || $c === '$' ||
|
||||
$c === '%' || $c === '&' || $c === '\'' || $c === '*' || $c === '+' ||
|
||||
$c === '-' || $c === '/' || $c === '=' || $c === '?' || $c === '^' ||
|
||||
$c === '_' || $c === '`' || $c === '{' || $c === '|' || $c === '}' ||
|
||||
$c === '~')) {
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// There is no domain name (or it's one-character long),
|
||||
// that's not a valid email address.
|
||||
if (++$i >= $length) return false;
|
||||
if (($i + 1) === $length) return false;
|
||||
|
||||
// The domain name can't begin with a dot.
|
||||
if ($value[$i] === '.') return false;
|
||||
|
||||
// Parsing the domain name.
|
||||
for (; $i < $length; ++$i) {
|
||||
|
||||
$c = $value[$i];
|
||||
|
||||
if ($c === '.') {
|
||||
|
||||
// A dot can't follow a dot.
|
||||
if ($value[$i - 1] === '.') return false;
|
||||
}
|
||||
elseif (! (ctype_alnum($c) || $c === '-')) {
|
||||
|
||||
// The domain characters have to be in this list to be valid.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
27
libs/SimpleValidator/Validators/Equals.php
Normal file
27
libs/SimpleValidator/Validators/Equals.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class Equals extends Base
|
||||
{
|
||||
private $field2;
|
||||
|
||||
public function __construct($field1, $field2, $error_message)
|
||||
{
|
||||
parent::__construct($field1, $error_message);
|
||||
$this->field2 = $field2;
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
if (! isset($data[$this->field2])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $data[$this->field] === $data[$this->field2];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
38
libs/SimpleValidator/Validators/Exists.php
Normal file
38
libs/SimpleValidator/Validators/Exists.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
use PDO;
|
||||
|
||||
class Exists extends Base
|
||||
{
|
||||
private $pdo;
|
||||
private $key;
|
||||
private $table;
|
||||
|
||||
public function __construct($field, $error_message, PDO $pdo, $table, $key = '')
|
||||
{
|
||||
parent::__construct($field, $error_message);
|
||||
|
||||
$this->pdo = $pdo;
|
||||
$this->table = $table;
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
if (! $this->isFieldNotEmpty($data)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->key === '') {
|
||||
$this->key = $this->field;
|
||||
}
|
||||
|
||||
$rq = $this->pdo->prepare('SELECT 1 FROM '.$this->table.' WHERE '.$this->key.'=?');
|
||||
$rq->execute(array($data[$this->field]));
|
||||
|
||||
return $rq->fetchColumn() == 1;
|
||||
}
|
||||
}
|
||||
23
libs/SimpleValidator/Validators/GreaterThan.php
Normal file
23
libs/SimpleValidator/Validators/GreaterThan.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class GreaterThan extends Base
|
||||
{
|
||||
private $min;
|
||||
|
||||
public function __construct($field, $error_message, $min)
|
||||
{
|
||||
parent::__construct($field, $error_message);
|
||||
$this->min = $min;
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
return $data[$this->field] > $this->min;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
23
libs/SimpleValidator/Validators/InArray.php
Normal file
23
libs/SimpleValidator/Validators/InArray.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class InArray extends Base
|
||||
{
|
||||
protected $array;
|
||||
|
||||
public function __construct($field, array $array, $error_message)
|
||||
{
|
||||
parent::__construct($field, $error_message);
|
||||
$this->array = $array;
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
return in_array($data[$this->field], $this->array);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
25
libs/SimpleValidator/Validators/Integer.php
Normal file
25
libs/SimpleValidator/Validators/Integer.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class Integer extends Base
|
||||
{
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
if (is_string($data[$this->field])) {
|
||||
|
||||
if ($data[$this->field][0] === '-') {
|
||||
return ctype_digit(substr($data[$this->field], 1));
|
||||
}
|
||||
|
||||
return ctype_digit($data[$this->field]);
|
||||
}
|
||||
else {
|
||||
return is_int($data[$this->field]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
15
libs/SimpleValidator/Validators/Ip.php
Normal file
15
libs/SimpleValidator/Validators/Ip.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class Ip extends Base
|
||||
{
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
return filter_var($data[$this->field], FILTER_VALIDATE_IP) !== false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
26
libs/SimpleValidator/Validators/Length.php
Normal file
26
libs/SimpleValidator/Validators/Length.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class Length extends Base
|
||||
{
|
||||
private $min;
|
||||
private $max;
|
||||
|
||||
public function __construct($field, $error_message, $min, $max)
|
||||
{
|
||||
parent::__construct($field, $error_message);
|
||||
$this->min = $min;
|
||||
$this->max = $max;
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
$length = mb_strlen($data[$this->field], 'UTF-8');
|
||||
return $length >= $this->min && $length <= $this->max;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
24
libs/SimpleValidator/Validators/MaxLength.php
Normal file
24
libs/SimpleValidator/Validators/MaxLength.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class MaxLength extends Base
|
||||
{
|
||||
private $max;
|
||||
|
||||
public function __construct($field, $error_message, $max)
|
||||
{
|
||||
parent::__construct($field, $error_message);
|
||||
$this->max = $max;
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
$length = mb_strlen($data[$this->field], 'UTF-8');
|
||||
return $length <= $this->max;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
24
libs/SimpleValidator/Validators/MinLength.php
Normal file
24
libs/SimpleValidator/Validators/MinLength.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class MinLength extends Base
|
||||
{
|
||||
private $min;
|
||||
|
||||
public function __construct($field, $error_message, $min)
|
||||
{
|
||||
parent::__construct($field, $error_message);
|
||||
$this->min = $min;
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
$length = mb_strlen($data[$this->field], 'UTF-8');
|
||||
return $length >= $this->min;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
15
libs/SimpleValidator/Validators/NotEmpty.php
Normal file
15
libs/SimpleValidator/Validators/NotEmpty.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class NotEmpty extends Base
|
||||
{
|
||||
public function execute(array $data)
|
||||
{
|
||||
if (array_key_exists($this->field, $data)) {
|
||||
return $data[$this->field] !== null && $data[$this->field] !== '';
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
28
libs/SimpleValidator/Validators/NotEquals.php
Normal file
28
libs/SimpleValidator/Validators/NotEquals.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class NotEquals extends Base
|
||||
{
|
||||
private $field2;
|
||||
|
||||
public function __construct($field1, $field2, $error_message)
|
||||
{
|
||||
parent::__construct($field1, $error_message);
|
||||
$this->field2 = $field2;
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
|
||||
if (! isset($data[$this->field2])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $data[$this->field] !== $data[$this->field2];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
15
libs/SimpleValidator/Validators/NotInArray.php
Normal file
15
libs/SimpleValidator/Validators/NotInArray.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class NotInArray extends InArray
|
||||
{
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
return ! in_array($data[$this->field], $this->array);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
15
libs/SimpleValidator/Validators/Numeric.php
Normal file
15
libs/SimpleValidator/Validators/Numeric.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class Numeric extends Base
|
||||
{
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
return is_numeric($data[$this->field]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
33
libs/SimpleValidator/Validators/Range.php
Normal file
33
libs/SimpleValidator/Validators/Range.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class Range extends Base
|
||||
{
|
||||
private $min;
|
||||
private $max;
|
||||
|
||||
public function __construct($field, $error_message, $min, $max)
|
||||
{
|
||||
parent::__construct($field, $error_message);
|
||||
|
||||
$this->min = $min;
|
||||
$this->max = $max;
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
|
||||
if (! is_numeric($data[$this->field])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($data[$this->field] < $this->min || $data[$this->field] > $this->max) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
11
libs/SimpleValidator/Validators/Required.php
Normal file
11
libs/SimpleValidator/Validators/Required.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
class Required extends Base
|
||||
{
|
||||
public function execute(array $data)
|
||||
{
|
||||
return $this->isFieldNotEmpty($data);
|
||||
}
|
||||
}
|
||||
48
libs/SimpleValidator/Validators/Unique.php
Normal file
48
libs/SimpleValidator/Validators/Unique.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace SimpleValidator\Validators;
|
||||
|
||||
use PDO;
|
||||
|
||||
class Unique extends Base
|
||||
{
|
||||
private $pdo;
|
||||
private $primary_key;
|
||||
private $table;
|
||||
|
||||
public function __construct($field, $error_message, PDO $pdo, $table, $primary_key = 'id')
|
||||
{
|
||||
parent::__construct($field, $error_message);
|
||||
|
||||
$this->pdo = $pdo;
|
||||
$this->primary_key = $primary_key;
|
||||
$this->table = $table;
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
if ($this->isFieldNotEmpty($data)) {
|
||||
if (! isset($data[$this->primary_key])) {
|
||||
$rq = $this->pdo->prepare('SELECT 1 FROM '.$this->table.' WHERE '.$this->field.'=?');
|
||||
$rq->execute(array($data[$this->field]));
|
||||
}
|
||||
else {
|
||||
|
||||
$rq = $this->pdo->prepare(
|
||||
'SELECT 1 FROM '.$this->table.'
|
||||
WHERE '.$this->field.'=? AND '.$this->primary_key.' != ?'
|
||||
);
|
||||
|
||||
$rq->execute(array($data[$this->field], $data[$this->primary_key]));
|
||||
}
|
||||
|
||||
$result = $rq->fetchColumn();
|
||||
|
||||
if ($result == 1) { // Postgresql returns an integer but other database returns a string '1'
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user