[PropertyAccess] Fixed CS and added missing documentation

This commit is contained in:
Bernhard Schussek 2014-03-28 19:21:04 +01:00
parent 6d2af217aa
commit 4262707e5a
3 changed files with 12 additions and 12 deletions

View File

@ -33,7 +33,7 @@ class PropertyAccessor implements PropertyAccessorInterface
/** /**
* @var Boolean * @var Boolean
*/ */
private $throwExceptionOnInvalidIndex; private $ignoreInvalidIndices;
/** /**
* Should not be used by application code. Use * Should not be used by application code. Use
@ -42,7 +42,7 @@ class PropertyAccessor implements PropertyAccessorInterface
public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false) public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false)
{ {
$this->magicCall = $magicCall; $this->magicCall = $magicCall;
$this->throwExceptionOnInvalidIndex = $throwExceptionOnInvalidIndex; $this->ignoreInvalidIndices = !$throwExceptionOnInvalidIndex;
} }
/** /**
@ -56,7 +56,7 @@ class PropertyAccessor implements PropertyAccessorInterface
throw new UnexpectedTypeException($propertyPath, 'string or Symfony\Component\PropertyAccess\PropertyPathInterface'); throw new UnexpectedTypeException($propertyPath, 'string or Symfony\Component\PropertyAccess\PropertyPathInterface');
} }
$propertyValues =& $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->throwExceptionOnInvalidIndex); $propertyValues =& $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
return $propertyValues[count($propertyValues) - 1][self::VALUE]; return $propertyValues[count($propertyValues) - 1][self::VALUE];
} }
@ -117,7 +117,7 @@ class PropertyAccessor implements PropertyAccessorInterface
} }
try { try {
$this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->throwExceptionOnInvalidIndex); $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
return true; return true;
} catch (NoSuchIndexException $e) { } catch (NoSuchIndexException $e) {
@ -186,15 +186,18 @@ class PropertyAccessor implements PropertyAccessorInterface
/** /**
* Reads the path from an object up to a given path index. * Reads the path from an object up to a given path index.
* *
* @param object|array $objectOrArray The object or array to read from * @param object|array $objectOrArray The object or array to read from
* @param PropertyPathInterface $propertyPath The property path to read * @param PropertyPathInterface $propertyPath The property path to read
* @param integer $lastIndex The index up to which should be read * @param integer $lastIndex The index up to which should be read
* @param Boolean $ignoreInvalidIndices Whether to ignore invalid indices
* or throw an exception
* *
* @return array The values read in the path. * @return array The values read in the path.
* *
* @throws UnexpectedTypeException If a value within the path is neither object nor array. * @throws UnexpectedTypeException If a value within the path is neither object nor array.
* @throws NoSuchIndexException If a non-existing index is accessed
*/ */
private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex, $throwExceptionOnInvalidIndex = false) private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex, $ignoreInvalidIndices = true)
{ {
$propertyValues = array(); $propertyValues = array();
@ -209,7 +212,7 @@ class PropertyAccessor implements PropertyAccessorInterface
// Create missing nested arrays on demand // Create missing nested arrays on demand
if ($isIndex && $isArrayAccess && !isset($objectOrArray[$property])) { if ($isIndex && $isArrayAccess && !isset($objectOrArray[$property])) {
if ($throwExceptionOnInvalidIndex) { if (!$ignoreInvalidIndices) {
throw new NoSuchIndexException(sprintf('Cannot read property "%s". Available properties are "%s"', $property, print_r(array_keys($objectOrArray), true))); throw new NoSuchIndexException(sprintf('Cannot read property "%s". Available properties are "%s"', $property, print_r(array_keys($objectOrArray), true)));
} }

View File

@ -11,9 +11,7 @@
namespace Symfony\Component\PropertyAccess\Tests; namespace Symfony\Component\PropertyAccess\Tests;
use Symfony\Component\PropertyAccess\Exception\ExceptionInterface;
use Symfony\Component\PropertyAccess\PropertyAccessor; use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\StringUtil;
class PropertyAccessorCollectionTest_Car class PropertyAccessorCollectionTest_Car
{ {

View File

@ -13,7 +13,6 @@ namespace Symfony\Component\PropertyAccess\Tests;
use Symfony\Component\PropertyAccess\PropertyAccessor; use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClass; use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClass;
use Symfony\Component\PropertyAccess\Tests\Fixtures\Magician;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicCall; use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicCall;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicGet; use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicGet;