Engine.php
3 years ago
ValidModel.php
3 years ago
ValidationException.php
3 years ago
index.php
3 years ago
Engine.php
99 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoetVendor\Sudzy; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | /** |
| 9 | * Singleton valdation engine |
| 10 | **/ |
| 11 | class Engine |
| 12 | { |
| 13 | /** |
| 14 | * Validation methods are stored here so they can easily be overwritten |
| 15 | */ |
| 16 | protected $_checks; |
| 17 | |
| 18 | public function __construct() { |
| 19 | $this->_checks = [ |
| 20 | 'required' => [$this, '_required'], |
| 21 | 'minLength' => [$this, '_minLength'], |
| 22 | 'maxLength' => [$this, '_maxLength'], |
| 23 | 'isEmail' => [$this, '_isEmail'], |
| 24 | 'isInteger' => [$this, '_isInteger'], |
| 25 | 'isNumeric' => [$this, '_isNumeric'], |
| 26 | ]; |
| 27 | } |
| 28 | |
| 29 | public function __call($name, $args) { |
| 30 | if (!isset($this->_checks[$name])) |
| 31 | throw new \InvalidArgumentException("{$name} is not a valid validation function."); |
| 32 | |
| 33 | $val = array_shift($args); |
| 34 | $args = array_shift($args); |
| 35 | |
| 36 | return call_user_func($this->_checks[$name], $val, $args); |
| 37 | } |
| 38 | |
| 39 | public function executeOne($check, $val, $params=[]) { |
| 40 | $callback = [$this, $check]; |
| 41 | if (is_callable($callback)) { |
| 42 | return call_user_func($callback, $val, $params); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param string $label label used to call function |
| 48 | * @param Callable $function function with params (value, additional params as array) |
| 49 | * @throws \Exception |
| 50 | */ |
| 51 | public function addValidator($label, $function) { |
| 52 | if (isset($this->_checks[$label])) throw new \Exception(); |
| 53 | $this->setValidator($label, $function); |
| 54 | } |
| 55 | |
| 56 | public function setValidator($label, $function) { |
| 57 | $this->_checks[$label] = $function; |
| 58 | } |
| 59 | |
| 60 | public function removeValidator($label) { |
| 61 | unset($this->_checks[$label]); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @return array<int, int|string> The list of usable validator methods |
| 66 | */ |
| 67 | public function getValidators() { |
| 68 | return array_keys($this->_checks); |
| 69 | } |
| 70 | |
| 71 | ///// Validator methods |
| 72 | protected function _isEmail($val, $params) { |
| 73 | return false !== filter_var($val, FILTER_VALIDATE_EMAIL); |
| 74 | } |
| 75 | |
| 76 | protected function _isInteger($val, $params) { |
| 77 | if (!is_numeric($val)) return false; |
| 78 | return intval($val) == $val; |
| 79 | } |
| 80 | |
| 81 | protected function _isNumeric($val, $params) { |
| 82 | return is_numeric($val); |
| 83 | } |
| 84 | |
| 85 | protected function _minLength($val, $params) { |
| 86 | $len = array_shift($params); |
| 87 | return strlen($val) >= $len; |
| 88 | } |
| 89 | |
| 90 | protected function _maxLength($val, $params) { |
| 91 | $len = array_shift($params); |
| 92 | return strlen($val) <= $len; |
| 93 | } |
| 94 | |
| 95 | protected function _required($val, $params=[]) { |
| 96 | return !(($val === null) || ('' === trim($val))); |
| 97 | } |
| 98 | } |
| 99 |