8000 [PropertyAccess] Added isReadable() and isWritable() by webmozart · Pull Request #10570 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] Added isReadable() and isWritable() #10570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 31, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[PropertyAccess] Fixed CS and added missing documentation
  • Loading branch information
webmozart committed Mar 28, 2014
commit 4262707e5aeee5b67448ba2fb422d2f6c8074445
21 changes: 12 additions & 9 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PropertyAccessor implements PropertyAccessorInterface
/**
* @var Boolean
*/
private $throwExceptionOnInvalidIndex;
private $ignoreInvalidIndices;

/**
* 8000 Should not be used by application code. Use
Expand All @@ -42,7 +42,7 @@ class PropertyAccessor implements PropertyAccessorInterface
public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false)
{
$this->magicCall = $magicCall;
$this->throwExceptionOnInvalidIndex = $throwExceptionOnInvalidIndex;
$this->ignoreInvalidIndices = !$throwExceptionOnInvalidIndex;
}

/**
Expand All @@ -56,7 +56,7 @@ public function getValue($objectOrArray, $propertyPath)
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];
}
Expand Down Expand Up @@ -117,7 +117,7 @@ public function isReadable($objectOrArray, $propertyPath)
}

try {
$this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->throwExceptionOnInvalidIndex);
$this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);

return true;
} catch (NoSuchIndexException $e) {
Expand Down Expand Up @@ -186,15 +186,18 @@ public function isWritable($objectOrArray, $propertyPath, $value)
/**
* Reads the path from an object up to a given path index.
*
* @param object|array $objectOrArray The object or array to read from
* @param PropertyPathInterface $propertyPath The property path to read
* @param integer $lastIndex The index up to which should be read
* @param object|array $objectOrArray The object or array to read from
* @param PropertyPathInterface $propertyPath The propert 10000 y path to 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.
*
* @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();

Expand All @@ -209,7 +212,7 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr

// Create missing nested arrays on demand
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)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

namespace Symfony\Component\PropertyAccess\Tests;

use Symfony\Component\PropertyAccess\Exception\ExceptionInterface;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\StringUtil;

class PropertyAccessorCollectionTest_Car
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\PropertyAccess\PropertyAccessor;
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\TestClassMagicGet;

Expand Down
0