2013-01-07 08:19:31 +00:00
< ? 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\NoSuchPropertyException ;
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 ;
2015-10-20 09:40:31 +01:00
const ACCESS_HAS_PROPERTY = 0 ;
const ACCESS_TYPE = 1 ;
const ACCESS_NAME = 2 ;
const ACCESS_REF = 3 ;
const ACCESS_ADDER = 4 ;
const ACCESS_REMOVER = 5 ;
const ACCESS_TYPE_METHOD = 0 ;
const ACCESS_TYPE_PROPERTY = 1 ;
const ACCESS_TYPE_MAGIC = 2 ;
const ACCESS_TYPE_ADDER_AND_REMOVER = 3 ;
const ACCESS_TYPE_NOT_FOUND = 4 ;
2013-01-07 08:19:31 +00:00
2013-03-04 22:59:45 +00:00
private $magicCall ;
2015-10-20 09:40:31 +01:00
private $readPropertyCache = array ();
private $writePropertyCache = array ();
2013-03-04 22:59:45 +00:00
2013-01-07 08:19:31 +00:00
/**
* Should not be used by application code . Use
2015-01-02 16:09:32 +00:00
* { @ link PropertyAccess :: createPropertyAccessor ()} instead .
2013-01-07 08:19:31 +00:00
*/
2013-03-04 22:59:45 +00:00
public function __construct ( $magicCall = false )
2013-01-07 08:19:31 +00:00
{
2013-03-04 22:59:45 +00:00
$this -> magicCall = $magicCall ;
2013-01-07 08:19:31 +00:00
}
/**
* { @ inheritdoc }
*/
public function getValue ( $objectOrArray , $propertyPath )
{
2015-02-19 12:40:39 +00:00
if ( ! $propertyPath instanceof PropertyPathInterface ) {
2013-01-07 08:19:31 +00:00
$propertyPath = new PropertyPath ( $propertyPath );
}
2015-03-27 22:06:33 +00:00
$propertyValues = & $this -> readPropertiesUntil ( $objectOrArray , $propertyPath , $propertyPath -> getLength ());
2013-01-07 08:19:31 +00:00
return $propertyValues [ count ( $propertyValues ) - 1 ][ self :: VALUE ];
}
/**
* { @ inheritdoc }
*/
public function setValue ( & $objectOrArray , $propertyPath , $value )
{
2015-02-19 12:40:39 +00:00
if ( ! $propertyPath instanceof PropertyPathInterface ) {
2013-01-07 08:19:31 +00:00
$propertyPath = new PropertyPath ( $propertyPath );
}
2015-03-27 22:06:33 +00:00
$propertyValues = & $this -> readPropertiesUntil ( $objectOrArray , $propertyPath , $propertyPath -> getLength () - 1 );
2013-01-07 08:19:31 +00:00
$overwrite = true ;
// 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 ) {
2015-03-27 22:06:33 +00:00
$objectOrArray = & $propertyValues [ $i ][ self :: VALUE ];
2013-01-07 08:19:31 +00:00
if ( $overwrite ) {
$property = $propertyPath -> getElement ( $i );
//$singular = $propertyPath->singulars[$i];
$singular = null ;
2012-07-31 12:15:57 +01:00
if ( $propertyPath -> isIndex ( $i )) {
$this -> writeIndex ( $objectOrArray , $property , $value );
} else {
$this -> writeProperty ( $objectOrArray , $property , $singular , $value );
}
2013-01-07 08:19:31 +00:00
}
2015-03-27 22:06:33 +00:00
$value = & $objectOrArray ;
2013-01-07 08:19:31 +00:00
$overwrite = ! $propertyValues [ $i ][ self :: IS_REF ];
}
}
/**
* Reads the path from an object up to a given path index .
*
2013-03-27 22:40:36 +00:00
* @ param object | array $objectOrArray The object or array to read from
* @ param PropertyPathInterface $propertyPath The property path to read
2014-04-12 18:54:57 +01:00
* @ param int $lastIndex The index up to which should be read
2013-01-07 08:19:31 +00:00
*
* @ return array The values read in the path .
*
* @ throws UnexpectedTypeException If a value within the path is neither object nor array .
*/
private function & readPropertiesUntil ( & $objectOrArray , PropertyPathInterface $propertyPath , $lastIndex )
{
2015-02-18 20:52:22 +00:00
if ( ! is_object ( $objectOrArray ) && ! is_array ( $objectOrArray )) {
throw new UnexpectedTypeException ( $objectOrArray , 'object or array' );
}
2013-01-07 08:19:31 +00:00
$propertyValues = array ();
2012-07-31 12:15:57 +01:00
for ( $i = 0 ; $i < $lastIndex ; ++ $i ) {
2013-01-07 08:19:31 +00:00
$property = $propertyPath -> getElement ( $i );
$isIndex = $propertyPath -> isIndex ( $i );
// Create missing nested arrays on demand
2015-01-09 14:53:40 +00:00
if (
$isIndex &&
(
( $objectOrArray instanceof \ArrayAccess && ! isset ( $objectOrArray [ $property ])) ||
( is_array ( $objectOrArray ) && ! array_key_exists ( $property , $objectOrArray ))
)
) {
2015-10-02 22:43:46 +01:00
if ( $i + 1 < $propertyPath -> getLength ()) {
$objectOrArray [ $property ] = array ();
}
2013-01-07 08:19:31 +00:00
}
2012-07-31 12:15:57 +01:00
if ( $isIndex ) {
2015-03-27 22:06:33 +00:00
$propertyValue = & $this -> readIndex ( $objectOrArray , $property );
2012-07-31 12:15:57 +01:00
} else {
2015-03-27 22:06:33 +00:00
$propertyValue = & $this -> readProperty ( $objectOrArray , $property );
2012-07-31 12:15:57 +01:00
}
2015-03-27 22:06:33 +00:00
$objectOrArray = & $propertyValue [ self :: VALUE ];
2013-01-07 08:19:31 +00:00
2015-02-18 20:52:22 +00:00
// 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' );
}
2015-03-27 22:06:33 +00:00
$propertyValues [] = & $propertyValue ;
2013-01-07 08:19:31 +00:00
}
return $propertyValues ;
}
2012-07-31 12:15:57 +01:00
/**
* Reads a key from an array - like structure .
*
2013-03-27 22:40:36 +00:00
* @ 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
2012-07-31 12:15:57 +01:00
*
* @ return mixed The value of the key
*
2013-03-27 22:40:36 +00:00
* @ throws NoSuchPropertyException If the array does not implement \ArrayAccess or it is not an array
2012-07-31 12:15:57 +01:00
*/
private function & readIndex ( & $array , $index )
{
if ( ! $array instanceof \ArrayAccess && ! is_array ( $array )) {
throw new NoSuchPropertyException ( sprintf ( 'Index "%s" cannot be read 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 ,
2012-07-31 12:15:57 +01:00
);
if ( isset ( $array [ $index ])) {
if ( is_array ( $array )) {
2015-03-27 22:06:33 +00:00
$result [ self :: VALUE ] = & $array [ $index ];
2012-07-31 12:15:57 +01:00
$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 ;
}
2013-01-07 08:19:31 +00:00
/**
* Reads the a property from an object or array .
*
2012-07-31 12:15:57 +01:00
* @ param object $object The object to read from .
* @ param string $property The property to read .
2013-01-07 08:19:31 +00:00
*
* @ return mixed The value of the read property
*
2013-04-18 15:39:54 +01:00
* @ throws NoSuchPropertyException If the property does not exist or is not
* public .
2013-01-07 08:19:31 +00:00
*/
2012-07-31 12:15:57 +01:00
private function & readProperty ( & $object , $property )
2013-01-07 08:19:31 +00:00
{
// 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 ,
2013-01-07 08:19:31 +00:00
);
2012-07-31 12:15:57 +01:00
if ( ! is_object ( $object )) {
throw new NoSuchPropertyException ( sprintf ( 'Cannot read property "%s" from an array. Maybe you should write the property path as "[%s]" instead?' , $property , $property ));
}
2013-01-07 08:19:31 +00:00
2015-10-20 09:40:31 +01:00
$access = $this -> getReadAccessInfo ( $object , $property );
if ( self :: ACCESS_TYPE_METHOD === $access [ self :: ACCESS_TYPE ]) {
$result [ self :: VALUE ] = $object -> { $access [ self :: ACCESS_NAME ]}();
} elseif ( self :: ACCESS_TYPE_PROPERTY === $access [ self :: ACCESS_TYPE ]) {
if ( $access [ self :: ACCESS_REF ]) {
$result [ self :: VALUE ] = & $object -> { $access [ self :: ACCESS_NAME ]};
$result [ self :: IS_REF ] = true ;
} else {
$result [ self :: VALUE ] = $object -> { $access [ self :: ACCESS_NAME ]};
}
} elseif ( ! $access [ self :: ACCESS_HAS_PROPERTY ] && property_exists ( $object , $property )) {
2013-04-18 15:39:54 +01:00
// 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.
2015-10-20 09:40:31 +01:00
2015-03-27 22:06:33 +00:00
$result [ self :: VALUE ] = & $object -> $property ;
2012-07-31 12:15:57 +01:00
$result [ self :: IS_REF ] = true ;
2015-10-20 09:40:31 +01:00
} elseif ( self :: ACCESS_TYPE_MAGIC === $access [ self :: ACCESS_TYPE ]) {
2013-03-04 22:59:45 +00:00
// we call the getter and hope the __call do the job
2015-10-20 09:40:31 +01:00
$result [ self :: VALUE ] = $object -> { $access [ self :: ACCESS_NAME ]}();
2013-01-07 08:19:31 +00:00
} else {
2015-10-20 09:40:31 +01:00
throw new NoSuchPropertyException ( $access [ self :: ACCESS_NAME ]);
2013-01-07 08:19:31 +00:00
}
// Objects are always passed around by reference
if ( is_object ( $result [ self :: VALUE ])) {
$result [ self :: IS_REF ] = true ;
}
return $result ;
}
2015-10-20 09:40:31 +01:00
/**
* Guesses how to read the property value .
*
* @ param string $object
* @ param string $property
*
* @ return array
*/
private function getReadAccessInfo ( $object , $property )
{
$key = get_class ( $object ) . '::' . $property ;
if ( isset ( $this -> readPropertyCache [ $key ])) {
$access = $this -> readPropertyCache [ $key ];
} else {
$access = array ();
$reflClass = new \ReflectionClass ( $object );
$access [ self :: ACCESS_HAS_PROPERTY ] = $reflClass -> hasProperty ( $property );
$camelProp = $this -> camelize ( $property );
$getter = 'get' . $camelProp ;
$isser = 'is' . $camelProp ;
$hasser = 'has' . $camelProp ;
$classHasProperty = $reflClass -> hasProperty ( $property );
if ( $reflClass -> hasMethod ( $getter ) && $reflClass -> getMethod ( $getter ) -> isPublic ()) {
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_METHOD ;
$access [ self :: ACCESS_NAME ] = $getter ;
} elseif ( $reflClass -> hasMethod ( $isser ) && $reflClass -> getMethod ( $isser ) -> isPublic ()) {
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_METHOD ;
$access [ self :: ACCESS_NAME ] = $isser ;
} elseif ( $reflClass -> hasMethod ( $hasser ) && $reflClass -> getMethod ( $hasser ) -> isPublic ()) {
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_METHOD ;
$access [ self :: ACCESS_NAME ] = $hasser ;
} elseif ( $reflClass -> hasMethod ( '__get' ) && $reflClass -> getMethod ( '__get' ) -> isPublic ()) {
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_PROPERTY ;
$access [ self :: ACCESS_NAME ] = $property ;
$access [ self :: ACCESS_REF ] = false ;
} elseif ( $classHasProperty && $reflClass -> getProperty ( $property ) -> isPublic ()) {
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_PROPERTY ;
$access [ self :: ACCESS_NAME ] = $property ;
$access [ self :: ACCESS_REF ] = true ;
$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
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_MAGIC ;
$access [ self :: ACCESS_NAME ] = $getter ;
} else {
$methods = array ( $getter , $isser , $hasser , '__get' );
if ( $this -> magicCall ) {
$methods [] = '__call' ;
}
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_NOT_FOUND ;
$access [ self :: ACCESS_NAME ] = sprintf (
'Neither the property "%s" nor one of the methods "%s()" ' .
'exist and have public access in class "%s".' ,
$property ,
implode ( '()", "' , $methods ),
$reflClass -> name
);
}
$this -> readPropertyCache [ $key ] = $access ;
}
return $access ;
}
2013-01-07 08:19:31 +00:00
/**
2014-12-21 17:00:50 +00:00
* Sets the value of the property at the given index in the path .
2013-01-07 08:19:31 +00:00
*
2013-03-27 22:40:36 +00:00
* @ 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
2013-03-27 22:40:36 +00:00
* @ param mixed $value The value to write
2012-07-31 12:15:57 +01:00
*
2013-03-27 22:40:36 +00:00
* @ throws NoSuchPropertyException If the array does not implement \ArrayAccess or it is not an array
2012-07-31 12:15:57 +01:00
*/
private function writeIndex ( & $array , $index , $value )
{
if ( ! $array instanceof \ArrayAccess && ! is_array ( $array )) {
throw new NoSuchPropertyException ( sprintf ( 'Index "%s" cannot be modified in object of type "%s" because it doesn\'t implement \ArrayAccess' , $index , get_class ( $array )));
}
$array [ $index ] = $value ;
}
/**
2014-12-21 17:00:50 +00:00
* Sets the value of the property at the given index in the path .
2012-07-31 12:15:57 +01:00
*
2013-04-18 15:39:54 +01:00
* @ param object | array $object The object or array to write to
* @ param string $property The property to write
* @ param string | null $singular The singular form of the property name or null
* @ param mixed $value The value to write
2013-01-07 08:19:31 +00:00
*
2013-04-18 15:39:54 +01:00
* @ throws NoSuchPropertyException If the property does not exist or is not
* public .
2013-01-07 08:19:31 +00:00
*/
2012-07-31 12:15:57 +01:00
private function writeProperty ( & $object , $property , $singular , $value )
2013-01-07 08:19:31 +00:00
{
2012-07-31 12:15:57 +01:00
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 ));
}
2013-01-07 08:19:31 +00:00
2015-10-20 09:40:31 +01:00
$access = $this -> getWriteAccessInfo ( $object , $property , $singular , $value );
if ( self :: ACCESS_TYPE_METHOD === $access [ self :: ACCESS_TYPE ]) {
$object -> { $access [ self :: ACCESS_NAME ]}( $value );
} elseif ( self :: ACCESS_TYPE_PROPERTY === $access [ self :: ACCESS_TYPE ]) {
$object -> { $access [ self :: ACCESS_NAME ]} = $value ;
} elseif ( self :: ACCESS_TYPE_ADDER_AND_REMOVER === $access [ self :: ACCESS_TYPE ]) {
// 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 ( $value ) ? iterator_to_array ( $value ) : $value ;
$itemToRemove = array ();
$propertyValue = & $this -> readProperty ( $object , $property );
$previousValue = $propertyValue [ self :: VALUE ];
// remove reference to avoid modifications
unset ( $propertyValue );
if ( is_array ( $previousValue ) || $previousValue instanceof \Traversable ) {
foreach ( $previousValue as $previousItem ) {
foreach ( $value as $key => $item ) {
if ( $item === $previousItem ) {
// Item found, don't add
unset ( $itemsToAdd [ $key ]);
// Next $previousItem
continue 2 ;
2013-01-07 08:19:31 +00:00
}
}
2015-10-20 09:40:31 +01:00
// Item not found, add to remove list
$itemToRemove [] = $previousItem ;
2012-07-31 12:15:57 +01:00
}
2013-01-07 08:19:31 +00:00
}
2015-10-20 09:40:31 +01:00
foreach ( $itemToRemove as $item ) {
call_user_func ( array ( $object , $access [ self :: ACCESS_REMOVER ]), $item );
}
2013-01-07 08:19:31 +00:00
2015-10-20 09:40:31 +01:00
foreach ( $itemsToAdd as $item ) {
call_user_func ( array ( $object , $access [ self :: ACCESS_ADDER ]), $item );
}
} elseif ( ! $access [ self :: ACCESS_HAS_PROPERTY ] && property_exists ( $object , $property )) {
2013-04-18 15:39:54 +01:00
// 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.
2015-10-20 09:40:31 +01:00
$object -> { $access [ self :: ACCESS_NAME ]} = $value ;
} elseif ( self :: ACCESS_TYPE_MAGIC === $access [ self :: ACCESS_TYPE ]) {
$object -> { $access [ self :: ACCESS_NAME ]}( $value );
2013-01-07 08:19:31 +00:00
} else {
2015-10-20 09:40:31 +01:00
throw new NoSuchPropertyException ( $access [ self :: ACCESS_NAME ]);
2013-01-07 08:19:31 +00:00
}
}
2015-10-20 09:40:31 +01:00
/**
* Guesses how to write the property value .
*
* @ param string $object
* @ param string $property
* @ param string | null $singular
* @ param mixed $value
*
* @ return array
*/
private function getWriteAccessInfo ( $object , $property , $singular , $value )
{
$key = get_class ( $object ) . '::' . $property ;
$guessedAdders = '' ;
if ( isset ( $this -> writePropertyCache [ $key ])) {
$access = $this -> writePropertyCache [ $key ];
} else {
$access = array ();
$reflClass = new \ReflectionClass ( $object );
$access [ self :: ACCESS_HAS_PROPERTY ] = $reflClass -> hasProperty ( $property );
$plural = $this -> camelize ( $property );
// Any of the two methods is required, but not yet known
$singulars = null !== $singular ? array ( $singular ) : ( array ) StringUtil :: singularify ( $plural );
if ( is_array ( $value ) || $value instanceof \Traversable ) {
$methods = $this -> findAdderAndRemover ( $reflClass , $singulars );
if ( null === $methods ) {
// It is sufficient to include only the adders in the error
// message. If the user implements the adder but not the remover,
// an exception will be thrown in findAdderAndRemover() that
// the remover has to be implemented as well.
$guessedAdders = '"add' . implode ( '()", "add' , $singulars ) . '()", ' ;
} else {
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_ADDER_AND_REMOVER ;
$access [ self :: ACCESS_ADDER ] = $methods [ 0 ];
$access [ self :: ACCESS_REMOVER ] = $methods [ 1 ];
}
}
if ( ! isset ( $access [ self :: ACCESS_TYPE ])) {
$setter = 'set' . $this -> camelize ( $property );
$classHasProperty = $reflClass -> hasProperty ( $property );
if ( $reflClass -> hasMethod ( $setter ) && $reflClass -> getMethod ( $setter ) -> isPublic ()) {
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_METHOD ;
$access [ self :: ACCESS_NAME ] = $setter ;
} elseif ( $reflClass -> hasMethod ( '__set' ) && $reflClass -> getMethod ( '__set' ) -> isPublic ()) {
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_PROPERTY ;
$access [ self :: ACCESS_NAME ] = $property ;
} elseif ( $classHasProperty && $reflClass -> getProperty ( $property ) -> isPublic ()) {
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_PROPERTY ;
$access [ self :: ACCESS_NAME ] = $property ;
} elseif ( $this -> magicCall && $reflClass -> hasMethod ( '__call' ) && $reflClass -> getMethod ( '__call' ) -> isPublic ()) {
// we call the getter and hope the __call do the job
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_MAGIC ;
$access [ self :: ACCESS_NAME ] = $setter ;
} else {
$access [ self :: ACCESS_TYPE ] = self :: ACCESS_TYPE_NOT_FOUND ;
$access [ self :: ACCESS_NAME ] = sprintf (
'Neither the property "%s" nor one of the methods %s"%s()", ' .
'"__set()" or "__call()" exist and have public access in class "%s".' ,
$property ,
$guessedAdders ,
$setter ,
$reflClass -> name
);
}
}
$this -> writePropertyCache [ $key ] = $access ;
}
return $access ;
}
2013-01-07 08:19:31 +00:00
/**
* Camelizes a given string .
*
2014-11-30 13:33:44 +00:00
* @ param string $string Some string
2013-01-07 08:19:31 +00:00
*
2013-03-27 22:40:36 +00:00
* @ return string The camelized version of the string
2013-01-07 08:19:31 +00:00
*/
private function camelize ( $string )
{
return preg_replace_callback ( '/(^|_|\.)+(.)/' , function ( $match ) { return ( '.' === $match [ 1 ] ? '_' : '' ) . strtoupper ( $match [ 2 ]); }, $string );
}
/**
* Searches for add and remove methods .
*
* @ param \ReflectionClass $reflClass The reflection class for the given object
2013-03-27 22:40:36 +00:00
* @ param array $singulars The singular form of the property name or null
2013-01-07 08:19:31 +00:00
*
2013-03-27 22:40:36 +00:00
* @ return array | null An array containing the adder and remover when found , null otherwise
2013-01-07 08:19:31 +00:00
*
2013-03-27 22:40:36 +00:00
* @ throws NoSuchPropertyException If the property does not exist
2013-01-07 08:19:31 +00:00
*/
private function findAdderAndRemover ( \ReflectionClass $reflClass , array $singulars )
{
foreach ( $singulars as $singular ) {
2013-04-02 10:39:57 +01:00
$addMethod = 'add' . $singular ;
$removeMethod = 'remove' . $singular ;
2013-01-07 08:19:31 +00:00
$addMethodFound = $this -> isAccessible ( $reflClass , $addMethod , 1 );
$removeMethodFound = $this -> isAccessible ( $reflClass , $removeMethod , 1 );
if ( $addMethodFound && $removeMethodFound ) {
return array ( $addMethod , $removeMethod );
}
if ( $addMethodFound xor $removeMethodFound ) {
throw new NoSuchPropertyException ( sprintf (
2013-04-18 15:39:54 +01:00
'Found the public method "%s()", but did not find a public "%s()" on class %s' ,
2013-01-07 08:19:31 +00:00
$addMethodFound ? $addMethod : $removeMethod ,
$addMethodFound ? $removeMethod : $addMethod ,
$reflClass -> name
));
}
}
}
/**
* Returns whether a method is public and has a specific 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
2013-01-07 08:19:31 +00:00
*
2014-11-30 13:33:44 +00:00
* @ return bool Whether the method is public and has $parameters
* required parameters
2013-01-07 08:19:31 +00:00
*/
private function isAccessible ( \ReflectionClass $class , $methodName , $parameters )
{
if ( $class -> hasMethod ( $methodName )) {
$method = $class -> getMethod ( $methodName );
if ( $method -> isPublic () && $method -> getNumberOfRequiredParameters () === $parameters ) {
return true ;
}
}
return false ;
}
}