Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Feb 7, 2017
2 parents 6480cc4 + 23a8c38 commit df4aa87
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 27 additions & 4 deletions source/Spiral/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -355,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.
*
Expand All @@ -365,13 +385,16 @@ 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);
/** @var string $name */
$name = $this->config->hasChecker($name) ? $this->config->checkerClass($name) : $name;

return $this->container->get($name)->withValidator($this);
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/Validation/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,6 +168,44 @@ public function testAliasesAndClasses()
$this->assertTrue($validator->isValid());
}

public function testNotAliasChecker()
{
$config = new ValidatorConfig([
'emptyConditions' => [],
'checkers' => [],
'aliases' => []
]);
$validator = new Validator([], [], $config, $this->container);

//Test rule without arguments
$validator->setRules(['url' => [AddressChecker::class . '::url']]);

$validator->setData(['url' => 'http://example.com']);
$this->assertTrue($validator->isValid());

$validator->setData(['url' => 'example.com']);
$this->assertFalse($validator->isValid());

$this->assertEquals(
substr(AddressChecker::MESSAGES['url'], 2, -2),
$validator->getErrors()['url']
);

//Test rule with arguments
$validator->setRules(['url' => [[AddressChecker::class . '::url', false]]]);

$validator->setData(['url' => 'example.com']);
$this->assertTrue($validator->isValid());

$validator->setData(['url' => 'before:after']);
$this->assertFalse($validator->isValid());

$this->assertEquals(
substr(AddressChecker::MESSAGES['url'], 2, -2),
$validator->getErrors()['url']
);
}

public function testRuleChain()
{
$validator = new Validator(['email' => ['notEmpty', 'email']], [],
Expand Down

0 comments on commit df4aa87

Please sign in to comment.