From 6743106d9a8c760580690aab704f908766731801 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 10 Jul 2019 13:34:15 +0200 Subject: Replaced validation with `respect/validation` --- src/Http/Validation/Rules/In.php | 21 +++ src/Http/Validation/Rules/NotIn.php | 15 +++ src/Http/Validation/Validates.php | 154 ---------------------- src/Http/Validation/ValidationServiceProvider.php | 3 - src/Http/Validation/Validator.php | 61 ++++++--- 5 files changed, 79 insertions(+), 175 deletions(-) create mode 100644 src/Http/Validation/Rules/In.php create mode 100644 src/Http/Validation/Rules/NotIn.php delete mode 100644 src/Http/Validation/Validates.php (limited to 'src/Http/Validation') diff --git a/src/Http/Validation/Rules/In.php b/src/Http/Validation/Rules/In.php new file mode 100644 index 00000000..d585cc3d --- /dev/null +++ b/src/Http/Validation/Rules/In.php @@ -0,0 +1,21 @@ +validateParameterCount(2, $parameters, __FUNCTION__); - $size = $this->getSize($value); - - return $size >= $parameters[0] && $size <= $parameters[1]; - } - - /** - * @param mixed $value - * @return bool - */ - public function bool($value): bool - { - return in_array($value, ['1', 1, true, '0', 0, false], true); - } - - /** - * @param mixed $value - * @param array $parameters ['1,2,3,56,7'] - * @return bool - */ - public function in($value, $parameters): bool - { - $this->validateParameterCount(1, $parameters, __FUNCTION__); - - return in_array($value, explode(',', $parameters[0])); - } - - /** - * @param mixed $value - * @return bool - */ - public function int($value): bool - { - return filter_var($value, FILTER_VALIDATE_INT) !== false; - } - - /** - * @param string $value - * @param array $parameters ['max'] - * @return bool - */ - public function max($value, $parameters): bool - { - $this->validateParameterCount(1, $parameters, __FUNCTION__); - $size = $this->getSize($value); - - return $size <= $parameters[0]; - } - - /** - * @param string $value - * @param array $parameters ['min'] - * @return bool - */ - public function min($value, $parameters) - { - $this->validateParameterCount(1, $parameters, __FUNCTION__); - $size = $this->getSize($value); - - return $size >= $parameters[0]; - } - - /** - * @param mixed $value - * @param array $parameters ['1,2,3,56,7'] - * @return bool - */ - public function notIn($value, $parameters): bool - { - $this->validateParameterCount(1, $parameters, __FUNCTION__); - - return !$this->in($value, $parameters); - } - - /** - * @param mixed $value - * @return bool - */ - public function numeric($value): bool - { - return is_numeric($value); - } - - /** - * @param mixed $value - * @return bool - */ - public function required($value): bool - { - if ( - is_null($value) - || (is_string($value) && trim($value) === '') - ) { - return false; - } - - return true; - } - - /** - * @param mixed $value - * @return int|float - */ - protected function getSize($value) - { - if (is_numeric($value)) { - return $value; - } - - return mb_strlen($value); - } - - /** - * @param int $count - * @param array $parameters - * @param string $rule - * - * @throws InvalidArgumentException - */ - protected function validateParameterCount(int $count, array $parameters, string $rule) - { - if (count($parameters) < $count) { - throw new InvalidArgumentException(sprintf( - 'The rule "%s" requires at least %d parameters', - $rule, - $count - )); - } - } -} diff --git a/src/Http/Validation/ValidationServiceProvider.php b/src/Http/Validation/ValidationServiceProvider.php index 2f1c6359..14530ae6 100644 --- a/src/Http/Validation/ValidationServiceProvider.php +++ b/src/Http/Validation/ValidationServiceProvider.php @@ -10,9 +10,6 @@ class ValidationServiceProvider extends ServiceProvider { public function register() { - $validates = $this->app->make(Validates::class); - $this->app->instance(Validates::class, $validates); - $validator = $this->app->make(Validator::class); $this->app->instance(Validator::class, $validator); $this->app->instance('validator', $validator); diff --git a/src/Http/Validation/Validator.php b/src/Http/Validation/Validator.php index a9235a5f..0bd846bd 100644 --- a/src/Http/Validation/Validator.php +++ b/src/Http/Validation/Validator.php @@ -4,25 +4,23 @@ namespace Engelsystem\Http\Validation; use Illuminate\Support\Str; use InvalidArgumentException; +use Respect\Validation\Exceptions\ComponentException; +use Respect\Validation\Validator as RespectValidator; class Validator { - /** @var Validates */ - protected $validate; - /** @var string[] */ protected $errors = []; /** @var array */ protected $data = []; - /** - * @param Validates $validate - */ - public function __construct(Validates $validate) - { - $this->validate = $validate; - } + /** @var array */ + protected $mapping = [ + 'accepted' => 'TrueVal', + 'int' => 'IntVal', + 'required' => 'NotEmpty', + ]; /** * @param array $data @@ -35,29 +33,56 @@ class Validator $this->data = []; foreach ($rules as $key => $values) { + $v = new RespectValidator(); + $v->with('\\Engelsystem\\Http\\Validation\\Rules', true); + + $value = isset($data[$key]) ? $data[$key] : null; + foreach (explode('|', $values) as $parameters) { $parameters = explode(':', $parameters); $rule = array_shift($parameters); $rule = Str::camel($rule); + $rule = $this->map($rule); - if (!method_exists($this->validate, $rule)) { - throw new InvalidArgumentException('Unknown validation rule: ' . $rule); + try { + call_user_func_array([$v, $rule], $parameters); + } catch (ComponentException $e) { + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); } - $value = isset($data[$key]) ? $data[$key] : null; - if (!$this->validate->{$rule}($value, $parameters, $data)) { - $this->errors[$key][] = implode('.', ['validation', $key, $rule]); - - continue; + if ($v->validate($value)) { + $this->data[$key] = $value; + } else { + $this->errors[$key][] = implode('.', ['validation', $key, $this->mapBack($rule)]); } - $this->data[$key] = $value; + $v->removeRules(); } } return empty($this->errors); } + /** + * @param string $rule + * @return string + */ + protected function map($rule) + { + return $this->mapping[$rule] ?? $rule; + } + + /** + * @param string $rule + * @return string + */ + protected function mapBack($rule) + { + $mapping = array_flip($this->mapping); + + return $mapping[$rule] ?? $rule; + } + /** * @return array */ -- cgit v1.2.3-54-g00ecf