diff options
author | Frédéric Guillot <contact@fredericguillot.com> | 2014-01-25 14:56:02 -0500 |
---|---|---|
committer | Frédéric Guillot <contact@fredericguillot.com> | 2014-01-25 14:56:02 -0500 |
commit | 9383a15af699ede77142d040b65118e15754a2ca (patch) | |
tree | b550b5adf5bcf8f5a8793c188cc5630f26a27d49 /vendor/SimpleValidator |
First commit
Diffstat (limited to 'vendor/SimpleValidator')
-rw-r--r-- | vendor/SimpleValidator/Base.php | 44 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validator.php | 67 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/Alpha.php | 33 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/AlphaNumeric.php | 33 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/Email.php | 81 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/Equals.php | 43 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/Integer.php | 42 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/Ip.php | 33 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/Length.php | 48 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/MacAddress.php | 37 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/MaxLength.php | 46 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/MinLength.php | 46 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/Numeric.php | 33 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/Range.php | 51 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/Required.php | 30 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/Unique.php | 78 | ||||
-rw-r--r-- | vendor/SimpleValidator/Validators/Version.php | 32 |
17 files changed, 777 insertions, 0 deletions
diff --git a/vendor/SimpleValidator/Base.php b/vendor/SimpleValidator/Base.php new file mode 100644 index 00000000..45c01a6e --- /dev/null +++ b/vendor/SimpleValidator/Base.php @@ -0,0 +1,44 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +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() + { + return $this->field; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validator.php b/vendor/SimpleValidator/Validator.php new file mode 100644 index 00000000..8bb4d620 --- /dev/null +++ b/vendor/SimpleValidator/Validator.php @@ -0,0 +1,67 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +class Validator +{ + private $data = array(); + private $errors = array(); + private $validators = array(); + + + public function __construct(array $data, array $validators) + { + $this->data = $data; + $this->validators = $validators; + } + + + public function execute() + { + $result = true; + + foreach ($this->validators as $validator) { + + if (! $validator->execute($this->data)) { + + $this->addError( + $validator->getField(), + $validator->getErrorMessage() + ); + + $result = false; + } + } + + return $result; + } + + + public function addError($field, $message) + { + if (! isset($this->errors[$field])) { + + $this->errors[$field] = array(); + } + + $this->errors[$field][] = $message; + } + + + public function getErrors() + { + return $this->errors; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/Alpha.php b/vendor/SimpleValidator/Validators/Alpha.php new file mode 100644 index 00000000..b00b819b --- /dev/null +++ b/vendor/SimpleValidator/Validators/Alpha.php @@ -0,0 +1,33 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +class Alpha extends Base +{ + public function execute(array $data) + { + if (isset($data[$this->field]) && $data[$this->field] !== '') { + + if (! ctype_alpha($data[$this->field])) { + + return false; + } + } + + return true; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/AlphaNumeric.php b/vendor/SimpleValidator/Validators/AlphaNumeric.php new file mode 100644 index 00000000..e1762d67 --- /dev/null +++ b/vendor/SimpleValidator/Validators/AlphaNumeric.php @@ -0,0 +1,33 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +class AlphaNumeric extends Base +{ + public function execute(array $data) + { + if (isset($data[$this->field]) && $data[$this->field] !== '') { + + if (! ctype_alnum($data[$this->field])) { + + return false; + } + } + + return true; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/Email.php b/vendor/SimpleValidator/Validators/Email.php new file mode 100644 index 00000000..e4e3d5d6 --- /dev/null +++ b/vendor/SimpleValidator/Validators/Email.php @@ -0,0 +1,81 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +class Email extends Base +{ + public function execute(array $data) + { + if (isset($data[$this->field]) && $data[$this->field] !== '') { + + // 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; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/Equals.php b/vendor/SimpleValidator/Validators/Equals.php new file mode 100644 index 00000000..91f34e4b --- /dev/null +++ b/vendor/SimpleValidator/Validators/Equals.php @@ -0,0 +1,43 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +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 (isset($data[$this->field]) && $data[$this->field] !== '') { + + if (! isset($data[$this->field2])) return false; + + return $data[$this->field] === $data[$this->field2]; + } + + return true; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/Integer.php b/vendor/SimpleValidator/Validators/Integer.php new file mode 100644 index 00000000..150558a3 --- /dev/null +++ b/vendor/SimpleValidator/Validators/Integer.php @@ -0,0 +1,42 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +class Integer extends Base +{ + public function execute(array $data) + { + if (isset($data[$this->field]) && $data[$this->field] !== '') { + + 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; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/Ip.php b/vendor/SimpleValidator/Validators/Ip.php new file mode 100644 index 00000000..48afe569 --- /dev/null +++ b/vendor/SimpleValidator/Validators/Ip.php @@ -0,0 +1,33 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +class Ip extends Base +{ + public function execute(array $data) + { + if (isset($data[$this->field]) && $data[$this->field] !== '') { + + if (! filter_var($data[$this->field], FILTER_VALIDATE_IP)) { + + return false; + } + } + + return true; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/Length.php b/vendor/SimpleValidator/Validators/Length.php new file mode 100644 index 00000000..36e50b37 --- /dev/null +++ b/vendor/SimpleValidator/Validators/Length.php @@ -0,0 +1,48 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +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 (isset($data[$this->field]) && $data[$this->field] !== '') { + + $length = mb_strlen($data[$this->field], 'UTF-8'); + + if ($length < $this->min || $length > $this->max) { + + return false; + } + } + + return true; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/MacAddress.php b/vendor/SimpleValidator/Validators/MacAddress.php new file mode 100644 index 00000000..d9348417 --- /dev/null +++ b/vendor/SimpleValidator/Validators/MacAddress.php @@ -0,0 +1,37 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +class MacAddress extends Base +{ + public function execute(array $data) + { + if (isset($data[$this->field]) && $data[$this->field] !== '') { + + $groups = explode(':', $data[$this->field]); + + if (count($groups) !== 6) return false; + + foreach ($groups as $group) { + + if (! ctype_xdigit($group)) return false; + } + } + + return true; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/MaxLength.php b/vendor/SimpleValidator/Validators/MaxLength.php new file mode 100644 index 00000000..d8e032b0 --- /dev/null +++ b/vendor/SimpleValidator/Validators/MaxLength.php @@ -0,0 +1,46 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +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 (isset($data[$this->field]) && $data[$this->field] !== '') { + + $length = mb_strlen($data[$this->field], 'UTF-8'); + + if ($length > $this->max) { + + return false; + } + } + + return true; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/MinLength.php b/vendor/SimpleValidator/Validators/MinLength.php new file mode 100644 index 00000000..4b7f7d24 --- /dev/null +++ b/vendor/SimpleValidator/Validators/MinLength.php @@ -0,0 +1,46 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +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 (isset($data[$this->field]) && $data[$this->field] !== '') { + + $length = mb_strlen($data[$this->field], 'UTF-8'); + + if ($length < $this->min) { + + return false; + } + } + + return true; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/Numeric.php b/vendor/SimpleValidator/Validators/Numeric.php new file mode 100644 index 00000000..a958df1a --- /dev/null +++ b/vendor/SimpleValidator/Validators/Numeric.php @@ -0,0 +1,33 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +class Numeric extends Base +{ + public function execute(array $data) + { + if (isset($data[$this->field]) && $data[$this->field] !== '') { + + if (! is_numeric($data[$this->field])) { + + return false; + } + } + + return true; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/Range.php b/vendor/SimpleValidator/Validators/Range.php new file mode 100644 index 00000000..1d71b926 --- /dev/null +++ b/vendor/SimpleValidator/Validators/Range.php @@ -0,0 +1,51 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +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 (isset($data[$this->field]) && $data[$this->field] !== '') { + + if (! is_numeric($data[$this->field])) { + + return false; + } + + if ($data[$this->field] < $this->min || $data[$this->field] > $this->max) { + + return false; + } + } + + return true; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/Required.php b/vendor/SimpleValidator/Validators/Required.php new file mode 100644 index 00000000..e7ef2714 --- /dev/null +++ b/vendor/SimpleValidator/Validators/Required.php @@ -0,0 +1,30 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +class Required extends Base +{ + public function execute(array $data) + { + if (! isset($data[$this->field]) || $data[$this->field] === '') { + + return false; + } + + return true; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/Unique.php b/vendor/SimpleValidator/Validators/Unique.php new file mode 100644 index 00000000..c20dbe11 --- /dev/null +++ b/vendor/SimpleValidator/Validators/Unique.php @@ -0,0 +1,78 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + */ +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 (isset($data[$this->field]) && $data[$this->field] !== '') { + + if (! isset($data[$this->primary_key])) { + + $rq = $this->pdo->prepare('SELECT COUNT(*) FROM '.$this->table.' WHERE '.$this->field.'=?'); + + $rq->execute(array( + $data[$this->field] + )); + + $result = $rq->fetch(\PDO::FETCH_NUM); + + if (isset($result[0]) && $result[0] === '1') { + + return false; + } + } + else { + + $rq = $this->pdo->prepare( + 'SELECT COUNT(*) FROM '.$this->table.' + WHERE '.$this->field.'=? AND '.$this->primary_key.' != ?' + ); + + $rq->execute(array( + $data[$this->field], + $data[$this->primary_key] + )); + + $result = $rq->fetch(\PDO::FETCH_NUM); + + if (isset($result[0]) && $result[0] === '1') { + + return false; + } + } + } + + return true; + } +}
\ No newline at end of file diff --git a/vendor/SimpleValidator/Validators/Version.php b/vendor/SimpleValidator/Validators/Version.php new file mode 100644 index 00000000..273a28a5 --- /dev/null +++ b/vendor/SimpleValidator/Validators/Version.php @@ -0,0 +1,32 @@ +<?php + +/* + * This file is part of Simple Validator. + * + * (c) Frédéric Guillot <contact@fredericguillot.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace SimpleValidator\Validators; + +use SimpleValidator\Base; + +/** + * @author Frédéric Guillot <contact@fredericguillot.com> + * @link http://semver.org/ + */ +class Version extends Base +{ + public function execute(array $data) + { + if (isset($data[$this->field]) && $data[$this->field] !== '') { + + $pattern = '/^[0-9]+\.[0-9]+\.[0-9]+([+-][^+-][0-9A-Za-z-.]*)?$/'; + return (bool) preg_match($pattern, $data[$this->field]); + } + + return true; + } +}
\ No newline at end of file |