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/Validator.php | 61 +++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 18 deletions(-) (limited to 'src/Http/Validation/Validator.php') 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