mirror of
https://github.com/Respect/Validation.git
synced 2024-06-09 17:22:20 +02:00
Always check the type then executing KeyNested
This commit is contained in:
parent
fb7f19f9ad
commit
7e88a7a0c8
|
@ -29,56 +29,56 @@ class KeyNested extends AbstractRelated
|
|||
|
||||
private function getReferencePieces()
|
||||
{
|
||||
return explode('.', $this->reference);
|
||||
return explode('.', rtrim($this->reference, '.'));
|
||||
}
|
||||
|
||||
private function getReferenceArrayValue($input)
|
||||
private function getValueFromArray($array, $key)
|
||||
{
|
||||
$keys = $this->getReferencePieces();
|
||||
$value = $input;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
$value = $value[$key];
|
||||
if (!array_key_exists($key, $array)) {
|
||||
$message = sprintf('Cannot select the key %s from the given array', $this->reference);
|
||||
throw new ComponentException($message);
|
||||
}
|
||||
|
||||
return $value;
|
||||
return $array[$key];
|
||||
}
|
||||
|
||||
private function getReferenceObjectValue($input)
|
||||
private function getValueFromObject($object, $property)
|
||||
{
|
||||
$properties = $this->getReferencePieces();
|
||||
$value = $input;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
$value = $value->$property;
|
||||
if (empty($property) || !property_exists($object, $property)) {
|
||||
$message = sprintf('Cannot select the property %s from the given object', $this->reference);
|
||||
throw new ComponentException($message);
|
||||
}
|
||||
|
||||
return $value;
|
||||
return $object->{$property};
|
||||
}
|
||||
|
||||
private function getValue($value, $key)
|
||||
{
|
||||
if (is_array($value) || $value instanceof ArrayAccess) {
|
||||
return $this->getValueFromArray($value, $key);
|
||||
}
|
||||
|
||||
if (is_object($value)) {
|
||||
return $this->getValueFromObject($value, $key);
|
||||
}
|
||||
|
||||
$message = sprintf('Cannot select the property %s from the given data', $this->reference);
|
||||
throw new ComponentException($message);
|
||||
}
|
||||
|
||||
public function getReferenceValue($input)
|
||||
{
|
||||
if (is_array($input) || $input instanceof ArrayAccess) {
|
||||
return $this->getReferenceArrayValue($input);
|
||||
if (is_scalar($input)) {
|
||||
$message = sprintf('Cannot select the %s in the given data', $this->reference);
|
||||
throw new ComponentException($message);
|
||||
}
|
||||
|
||||
if (is_object($input)) {
|
||||
return $this->getReferenceObjectValue($input);
|
||||
$keys = $this->getReferencePieces();
|
||||
$value = $input;
|
||||
while (!is_null($key = array_shift($keys))) {
|
||||
$value = $this->getValue($value, $key);
|
||||
}
|
||||
|
||||
$message = sprintf('Cannot select the %s in the given data', $this->reference);
|
||||
throw new ComponentException($message);
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
|
40
tests/integration/issue-727.phpt
Normal file
40
tests/integration/issue-727.phpt
Normal file
|
@ -0,0 +1,40 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\PhoneException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
$work = new stdClass();
|
||||
$work->number = "+61.(03) 4546 5498";
|
||||
$work->countryCode = 61;
|
||||
$work->primary = true;
|
||||
|
||||
$personal = new stdClass();
|
||||
$personal->number = "+61.0406 464 890";
|
||||
$personal->country = 61;
|
||||
$personal->primary = false;
|
||||
|
||||
$phoneNumbers = new stdClass();
|
||||
$phoneNumbers->personal = $personal;
|
||||
$phoneNumbers->work = $work;
|
||||
|
||||
$validateThis = ['phoneNumbers' => $phoneNumbers];
|
||||
|
||||
try {
|
||||
v::create()
|
||||
->keyNested('phoneNumbers.personal.country', v::intType(), false)
|
||||
->keyNested('phoneNumbers.personal.number', v::phone(), false)
|
||||
->keyNested('phoneNumbers.personal.primary', v::boolType(), false)
|
||||
->keyNested('phoneNumbers.work.country', v::intType(), false)
|
||||
->keyNested('phoneNumbers.work.number', v::phone(), false)
|
||||
->keyNested('phoneNumbers.work.primary', v::boolType(), false)
|
||||
->check($validateThis);
|
||||
|
||||
} catch (PhoneException $exception) {
|
||||
echo $exception->getMainMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
phoneNumbers.personal.number must be a valid telephone number
|
|
@ -67,7 +67,7 @@ class KeyNestedTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertTrue($rule->validate($array));
|
||||
}
|
||||
|
||||
public function testOnjectWithPresentPropertiesWillReturnTrueForDirtyPathValidator()
|
||||
public function testObjectWithPresentPropertiesWillReturnTrueForDirtyPathValidator()
|
||||
{
|
||||
$object = (object) [
|
||||
'bar' => (object) [
|
||||
|
|
Loading…
Reference in a new issue