minor #13742 [PropertyAccess] refactor type checks (Tobion)
This PR was merged into the 2.3 branch. Discussion ---------- [PropertyAccess] refactor type checks | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | - #13735 for 2.3 Commits -------9cacecb
[PropertyAccess] the property path constructor already implements the type check4e11c07
[PropertyAccess] refactor type checks to remove duplicate logic
This commit is contained in:
commit
7b9bc804e7
@ -40,10 +40,8 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
*/
|
*/
|
||||||
public function getValue($objectOrArray, $propertyPath)
|
public function getValue($objectOrArray, $propertyPath)
|
||||||
{
|
{
|
||||||
if (is_string($propertyPath)) {
|
if (!$propertyPath instanceof PropertyPathInterface) {
|
||||||
$propertyPath = new PropertyPath($propertyPath);
|
$propertyPath = new PropertyPath($propertyPath);
|
||||||
} elseif (!$propertyPath instanceof PropertyPathInterface) {
|
|
||||||
throw new UnexpectedTypeException($propertyPath, 'string or Symfony\Component\PropertyAccess\PropertyPathInterface');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$propertyValues = & $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength());
|
$propertyValues = & $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength());
|
||||||
@ -56,10 +54,8 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
*/
|
*/
|
||||||
public function setValue(&$objectOrArray, $propertyPath, $value)
|
public function setValue(&$objectOrArray, $propertyPath, $value)
|
||||||
{
|
{
|
||||||
if (is_string($propertyPath)) {
|
if (!$propertyPath instanceof PropertyPathInterface) {
|
||||||
$propertyPath = new PropertyPath($propertyPath);
|
$propertyPath = new PropertyPath($propertyPath);
|
||||||
} elseif (!$propertyPath instanceof PropertyPathInterface) {
|
|
||||||
throw new UnexpectedTypeException($propertyPath, 'string or Symfony\Component\PropertyAccess\PropertyPathInterface');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$propertyValues = & $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength() - 1);
|
$propertyValues = & $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength() - 1);
|
||||||
@ -75,10 +71,6 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
$objectOrArray = & $propertyValues[$i][self::VALUE];
|
$objectOrArray = & $propertyValues[$i][self::VALUE];
|
||||||
|
|
||||||
if ($overwrite) {
|
if ($overwrite) {
|
||||||
if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
|
|
||||||
throw new UnexpectedTypeException($objectOrArray, 'object or array');
|
|
||||||
}
|
|
||||||
|
|
||||||
$property = $propertyPath->getElement($i);
|
$property = $propertyPath->getElement($i);
|
||||||
//$singular = $propertyPath->singulars[$i];
|
//$singular = $propertyPath->singulars[$i];
|
||||||
$singular = null;
|
$singular = null;
|
||||||
@ -108,13 +100,13 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
*/
|
*/
|
||||||
private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex)
|
private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex)
|
||||||
{
|
{
|
||||||
$propertyValues = array();
|
|
||||||
|
|
||||||
for ($i = 0; $i < $lastIndex; ++$i) {
|
|
||||||
if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
|
if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
|
||||||
throw new UnexpectedTypeException($objectOrArray, 'object or array');
|
throw new UnexpectedTypeException($objectOrArray, 'object or array');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$propertyValues = array();
|
||||||
|
|
||||||
|
for ($i = 0; $i < $lastIndex; ++$i) {
|
||||||
$property = $propertyPath->getElement($i);
|
$property = $propertyPath->getElement($i);
|
||||||
$isIndex = $propertyPath->isIndex($i);
|
$isIndex = $propertyPath->isIndex($i);
|
||||||
|
|
||||||
@ -137,6 +129,11 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
|
|
||||||
$objectOrArray = & $propertyValue[self::VALUE];
|
$objectOrArray = & $propertyValue[self::VALUE];
|
||||||
|
|
||||||
|
// the final value of the path must not be validated
|
||||||
|
if ($i + 1 < $propertyPath->getLength() && !is_object($objectOrArray) && !is_array($objectOrArray)) {
|
||||||
|
throw new UnexpectedTypeException($objectOrArray, 'object or array');
|
||||||
|
}
|
||||||
|
|
||||||
$propertyValues[] = & $propertyValue;
|
$propertyValues[] = & $propertyValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,6 +29,20 @@ class PropertyAccessorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->propertyAccessor = new PropertyAccessor();
|
$this->propertyAccessor = new PropertyAccessor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getPathsWithUnexpectedType()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('', 'foobar'),
|
||||||
|
array('foo', 'foobar'),
|
||||||
|
array(null, 'foobar'),
|
||||||
|
array(123, 'foobar'),
|
||||||
|
array((object) array('prop' => null), 'prop.foobar'),
|
||||||
|
array((object) array('prop' => (object) array('subProp' => null)), 'prop.subProp.foobar'),
|
||||||
|
array(array('index' => null), '[index][foobar]'),
|
||||||
|
array(array('index' => array('subIndex' => null)), '[index][subIndex][foobar]'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testGetValueReadsArray()
|
public function testGetValueReadsArray()
|
||||||
{
|
{
|
||||||
$array = array('firstName' => 'Bernhard');
|
$array = array('firstName' => 'Bernhard');
|
||||||
@ -198,27 +212,13 @@ class PropertyAccessorTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @dataProvider getPathsWithUnexpectedType
|
||||||
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
|
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
|
||||||
|
* @expectedExceptionMessage Expected argument of type "object or array"
|
||||||
*/
|
*/
|
||||||
public function testGetValueThrowsExceptionIfNotObjectOrArray()
|
public function testGetValueThrowsExceptionIfNotObjectOrArray($objectOrArray, $path)
|
||||||
{
|
{
|
||||||
$this->propertyAccessor->getValue('baz', 'foobar');
|
$this->propertyAccessor->getValue($objectOrArray, $path);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
|
|
||||||
*/
|
|
||||||
public function testGetValueThrowsExceptionIfNull()
|
|
||||||
{
|
|
||||||
$this->propertyAccessor->getValue(null, 'foobar');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
|
|
||||||
*/
|
|
||||||
public function testGetValueThrowsExceptionIfEmpty()
|
|
||||||
{
|
|
||||||
$this->propertyAccessor->getValue('', 'foobar');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetValueWhenArrayValueIsNull()
|
public function testGetValueWhenArrayValueIsNull()
|
||||||
@ -311,33 +311,13 @@ class PropertyAccessorTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @dataProvider getPathsWithUnexpectedType
|
||||||
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
|
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
|
||||||
|
* @expectedExceptionMessage Expected argument of type "object or array"
|
||||||
*/
|
*/
|
||||||
public function testSetValueThrowsExceptionIfNotObjectOrArray()
|
public function testSetValueThrowsExceptionIfNotObjectOrArray($objectOrArray, $path)
|
||||||
{
|
{
|
||||||
$value = 'baz';
|
$this->propertyAccessor->setValue($objectOrArray, $path, 'value');
|
||||||
|
|
||||||
$this->propertyAccessor->setValue($value, 'foobar', 'bam');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
|
|
||||||
*/
|
|
||||||
public function testSetValueThrowsExceptionIfNull()
|
|
||||||
{
|
|
||||||
$value = null;
|
|
||||||
|
|
||||||
$this->propertyAccessor->setValue($value, 'foobar', 'bam');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException \Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
|
|
||||||
*/
|
|
||||||
public function testSetValueThrowsExceptionIfEmpty()
|
|
||||||
{
|
|
||||||
$value = '';
|
|
||||||
|
|
||||||
$this->propertyAccessor->setValue($value, 'foobar', 'bam');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user