mirror of
https://github.com/Respect/Validation.git
synced 2024-06-28 02:00:06 +02:00
Implements numeric Key/KeyNested Addressing
The Key and KeyNested rules didn't supported numeric addressing (e.g. `v::key(42);`). This change implements it and adds new tests for the behavior.
This commit is contained in:
parent
19698d6a38
commit
84fa389d4c
|
@ -18,7 +18,7 @@ class Key extends AbstractRelated
|
|||
{
|
||||
public function __construct($reference, Validatable $referenceValidator = null, $mandatory = true)
|
||||
{
|
||||
if (!is_string($reference) || empty($reference)) {
|
||||
if (!is_scalar($reference) || '' === $reference) {
|
||||
throw new ComponentException('Invalid array key name');
|
||||
}
|
||||
parent::__construct($reference, $referenceValidator, $mandatory);
|
||||
|
|
|
@ -36,8 +36,9 @@ class KeyNested extends AbstractRelated
|
|||
{
|
||||
$keys = $this->getReferencePieces();
|
||||
$value = $input;
|
||||
while (($key = array_shift($keys))) {
|
||||
if (!isset($value[$key])) {
|
||||
|
||||
while (!is_null($key = array_shift($keys))) {
|
||||
if (!array_key_exists($key, $value)) {
|
||||
$message = sprintf('Cannot select the key %s from the given array', $this->reference);
|
||||
throw new ComponentException($message);
|
||||
}
|
||||
|
@ -52,8 +53,11 @@ class KeyNested extends AbstractRelated
|
|||
{
|
||||
$properties = $this->getReferencePieces();
|
||||
$value = $input;
|
||||
while (($property = array_shift($properties))) {
|
||||
if (!isset($value->$property)) {
|
||||
|
||||
while (!is_null($property = array_shift($properties)) &&
|
||||
'' != $property
|
||||
) {
|
||||
if (!is_object($value) || !property_exists($value, $property)) {
|
||||
$message = sprintf('Cannot select the property %s from the given object', $this->reference);
|
||||
throw new ComponentException($message);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
--TEST--
|
||||
alwaysInvalid()
|
||||
keyNested()
|
||||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
|
|
@ -38,6 +38,17 @@ class KeyNestedTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertTrue($rule->validate($array));
|
||||
}
|
||||
|
||||
public function testArrayWithNumericKeysWillReturnTrueForFullPathValidator()
|
||||
{
|
||||
$array = [
|
||||
0 => 'Zero, the hero!'
|
||||
];
|
||||
|
||||
$rule = new KeyNested(0, new Equals('Zero, the hero!'));
|
||||
|
||||
$this->assertTrue($rule->check($array));
|
||||
}
|
||||
|
||||
public function testArrayWithPresentKeysWillReturnTrueForHalfPathValidator()
|
||||
{
|
||||
$array = [
|
||||
|
|
|
@ -21,11 +21,17 @@ class KeyTest extends \PHPUnit_Framework_TestCase
|
|||
public function testArrayWithPresentKeyShouldReturnTrue()
|
||||
{
|
||||
$validator = new Key('bar');
|
||||
$obj = [];
|
||||
$obj['bar'] = 'foo';
|
||||
$this->assertTrue($validator->assert($obj));
|
||||
$this->assertTrue($validator->check($obj));
|
||||
$this->assertTrue($validator->validate($obj));
|
||||
$someArray = [];
|
||||
$someArray['bar'] = 'foo';
|
||||
$this->assertTrue($validator->validate($someArray));
|
||||
}
|
||||
|
||||
public function testArrayWithNumericKeyShouldReturnTrue()
|
||||
{
|
||||
$validator = new Key(0);
|
||||
$someArray = [];
|
||||
$someArray[0] = 'foo';
|
||||
$this->assertTrue($validator->validate($someArray));
|
||||
}
|
||||
|
||||
public function testEmptyInputMustReturnFalse()
|
||||
|
@ -60,8 +66,6 @@ class KeyTest extends \PHPUnit_Framework_TestCase
|
|||
$input = [];
|
||||
$input['someEmptyKey'] = '';
|
||||
|
||||
$this->assertTrue($validator->assert($input));
|
||||
$this->assertTrue($validator->check($input));
|
||||
$this->assertTrue($validator->validate($input));
|
||||
}
|
||||
|
||||
|
@ -91,9 +95,9 @@ class KeyTest extends \PHPUnit_Framework_TestCase
|
|||
public function testArrayWithAbsentKeyShouldThrowKeyException()
|
||||
{
|
||||
$validator = new Key('bar');
|
||||
$obj = [];
|
||||
$obj['baraaaaaa'] = 'foo';
|
||||
$this->assertTrue($validator->assert($obj));
|
||||
$someArray = [];
|
||||
$someArray['baraaaaaa'] = 'foo';
|
||||
$this->assertTrue($validator->assert($someArray));
|
||||
}
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\KeyException
|
||||
|
@ -101,8 +105,8 @@ class KeyTest extends \PHPUnit_Framework_TestCase
|
|||
public function testNotArrayShouldThrowKeyException()
|
||||
{
|
||||
$validator = new Key('bar');
|
||||
$obj = 123;
|
||||
$this->assertFalse($validator->assert($obj));
|
||||
$someArray = 123;
|
||||
$this->assertFalse($validator->assert($someArray));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -117,16 +121,16 @@ class KeyTest extends \PHPUnit_Framework_TestCase
|
|||
{
|
||||
$subValidator = new Length(1, 3);
|
||||
$validator = new Key('bar', $subValidator);
|
||||
$obj = [];
|
||||
$obj['bar'] = 'foo';
|
||||
$this->assertTrue($validator->assert($obj));
|
||||
$someArray = [];
|
||||
$someArray['bar'] = 'foo';
|
||||
$this->assertTrue($validator->assert($someArray));
|
||||
}
|
||||
|
||||
public function testNotMandatoryExtraValidatorShouldPassWithAbsentKey()
|
||||
{
|
||||
$subValidator = new Length(1, 3);
|
||||
$validator = new Key('bar', $subValidator, false);
|
||||
$obj = [];
|
||||
$this->assertTrue($validator->validate($obj));
|
||||
$someArray = [];
|
||||
$this->assertTrue($validator->validate($someArray));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue