Skip to content

Commit

Permalink
Merge pull request particle-php#41 from berry-langerak/feature/valida…
Browse files Browse the repository at this point in the history
…ting-in-arrays

Feature/validating in arrays
  • Loading branch information
berry-langerak committed May 24, 2015
2 parents c3e3b9a + 3464fbe commit bf034d5
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/Particle/Validator/Value/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(array $values = [])
*/
public function has($key)
{
return array_key_exists($key, $this->values);
return $this->traverse($key, false);
}

/**
Expand All @@ -51,7 +51,7 @@ public function has($key)
*/
public function get($key)
{
return $this->has($key) ? $this->values[$key] : null;
return $this->traverse($key, true);
}

/**
Expand All @@ -63,6 +63,9 @@ public function get($key)
*/
public function set($key, $value)
{
if (strpos($key, '.') !== false) {
return $this->setTraverse($key, $value);
}
$this->values[$key] = $value;
return $this;
}
Expand All @@ -76,4 +79,46 @@ public function getArrayCopy()
{
return $this->values;
}

/**
* Traverses the key using dot notation. Based on the second parameter, it will return the value or if it was set.
*
* @param string $key
* @param bool $returnValue
* @return mixed
*/
protected function traverse($key, $returnValue = true)
{
$value = $this->values;
foreach (explode('.', $key) as $part) {
if (!array_key_exists($part, $value)) {
return false;
}
$value = $value[$part];
}
return $returnValue ? $value : true;
}

/**
* Uses dot-notation to set a value.
*
* @param string $key
* @param mixed $value
* @return $this
*/
protected function setTraverse($key, $value)
{
$parts = explode('.', $key);
$ref = &$this->values;

foreach ($parts as $i => $part) {
if ($i < count($parts) - 1) {
$ref[$part] = [];
}
$ref = &$ref[$part];
}

$ref = $value;
return $this;
}
}
56 changes: 56 additions & 0 deletions tests/Particle/Validator/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,60 @@ public function testReturnsEmptyArrayInsteadOfValidatedValues()
$this->validator->required('first_name')->lengthBetween(2, 20);
$this->assertEquals([], $this->validator->getValues());
}

public function testCanUseDotNotationToValidateInArrays()
{
$this->validator->required('user.contact.email')->email();

$result = $this->validator->validate([
'user' => [
'contact' => [
'email' => '[email protected]'
]
]
]);

$this->assertTrue($result);
}

public function testDotNotationIsAddedToMessagesVerbatim()
{
$this->validator->required('user.email');
$result = $this->validator->validate([]);

$expected = [
'user.email' => [
Required::NON_EXISTENT_KEY => 'user.email must be provided, but does not exist'
]
];

$this->assertFalse($result);
$this->assertEquals($expected, $this->validator->getMessages());
}

public function testDotNotationIsAlsoUsedForOutputValueContainer()
{
$input = [
'user' => [
'email' => '[email protected]'
]
];

$this->validator->required('user.email');
$this->validator->validate($input);
$this->assertEquals($input, $this->validator->getValues());
}

public function testDotNotationWillReturnTrueForNullRequiredValue()
{
$this->validator->required('user.email', 'user email address', true);

$result = $this->validator->validate([
'user' => [
'email' => null,
]
]);

$this->assertTrue($result);
}
}

0 comments on commit bf034d5

Please sign in to comment.