This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php

588 lines
22 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess;
use Symfony\Component\PropertyAccess\Exception\AccessException;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
/**
* Default implementation of {@link PropertyAccessorInterface}.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class PropertyAccessor implements PropertyAccessorInterface
{
const VALUE = 0;
const IS_REF = 1;
/**
2014-04-16 11:34:42 +01:00
* @var bool
*/
private $magicCall;
/**
2014-04-16 11:34:42 +01:00
* @var bool
*/
private $ignoreInvalidIndices;
/**
* Should not be used by application code. Use
2013-08-24 23:47:14 +01:00
* {@link PropertyAccess::createPropertyAccessor()} instead.
*/
public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false)
{
$this->magicCall = $magicCall;
$this->ignoreInvalidIndices = !$throwExceptionOnInvalidIndex;
}
/**
* {@inheritdoc}
*/
public function getValue($objectOrArray, $propertyPath)
{
if (!$propertyPath instanceof PropertyPathInterface) {
$propertyPath = new PropertyPath($propertyPath);
}
Merge branch '2.3' into 2.4 * 2.3: fixed CS [Process] fixed some volatile tests [HttpKernel] fixed a volatile test [HttpFoundation] fixed some volatile tests Use getPathname() instead of string casting to get BinaryFileReponse file path Conflicts: src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php src/Symfony/Bundle/FrameworkBundle/EventListener/SessionListener.php src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php src/Symfony/Component/ClassLoader/Tests/ApcUniversalClassLoaderTest.php src/Symfony/Component/Config/Definition/ReferenceDumper.php src/Symfony/Component/Config/Tests/Definition/Dumper/YamlReferenceDumperTest.php src/Symfony/Component/Console/Application.php src/Symfony/Component/Console/Tests/ApplicationTest.php src/Symfony/Component/Filesystem/Exception/IOException.php src/Symfony/Component/Form/Extension/Templating/TemplatingExtension.php src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/HttpKernel/Tests/Bundle/BundleTest.php src/Symfony/Component/HttpKernel/Tests/Fragment/RoutableFragmentRendererTest.php src/Symfony/Component/HttpKernel/Tests/Profiler/Mock/RedisMock.php src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php src/Symfony/Component/PropertyAccess/PropertyAccessor.php src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCollectionTest.php src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php src/Symfony/Component/Routing/Tests/Annotation/RouteTest.php src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php src/Symfony/Component/Serializer/Encoder/XmlEncoder.php src/Symfony/Component/Validator/Constraints/CollectionValidator.php src/Symfony/Component/Validator/Tests/ExecutionContextTest.php
2014-09-22 09:51:05 +01:00
$propertyValues = & $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
return $propertyValues[count($propertyValues) - 1][self::VALUE];
}
/**
* {@inheritdoc}
*/
public function setValue(&$objectOrArray, $propertyPath, $value)
{
if (!$propertyPath instanceof PropertyPathInterface) {
$propertyPath = new PropertyPath($propertyPath);
}
2014-09-21 19:53:12 +01:00
$propertyValues = & $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength() - 1);
// Add the root object to the list
array_unshift($propertyValues, array(
self::VALUE => &$objectOrArray,
self::IS_REF => true,
));
for ($i = count($propertyValues) - 1; $i >= 0; --$i) {
2014-09-21 19:53:12 +01:00
$objectOrArray = & $propertyValues[$i][self::VALUE];
$property = $propertyPath->getElement($i);
if ($propertyPath->isIndex($i)) {
$this->writeIndex($objectOrArray, $property, $value);
} else {
$this->writeProperty($objectOrArray, $property, $value);
}
if ($propertyValues[$i][self::IS_REF]) {
return;
}
2014-09-21 19:53:12 +01:00
$value = & $objectOrArray;
}
}
/**
* {@inheritdoc}
*/
public function isReadable($objectOrArray, $propertyPath)
{
if (!$propertyPath instanceof PropertyPathInterface) {
$propertyPath = new PropertyPath($propertyPath);
}
try {
$this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
return true;
} catch (AccessException $e) {
return false;
} catch (UnexpectedTypeException $e) {
return false;
}
}
/**
* {@inheritdoc}
*/
public function isWritable($objectOrArray, $propertyPath)
{
if (!$propertyPath instanceof PropertyPathInterface) {
$propertyPath = new PropertyPath($propertyPath);
}
try {
$propertyValues = $this->readPropertiesUntil($objectOrArray, $propertyPath, $propertyPath->getLength() - 1);
// Add the root object to the list
array_unshift($propertyValues, array(
self::VALUE => $objectOrArray,
self::IS_REF => true,
));
for ($i = count($propertyValues) - 1; $i >= 0; --$i) {
$objectOrArray = $propertyValues[$i][self::VALUE];
$property = $propertyPath->getElement($i);
if ($propertyPath->isIndex($i)) {
if (!$objectOrArray instanceof \ArrayAccess && !is_array($objectOrArray)) {
return false;
}
} else {
if (!$this->isPropertyWritable($objectOrArray, $property)) {
return false;
}
}
if ($propertyValues[$i][self::IS_REF]) {
return true;
}
}
return true;
} catch (AccessException $e) {
return false;
} catch (UnexpectedTypeException $e) {
return false;
}
}
/**
* 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 int $lastIndex The index up to which should be read
* @param bool $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.
2014-12-04 20:26:11 +00:00
* @throws NoSuchIndexException If a non-existing index is accessed
*/
private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $propertyPath, $lastIndex, $ignoreInvalidIndices = true)
{
if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
throw new UnexpectedTypeException($objectOrArray, 'object or array');
}
$propertyValues = array();
for ($i = 0; $i < $lastIndex; ++$i) {
$property = $propertyPath->getElement($i);
$isIndex = $propertyPath->isIndex($i);
// Create missing nested arrays on demand
if ($isIndex &&
(
($objectOrArray instanceof \ArrayAccess && !isset($objectOrArray[$property])) ||
(is_array($objectOrArray) && !array_key_exists($property, $objectOrArray))
)
) {
if (!$ignoreInvalidIndices) {
if (!is_array($objectOrArray)) {
if (!$objectOrArray instanceof \Traversable) {
throw new NoSuchIndexException(sprintf(
2015-02-21 15:36:02 +00:00
'Cannot read index "%s" while trying to traverse path "%s".',
$property,
(string) $propertyPath
));
}
$objectOrArray = iterator_to_array($objectOrArray);
}
throw new NoSuchIndexException(sprintf(
2015-02-21 15:36:02 +00:00
'Cannot read index "%s" while trying to traverse path "%s". Available indices are "%s".',
$property,
2015-02-21 15:36:02 +00:00
(string) $propertyPath,
print_r(array_keys($objectOrArray), true)
));
}
$objectOrArray[$property] = $i + 1 < $propertyPath->getLength() ? array() : null;
}
if ($isIndex) {
2014-09-21 19:53:12 +01:00
$propertyValue = & $this->readIndex($objectOrArray, $property);
} else {
2014-09-21 19:53:12 +01:00
$propertyValue = & $this->readProperty($objectOrArray, $property);
}
2014-09-21 19:53:12 +01:00
$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');
}
2014-09-21 19:53:12 +01:00
$propertyValues[] = & $propertyValue;
}
return $propertyValues;
}
/**
* Reads a key from an array-like structure.
*
* @param \ArrayAccess|array $array The array or \ArrayAccess object to read from
2014-04-16 07:51:57 +01:00
* @param string|int $index The key to read
*
* @return mixed The value of the key
*
* @throws NoSuchIndexException If the array does not implement \ArrayAccess or it is not an array
*/
private function &readIndex(&$array, $index)
{
if (!$array instanceof \ArrayAccess && !is_array($array)) {
2015-02-21 15:36:02 +00:00
throw new NoSuchIndexException(sprintf('Cannot read index "%s" from object of type "%s" because it doesn\'t implement \ArrayAccess.', $index, get_class($array)));
}
// Use an array instead of an object since performance is very crucial here
$result = array(
self::VALUE => null,
2014-09-21 19:53:12 +01:00
self::IS_REF => false,
);
if (isset($array[$index])) {
if (is_array($array)) {
2014-09-21 19:53:12 +01:00
$result[self::VALUE] = & $array[$index];
$result[self::IS_REF] = true;
} else {
$result[self::VALUE] = $array[$index];
// Objects are always passed around by reference
$result[self::IS_REF] = is_object($array[$index]) ? true : false;
}
}
return $result;
}
/**
* Reads the a property from an object or array.
*
* @param object $object The object to read from.
* @param string $property The property to read.
*
* @return mixed The value of the read property
*
* @throws NoSuchPropertyException If the property does not exist or is not
* public.
*/
private function &readProperty(&$object, $property)
{
// Use an array instead of an object since performance is
// very crucial here
$result = array(
self::VALUE => null,
2014-09-21 19:53:12 +01:00
self::IS_REF => false,
);
if (!is_object($object)) {
2015-02-21 15:36:02 +00:00
throw new NoSuchPropertyException(sprintf('Cannot read property "%s" from an array. Maybe you intended to write the property path as "[%s]" instead.', $property, $property));
}
2014-10-22 18:11:55 +01:00
$camelized = $this->camelize($property);
$reflClass = new \ReflectionClass($object);
2014-10-22 18:11:55 +01:00
$getter = 'get'.$camelized;
$getsetter = lcfirst($camelized); // jQuery style, e.g. read: last(), write: last($item)
$isser = 'is'.$camelized;
$hasser = 'has'.$camelized;
$classHasProperty = $reflClass->hasProperty($property);
if ($reflClass->hasMethod($getter) && $reflClass->getMethod($getter)->isPublic()) {
$result[self::VALUE] = $object->$getter();
2014-10-22 18:11:55 +01:00
} elseif ($this->isMethodAccessible($reflClass, $getsetter, 0)) {
$result[self::VALUE] = $object->$getsetter();
} elseif ($reflClass->hasMethod($isser) && $reflClass->getMethod($isser)->isPublic()) {
$result[self::VALUE] = $object->$isser();
} elseif ($reflClass->hasMethod($hasser) && $reflClass->getMethod($hasser)->isPublic()) {
$result[self::VALUE] = $object->$hasser();
} elseif ($reflClass->hasMethod('__get') && $reflClass->getMethod('__get')->isPublic()) {
$result[self::VALUE] = $object->$property;
} elseif ($classHasProperty && $reflClass->getProperty($property)->isPublic()) {
2014-09-21 19:53:12 +01:00
$result[self::VALUE] = & $object->$property;
$result[self::IS_REF] = true;
} elseif (!$classHasProperty && property_exists($object, $property)) {
// Needed to support \stdClass instances. We need to explicitly
// exclude $classHasProperty, otherwise if in the previous clause
// a *protected* property was found on the class, property_exists()
// returns true, consequently the following line will result in a
// fatal error.
2014-09-21 19:53:12 +01:00
$result[self::VALUE] = & $object->$property;
$result[self::IS_REF] = true;
} elseif ($this->magicCall && $reflClass->hasMethod('__call') && $reflClass->getMethod('__call')->isPublic()) {
// we call the getter and hope the __call do the job
$result[self::VALUE] = $object->$getter();
} else {
2014-10-22 18:11:55 +01:00
$methods = array($getter, $getsetter, $isser, $hasser, '__get');
if ($this->magicCall) {
$methods[] = '__call';
}
throw new NoSuchPropertyException(sprintf(
'Neither the property "%s" nor one of the methods "%s()" '.
'exist and have public access in class "%s".',
$property,
implode('()", "', $methods),
$reflClass->name
));
}
// Objects are always passed around by reference
if (is_object($result[self::VALUE])) {
$result[self::IS_REF] = true;
}
return $result;
}
/**
* Sets the value of an index in a given array-accessible value.
*
* @param \ArrayAccess|array $array An array or \ArrayAccess object to write to
2014-04-16 07:51:57 +01:00
* @param string|int $index The index to write at
* @param mixed $value The value to write
*
* @throws NoSuchIndexException If the array does not implement \ArrayAccess or it is not an array
*/
private function writeIndex(&$array, $index, $value)
{
if (!$array instanceof \ArrayAccess && !is_array($array)) {
2015-02-21 15:36:02 +00:00
throw new NoSuchIndexException(sprintf('Cannot modify index "%s" in object of type "%s" because it doesn\'t implement \ArrayAccess', $index, get_class($array)));
}
$array[$index] = $value;
}
/**
Merge branch '2.3' into 2.5 * 2.3: [2.3] CS And DocBlock Fixes [2.3] CS Fixes Conflicts: src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php src/Symfony/Component/Config/Definition/ReferenceDumper.php src/Symfony/Component/Console/Application.php src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php src/Symfony/Component/Filesystem/Tests/FilesystemTest.php src/Symfony/Component/Form/Extension/Csrf/EventListener/CsrfValidationListener.php src/Symfony/Component/Form/FormError.php src/Symfony/Component/HttpFoundation/Request.php src/Symfony/Component/HttpFoundation/Response.php src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php src/Symfony/Component/Process/ProcessUtils.php src/Symfony/Component/PropertyAccess/PropertyAccessor.php src/Symfony/Component/PropertyAccess/PropertyAccessorInterface.php src/Symfony/Component/Serializer/Encoder/XmlEncoder.php src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php src/Symfony/Component/Validator/Constraints/GroupSequence.php src/Symfony/Component/Validator/Mapping/ClassMetadata.php src/Symfony/Component/Validator/Mapping/ClassMetadataFactory.php src/Symfony/Component/Validator/Mapping/MemberMetadata.php src/Symfony/Component/Validator/Tests/Fixtures/StubGlobalExecutionContext.php
2014-12-22 16:29:52 +00:00
* Sets the value of a property in the given object.
*
* @param object $object The object to write to
* @param string $property The property to write
* @param mixed $value The value to write
*
* @throws NoSuchPropertyException If the property does not exist or is not
* public.
*/
private function writeProperty(&$object, $property, $value)
{
if (!is_object($object)) {
throw new NoSuchPropertyException(sprintf('Cannot write property "%s" to an array. Maybe you should write the property path as "[%s]" instead?', $property, $property));
}
$reflClass = new \ReflectionClass($object);
2014-10-22 18:11:55 +01:00
$camelized = $this->camelize($property);
$singulars = (array) StringUtil::singularify($camelized);
if (is_array($value) || $value instanceof \Traversable) {
$methods = $this->findAdderAndRemover($reflClass, $singulars);
// Use addXxx() and removeXxx() to write the collection
if (null !== $methods) {
$this->writeCollection($object, $property, $value, $methods[0], $methods[1]);
return;
}
}
2014-10-22 18:11:55 +01:00
$setter = 'set'.$camelized;
$getsetter = lcfirst($camelized); // jQuery style, e.g. read: last(), write: last($item)
$classHasProperty = $reflClass->hasProperty($property);
if ($this->isMethodAccessible($reflClass, $setter, 1)) {
$object->$setter($value);
2014-10-22 18:11:55 +01:00
} elseif ($this->isMethodAccessible($reflClass, $getsetter, 1)) {
$object->$getsetter($value);
} elseif ($this->isMethodAccessible($reflClass, '__set', 2)) {
$object->$property = $value;
} elseif ($classHasProperty && $reflClass->getProperty($property)->isPublic()) {
$object->$property = $value;
} elseif (!$classHasProperty && property_exists($object, $property)) {
// Needed to support \stdClass instances. We need to explicitly
// exclude $classHasProperty, otherwise if in the previous clause
// a *protected* property was found on the class, property_exists()
// returns true, consequently the following line will result in a
// fatal error.
$object->$property = $value;
} elseif ($this->magicCall && $this->isMethodAccessible($reflClass, '__call', 2)) {
// we call the getter and hope the __call do the job
$object->$setter($value);
} else {
throw new NoSuchPropertyException(sprintf(
2014-05-09 16:28:08 +01:00
'Neither the property "%s" nor one of the methods %s"%s()", "%s()", '.
'"__set()" or "__call()" exist and have public access in class "%s".',
$property,
implode('', array_map(function ($singular) {
return '"add'.$singular.'()"/"remove'.$singular.'()", ';
}, $singulars)),
$setter,
2014-10-22 18:11:55 +01:00
$getsetter,
$reflClass->name
));
}
}
/**
* Adjusts a collection-valued property by calling add*() and remove*()
* methods.
*
* @param object $object The object to write to
* @param string $property The property to write
* @param array|\Traversable $collection The collection to write
* @param string $addMethod The add*() method
* @param string $removeMethod The remove*() method
*/
private function writeCollection($object, $property, $collection, $addMethod, $removeMethod)
{
// At this point the add and remove methods have been found
// Use iterator_to_array() instead of clone in order to prevent side effects
// see https://github.com/symfony/symfony/issues/4670
$itemsToAdd = is_object($collection) ? iterator_to_array($collection) : $collection;
$itemToRemove = array();
$propertyValue = $this->readProperty($object, $property);
$previousValue = $propertyValue[self::VALUE];
if (is_array($previousValue) || $previousValue instanceof \Traversable) {
foreach ($previousValue as $previousItem) {
foreach ($collection as $key => $item) {
if ($item === $previousItem) {
// Item found, don't add
unset($itemsToAdd[$key]);
// Next $previousItem
continue 2;
}
}
// Item not found, add to remove list
$itemToRemove[] = $previousItem;
}
}
foreach ($itemToRemove as $item) {
2014-11-21 09:14:57 +00:00
$object->{$removeMethod}($item);
}
foreach ($itemsToAdd as $item) {
2014-11-21 09:14:57 +00:00
$object->{$addMethod}($item);
}
}
/**
* Returns whether a property is writable in the given object.
*
* @param object $object The object to write to
* @param string $property The property to write
*
2014-12-04 20:26:11 +00:00
* @return bool Whether the property is writable
*/
private function isPropertyWritable($object, $property)
{
if (!is_object($object)) {
return false;
}
$reflClass = new \ReflectionClass($object);
2014-10-22 18:11:55 +01:00
$camelized = $this->camelize($property);
$setter = 'set'.$camelized;
$getsetter = lcfirst($camelized); // jQuery style, e.g. read: last(), write: last($item)
$classHasProperty = $reflClass->hasProperty($property);
if ($this->isMethodAccessible($reflClass, $setter, 1)
2014-10-22 18:11:55 +01:00
|| $this->isMethodAccessible($reflClass, $getsetter, 1)
|| $this->isMethodAccessible($reflClass, '__set', 2)
|| ($classHasProperty && $reflClass->getProperty($property)->isPublic())
|| (!$classHasProperty && property_exists($object, $property))
|| ($this->magicCall && $this->isMethodAccessible($reflClass, '__call', 2))) {
return true;
}
2014-10-22 18:11:55 +01:00
$singulars = (array) StringUtil::singularify($camelized);
// Any of the two methods is required, but not yet known
if (null !== $this->findAdderAndRemover($reflClass, $singulars)) {
return true;
}
return false;
}
/**
* Camelizes a given string.
*
2014-11-30 13:33:44 +00:00
* @param string $string Some string
*
* @return string The camelized version of the string
*/
private function camelize($string)
{
2014-10-22 18:11:55 +01:00
return strtr(ucwords(strtr($string, array('_' => ' '))), array(' ' => ''));
}
/**
* Searches for add and remove methods.
*
* @param \ReflectionClass $reflClass The reflection class for the given object
* @param array $singulars The singular form of the property name or null
*
* @return array|null An array containing the adder and remover when found, null otherwise
*/
private function findAdderAndRemover(\ReflectionClass $reflClass, array $singulars)
{
foreach ($singulars as $singular) {
$addMethod = 'add'.$singular;
$removeMethod = 'remove'.$singular;
$addMethodFound = $this->isMethodAccessible($reflClass, $addMethod, 1);
$removeMethodFound = $this->isMethodAccessible($reflClass, $removeMethod, 1);
if ($addMethodFound && $removeMethodFound) {
return array($addMethod, $removeMethod);
}
}
}
/**
* Returns whether a method is public and has the number of required parameters.
*
2014-11-30 13:33:44 +00:00
* @param \ReflectionClass $class The class of the method
* @param string $methodName The method name
* @param int $parameters The number of parameters
*
2014-11-30 13:33:44 +00:00
* @return bool Whether the method is public and has $parameters
* required parameters
*/
private function isMethodAccessible(\ReflectionClass $class, $methodName, $parameters)
{
if ($class->hasMethod($methodName)) {
$method = $class->getMethod($methodName);
if ($method->isPublic()
&& $method->getNumberOfRequiredParameters() <= $parameters
&& $method->getNumberOfParameters() >= $parameters) {
return true;
}
}
return false;
}
}