From f6b17476cef2d240b78b7c4caf1c36ec570e6837 Mon Sep 17 00:00:00 2001 From: Valentin Date: Mon, 6 Feb 2017 11:57:08 +0300 Subject: [PATCH 1/4] pull request validator check without alias --- source/Spiral/Validation/Validator.php | 16 +++++++++ .../Validation/Checkers/SimpleCheckerTest.php | 35 +++++++++++++++++++ .../Validation/Fixtures/SimpleTestChecker.php | 35 +++++++++++++++++++ tests/Validation/ValidatorTest.php | 34 ++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 tests/Validation/Checkers/SimpleCheckerTest.php create mode 100644 tests/Validation/Fixtures/SimpleTestChecker.php diff --git a/source/Spiral/Validation/Validator.php b/source/Spiral/Validation/Validator.php index ac632dbc1..c5cded313 100644 --- a/source/Spiral/Validation/Validator.php +++ b/source/Spiral/Validation/Validator.php @@ -333,6 +333,22 @@ protected function check(string $field, $value, &$condition, array $arguments = return $result; } + + //If checker doesn't have registered alias + if (class_exists($condition[0])) { + $checker = $this->container->get($condition[0])->withValidator($this); + + if ($checker instanceof CheckerInterface) { + $result = $checker->check($condition[1], $value, $arguments); + + if ($result === false) { + //To let validation() method know that message should be handled via Checker + return $checker; + } + + return $result; + } + } } if (is_array($condition)) { diff --git a/tests/Validation/Checkers/SimpleCheckerTest.php b/tests/Validation/Checkers/SimpleCheckerTest.php new file mode 100644 index 000000000..4492255b7 --- /dev/null +++ b/tests/Validation/Checkers/SimpleCheckerTest.php @@ -0,0 +1,35 @@ +assertFalse($checker->test('test2')); + $this->assertFalse($checker->test('')); + $this->assertFalse($checker->test(false)); + $this->assertFalse($checker->test(true)); + $this->assertFalse($checker->test(0)); + + $this->assertTrue($checker->test('test')); + } + + public function testString() + { + $checker = new SimpleTestChecker(new Container()); + + $this->assertFalse($checker->string('string2', 'string')); + $this->assertFalse($checker->string('', 'string')); + $this->assertFalse($checker->string(false, 'string')); + $this->assertFalse($checker->string(true, 'string')); + $this->assertFalse($checker->string(0, 'string')); + + $this->assertTrue($checker->string('string', 'string')); + } +} diff --git a/tests/Validation/Fixtures/SimpleTestChecker.php b/tests/Validation/Fixtures/SimpleTestChecker.php new file mode 100644 index 000000000..edd6efbc5 --- /dev/null +++ b/tests/Validation/Fixtures/SimpleTestChecker.php @@ -0,0 +1,35 @@ + '[[Given string should be equal to "test".]]', + 'string' => '[[Given string should be equal to another string.]]', + ]; + + /** + * @param string $value + * @return bool + */ + public function test(string $value): bool + { + return strcasecmp($value, 'test') === 0; + } + + /** + * @param string $value + * @param string $string + * @return bool + */ + public function string(string $value, $string): bool + { + return strcasecmp($value, $string) === 0; + } +} \ No newline at end of file diff --git a/tests/Validation/ValidatorTest.php b/tests/Validation/ValidatorTest.php index 01f95a871..ab0d99aef 100644 --- a/tests/Validation/ValidatorTest.php +++ b/tests/Validation/ValidatorTest.php @@ -11,6 +11,7 @@ use Interop\Container\ContainerInterface; use Psr\Log\LoggerInterface; use Spiral\Debug\LogsInterface; +use Spiral\Tests\Validation\Fixtures\SimpleTestChecker; use Spiral\Translator\TranslatorInterface; use Spiral\Validation\Checkers\AddressChecker; use Spiral\Validation\Checkers\TypeChecker; @@ -90,6 +91,8 @@ public function setUp() return new TypeChecker($this->container); case AddressChecker::class: return new AddressChecker($this->container); + case SimpleTestChecker::class: + return new SimpleTestChecker($this->container); default: // it actually must throw NotFoundException (interface), but it will not // because there is no real reason for SomeException (interface) @@ -167,6 +170,37 @@ public function testAliasesAndClasses() $this->assertTrue($validator->isValid()); } + public function testNotAliasChecker() + { + $validator = new Validator([], [], $this->config, $this->container); + + //Test rule without arguments + $validator->setRules(['string' => [SimpleTestChecker::class . '::test']]); + + $validator->setData(['string' => 'test']); + $this->assertTrue($validator->isValid()); + + $validator->setData(['string' => 'not a test']); + $this->assertFalse($validator->isValid()); + $this->assertEquals( + substr(SimpleTestChecker::MESSAGES['test'], 2, -2), + $validator->getErrors()['string'] + ); + + //Test rule with arguments + $validator->setRules(['string' => [[SimpleTestChecker::class . '::string', 'str']]]); + + $validator->setData(['string' => 'str']); + $this->assertTrue($validator->isValid()); + + $validator->setData(['string' => 'another str']); + $this->assertFalse($validator->isValid()); + $this->assertEquals( + substr(SimpleTestChecker::MESSAGES['string'], 2, -2), + $validator->getErrors()['string'] + ); + } + public function testRuleChain() { $validator = new Validator(['email' => ['notEmpty', 'email']], [], From 2cd9e71ab07224c8adb701df5911ed2cbe9ce610 Mon Sep 17 00:00:00 2001 From: Valentin Date: Tue, 7 Feb 2017 15:37:00 +0300 Subject: [PATCH 2/4] pull request validator check without alias. --- source/Spiral/Validation/Validator.php | 46 +++++++++++-------- .../Validation/Checkers/SimpleCheckerTest.php | 35 -------------- .../Validation/Fixtures/SimpleTestChecker.php | 35 -------------- tests/Validation/ValidatorTest.php | 31 +++++++------ 4 files changed, 44 insertions(+), 103 deletions(-) delete mode 100644 tests/Validation/Checkers/SimpleCheckerTest.php delete mode 100644 tests/Validation/Fixtures/SimpleTestChecker.php diff --git a/source/Spiral/Validation/Validator.php b/source/Spiral/Validation/Validator.php index c5cded313..2b96b035a 100644 --- a/source/Spiral/Validation/Validator.php +++ b/source/Spiral/Validation/Validator.php @@ -321,8 +321,7 @@ protected function check(string $field, $value, &$condition, array $arguments = try { if (!is_array($condition) && strpos($condition, ':')) { $condition = explode(':', $condition); - if ($this->config->hasChecker($condition[0])) { - + if ($this->hasChecker($condition[0])) { $checker = $this->getChecker($condition[0]); $result = $checker->check($condition[1], $value, $arguments); @@ -333,22 +332,6 @@ protected function check(string $field, $value, &$condition, array $arguments = return $result; } - - //If checker doesn't have registered alias - if (class_exists($condition[0])) { - $checker = $this->container->get($condition[0])->withValidator($this); - - if ($checker instanceof CheckerInterface) { - $result = $checker->check($condition[1], $value, $arguments); - - if ($result === false) { - //To let validation() method know that message should be handled via Checker - return $checker; - } - - return $result; - } - } } if (is_array($condition)) { @@ -371,6 +354,27 @@ protected function check(string $field, $value, &$condition, array $arguments = } } + /** + * Does validation config has alias defined for a given checker name or class exists + * + * @param string $name + * @return bool + */ + protected function hasChecker(string $name): bool + { + if ($this->config->hasChecker($name)) { + return true; + } + + if (class_exists($name)) { + $checker = $this->container->get($name); + + return $checker instanceof CheckerInterface; + } + + return false; + } + /** * Get or create instance of validation checker. * @@ -381,13 +385,15 @@ protected function check(string $field, $value, &$condition, array $arguments = */ protected function getChecker(string $name): CheckerInterface { - if (!$this->config->hasChecker($name)) { + if (!$this->hasChecker($name)) { throw new ValidationException( "Unable to create validation checker defined by '{$name}' name" ); } - return $this->container->get($this->config->checkerClass($name))->withValidator($this); + $name = $this->config->hasChecker($name) ? $this->config->checkerClass($name) : $name; + + return $this->container->get($name)->withValidator($this); } /** diff --git a/tests/Validation/Checkers/SimpleCheckerTest.php b/tests/Validation/Checkers/SimpleCheckerTest.php deleted file mode 100644 index 4492255b7..000000000 --- a/tests/Validation/Checkers/SimpleCheckerTest.php +++ /dev/null @@ -1,35 +0,0 @@ -assertFalse($checker->test('test2')); - $this->assertFalse($checker->test('')); - $this->assertFalse($checker->test(false)); - $this->assertFalse($checker->test(true)); - $this->assertFalse($checker->test(0)); - - $this->assertTrue($checker->test('test')); - } - - public function testString() - { - $checker = new SimpleTestChecker(new Container()); - - $this->assertFalse($checker->string('string2', 'string')); - $this->assertFalse($checker->string('', 'string')); - $this->assertFalse($checker->string(false, 'string')); - $this->assertFalse($checker->string(true, 'string')); - $this->assertFalse($checker->string(0, 'string')); - - $this->assertTrue($checker->string('string', 'string')); - } -} diff --git a/tests/Validation/Fixtures/SimpleTestChecker.php b/tests/Validation/Fixtures/SimpleTestChecker.php deleted file mode 100644 index edd6efbc5..000000000 --- a/tests/Validation/Fixtures/SimpleTestChecker.php +++ /dev/null @@ -1,35 +0,0 @@ - '[[Given string should be equal to "test".]]', - 'string' => '[[Given string should be equal to another string.]]', - ]; - - /** - * @param string $value - * @return bool - */ - public function test(string $value): bool - { - return strcasecmp($value, 'test') === 0; - } - - /** - * @param string $value - * @param string $string - * @return bool - */ - public function string(string $value, $string): bool - { - return strcasecmp($value, $string) === 0; - } -} \ No newline at end of file diff --git a/tests/Validation/ValidatorTest.php b/tests/Validation/ValidatorTest.php index ab0d99aef..f1f6ef21d 100644 --- a/tests/Validation/ValidatorTest.php +++ b/tests/Validation/ValidatorTest.php @@ -91,8 +91,6 @@ public function setUp() return new TypeChecker($this->container); case AddressChecker::class: return new AddressChecker($this->container); - case SimpleTestChecker::class: - return new SimpleTestChecker($this->container); default: // it actually must throw NotFoundException (interface), but it will not // because there is no real reason for SomeException (interface) @@ -172,32 +170,39 @@ public function testAliasesAndClasses() public function testNotAliasChecker() { - $validator = new Validator([], [], $this->config, $this->container); + $config = new ValidatorConfig([ + 'emptyConditions' => [], + 'checkers' => [], + 'aliases' => [] + ]); + $validator = new Validator([], [], $config, $this->container); //Test rule without arguments - $validator->setRules(['string' => [SimpleTestChecker::class . '::test']]); + $validator->setRules(['url' => [AddressChecker::class . '::url']]); - $validator->setData(['string' => 'test']); + $validator->setData(['url' => 'http://example.com']); $this->assertTrue($validator->isValid()); - $validator->setData(['string' => 'not a test']); + $validator->setData(['url' => 'example.com']); $this->assertFalse($validator->isValid()); + $this->assertEquals( - substr(SimpleTestChecker::MESSAGES['test'], 2, -2), - $validator->getErrors()['string'] + substr(AddressChecker::MESSAGES['url'], 2, -2), + $validator->getErrors()['url'] ); //Test rule with arguments - $validator->setRules(['string' => [[SimpleTestChecker::class . '::string', 'str']]]); + $validator->setRules(['url' => [[AddressChecker::class . '::url', false]]]); - $validator->setData(['string' => 'str']); + $validator->setData(['url' => 'example.com']); $this->assertTrue($validator->isValid()); - $validator->setData(['string' => 'another str']); + $validator->setData(['url' => 'before:after']); $this->assertFalse($validator->isValid()); + $this->assertEquals( - substr(SimpleTestChecker::MESSAGES['string'], 2, -2), - $validator->getErrors()['string'] + substr(AddressChecker::MESSAGES['url'], 2, -2), + $validator->getErrors()['url'] ); } From 2c30b8a63490132a7c9d5a7e2f0dd543737994c6 Mon Sep 17 00:00:00 2001 From: Valentin Date: Tue, 7 Feb 2017 16:24:39 +0300 Subject: [PATCH 3/4] pull request validator check without alias. --- source/Spiral/Validation/Validator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/source/Spiral/Validation/Validator.php b/source/Spiral/Validation/Validator.php index 2b96b035a..7a9ca8f99 100644 --- a/source/Spiral/Validation/Validator.php +++ b/source/Spiral/Validation/Validator.php @@ -391,6 +391,7 @@ protected function getChecker(string $name): CheckerInterface ); } + /** @var string $name */ $name = $this->config->hasChecker($name) ? $this->config->checkerClass($name) : $name; return $this->container->get($name)->withValidator($this); From 23a8c38f047668ac5e72fbddaf5ae47a657b4b80 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Tue, 7 Feb 2017 16:41:03 +0300 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7305a51a..1c433143f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG for 0.9.0 RC ====================== +0.9.6 (07.02.2017) +----- +* Dependencies update +* Validator can now accept checkers outside of it's config + 0.9.5 (07.02.2017) ----- * Proper timezone detection for datetime accessors