diff --git a/src/Rule/AbstractRule.php b/src/Rule/AbstractRule.php index 7a5af83..3728315 100644 --- a/src/Rule/AbstractRule.php +++ b/src/Rule/AbstractRule.php @@ -60,7 +60,7 @@ public function getKey(): string return $this->key; } - public function getValue(): string + public function getValue() { return $this->value; } diff --git a/src/Rule/RuleInterface.php b/src/Rule/RuleInterface.php index 006621c..0940ed5 100644 --- a/src/Rule/RuleInterface.php +++ b/src/Rule/RuleInterface.php @@ -70,7 +70,7 @@ public function getKey(): string; * Retrieve the value. Value will be trimmed * if trim is set to true. * - * @return string + * @return mixed */ - public function getValue(): string; + public function getValue(); } diff --git a/src/Validator.php b/src/Validator.php new file mode 100644 index 0000000..8e14adc --- /dev/null +++ b/src/Validator.php @@ -0,0 +1,126 @@ +setContext($context); + $this->setStopOnError($stopOnError); + } + + public function setContext(array $context): self + { + $this->context = $context; + return $this; + } + + public function getValidatedContext(): array + { + return $this->validatedContext; + } + + public function setRules(array $rules):self + { + $this->rules = []; + foreach ($rules as $rule) { + // The first element must be the key context + $key = $rule[0]; + // The second element must be the class valdiator name + $class = $rule[1]; + $this->rules[] = new $class($key, $this->get($key), $rule); + } + // Keep chaining + return $this; + } + + public function getRules(): array + { + return $this->rules; + } + + public function getErrors(): array + { + return $this->errors; + } + + public function get(string $key) + { + return $this->context[$key] ?? null; + } + + public function shouldStopOnError(): bool + { + return $this->stopOnError; + } + + public function setStopOnError(bool $stopOnError): self + { + $this->stopOnError = $stopOnError; + + return $this; + } + + public function validate(): bool + { + $this->validatedContext = []; + + // All rules supposed OK + $res = true; + $this->errors = []; + + foreach ($this->rules as $rule) { + /* @var $rule RuleInterface */ + if ($rule->validate() !== RuleInterface::ERROR) { + $this->validatedContext[$rule->getKey()] = $rule->getValue(); + continue; + } + + $this->errors[] = $rule->getError(); + $res = false; + + if ($this->stopOnError) { + return $res; + } + } + + return $res; + } +} diff --git a/src/ValidatorInterface.php b/src/ValidatorInterface.php index 3f2fc8a..cb88bcc 100644 --- a/src/ValidatorInterface.php +++ b/src/ValidatorInterface.php @@ -92,6 +92,8 @@ public function shouldStopOnError(): bool; /** * Change stopOnError value. + * + * @return ValidatorInterface */ public function setStopOnError(bool $stopOnError); @@ -103,5 +105,5 @@ public function setStopOnError(bool $stopOnError); * * @return bool */ - public function valdiate(): bool; + public function validate(): bool; } diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php new file mode 100644 index 0000000..06d20be --- /dev/null +++ b/tests/ValidatorTest.php @@ -0,0 +1,84 @@ + 'Ben ']); + + $rule = ['name', StringRule::class, 'min' => 3, 'max' => 30]; + + $validator->setRules([$rule]); + + $res = $validator->validate(); + assertThat($res, is(true)); + + $validatedContext = $validator->getValidatedContext(); + assertThat($validatedContext, anArray(['name' => 'Ben'])); + + $value = $validator->get('name'); + assertThat($value, identicalTo('Ben ')); + + $value = $validator->get('age'); + assertThat($value, nullValue()); + + $rules = $validator->getRules(); + assertThat($rules[0], anInstanceOf(StringRule::class)); + + assertThat($validator->shouldStopOnError(), is(false)); + } + + /** + * @dataProvider getValidatorProvider + */ + public function testValidate($context, $rules, $expectedResult, $errorsSize): void + { + $validator = new Validator($context); + $validator->setRules($rules); + + $res = $validator->validate(); + + assertThat($res, identicalTo($expectedResult)); + assertThat($validator->getErrors(), arrayWithSize($errorsSize)); + } + + public function getValidatorProvider(): \Generator + { + yield 'Age and name are valid' => [ + // context + [ + 'age' => 25, + 'name' => 'Ben' + ], + // rules + [ + ['age', NumericRule::class, 'min' => 5, 'max' => 65], + ['name', StringRule::class, 'min' => 3, 'max' => 30], + ], + // expectedResult + true, + // $errorsSize + 0 + ]; + + yield 'Age is not valid' => [ + [ + 'age' => 25, + ], + [ + ['age', NumericRule::class, 'min' => 26, 'max' => 65], + ], + false, + 1 + ]; + } +}