TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
ObjectAccess Class Reference

Static Public Member Functions

static getProperty ($subject, $propertyName, $forceDirectAccess=false)
 
static getPropertyInternal ($subject, $propertyName, $forceDirectAccess=false)
 
static getPropertyPath ($subject, $propertyPath)
 
static setProperty (&$subject, $propertyName, $propertyValue, $forceDirectAccess=false)
 
static getGettablePropertyNames ($object)
 
static getSettablePropertyNames ($object)
 
static isPropertySettable ($object, $propertyName)
 
static isPropertyGettable ($object, $propertyName)
 
static getGettableProperties ($object)
 
static buildSetterMethodName ($propertyName)
 

Public Attributes

const ACCESS_GET = 0
 
const ACCESS_SET = 1
 
const ACCESS_PUBLIC = 2
 

Detailed Description

Provides methods to call appropriate getter/setter on an object given the property name. It does this following these rules:

  • if the target object is an instance of ArrayAccess, it gets/sets the property
  • if public getter/setter method exists, call it.
  • if public property exists, return/set the value of it.
  • else, throw exception

Definition at line 27 of file ObjectAccess.php.

Member Function Documentation

static buildSetterMethodName (   $propertyName)
static

Build the setter method name for a given property by capitalizing the first letter of the property, and prepending it with "set".

Parameters
string$propertyNameName of the property
Returns
string Name of the setter method name

Definition at line 370 of file ObjectAccess.php.

static getGettableProperties (   $object)
static

Get all properties (names and their current values) of the current $object that are accessible through this class.

Parameters
object$objectObject to get all properties from.
Exceptions
InvalidArgumentException
Returns
array Associative array of all properties.
Todo:
What to do with ArrayAccess

Definition at line 350 of file ObjectAccess.php.

Referenced by ObjectAccessTest\getGettablePropertiesReturnsPropertiesOfStdClass(), ObjectAccessTest\getGettablePropertiesReturnsTheCorrectValuesForAllProperties(), AbstractController\getStructure(), BlogController\getStructure(), and CObjectViewHelper\render().

static getGettablePropertyNames (   $object)
static

Returns an array of properties which can be get with the getProperty() method. Includes the following properties:

  • which can be get through a public getter method.
  • public properties which can be directly get.
Parameters
object$objectObject to receive property names for
Exceptions
InvalidArgumentException
Returns
array Array of all gettable property names

Definition at line 223 of file ObjectAccess.php.

Referenced by ObjectAccessTest\getGettablePropertyNamesReturnsAllPropertiesWhichAreAvailable(), and JsonView\transformObject().

static getProperty (   $subject,
  $propertyName,
  $forceDirectAccess = false 
)
static

Get a property of a given object. Tries to get the property the following ways:

  • if the target is an array, and has this property, we call it.
  • if super cow powers should be used, fetch value through reflection
  • if public getter method exists, call it.
  • if the target object is an instance of ArrayAccess, it gets the property on it if it exists.
  • if public property exists, return the value of it.
  • else, throw exception
Parameters
mixed$subjectObject or array to get the property from
string$propertyNamename of the property to retrieve
bool$forceDirectAccessdirectly access property using reflection(!)
Exceptions
InvalidArgumentException in case $subject was not an object or $propertyName was not a string
Returns
mixed Value of the property

Definition at line 53 of file ObjectAccess.php.

Referenced by AutocompleteController\autocompleteAction(), Backend\determineStoragePageIdForNewRecord(), ObjectAccessTest\getPropertyCanAccessPropertiesOfAnArray(), ObjectAccessTest\getPropertyCanAccessPropertiesOfAnArrayObject(), ObjectAccessTest\getPropertyCanAccessPropertiesOfAnObjectImplementingArrayAccess(), ObjectAccessTest\getPropertyCanAccessPropertiesOfAnObjectStorageObject(), ObjectAccessTest\getPropertyCanAccessPropertiesOfArrayAccessWithGetterMethodWhenOffsetNotExists(), ObjectAccessTest\getPropertyCanAccessPropertiesOfArrayAccessWithPriorityForOffsetIfOffsetExists(), ObjectAccessTest\getPropertyReturnsExpectedValueForGetterProperty(), ObjectAccessTest\getPropertyReturnsExpectedValueForPublicProperty(), ObjectAccessTest\getPropertyReturnsExpectedValueForUnexposedPropertyIfForceDirectAccessIsTrue(), ObjectAccessTest\getPropertyReturnsExpectedValueForUnknownPropertyIfForceDirectAccessIsTrue(), ObjectAccessTest\getPropertyReturnsNullIfArrayKeyDoesNotExist(), ObjectAccessTest\getPropertyThrowsExceptionIfPropertyDoesNotExist(), ObjectAccessTest\getPropertyThrowsExceptionIfThePropertyNameIsNotAString(), ObjectAccessTest\getPropertyThrowsPropertyNotAccessibleExceptionForNotExistingPropertyIfForceDirectAccessIsTrue(), ObjectAccessTest\getPropertyTriesToCallABooleanGetterMethodIfItExists(), GenericObjectValidator\getPropertyValue(), and JsonView\transformObject().

static getPropertyInternal (   $subject,
  $propertyName,
  $forceDirectAccess = false 
)
static

Gets a property of a given object or array. This is an internal method that does only limited type checking for performance reasons. If you can't make sure that $subject is either of type array or object and $propertyName of type string you should use getProperty() instead.

See also
getProperty()
Parameters
mixed$subjectObject or array to get the property from
string$propertyNamename of the property to retrieve
bool$forceDirectAccessdirectly access property using reflection(!)
Exceptions
ExceptionPropertyNotAccessibleException
Returns
mixed Value of the property

Definition at line 79 of file ObjectAccess.php.

static getPropertyPath (   $subject,
  $propertyPath 
)
static

Gets a property path from a given object or array.

If propertyPath is "bla.blubb", then we first call getProperty($object, 'bla'), and on the resulting object we call getProperty(..., 'blubb')

For arrays the keys are checked likewise.

Parameters
mixed$subjectObject or array to get the property path from
string$propertyPath
Returns
mixed Value of the property

Definition at line 140 of file ObjectAccess.php.

Referenced by AbstractFormFieldViewHelper\addAdditionalIdentityPropertiesIfNeeded(), FormDefinition\getElementDefaultValueByIdentifier(), AbstractFormFieldViewHelper\getLastSubmittedFormData(), SelectViewHelper\getOptions(), ObjectAccessTest\getPropertyPathCanAccessPropertiesOfAnArray(), ObjectAccessTest\getPropertyPathCanAccessPropertiesOfAnObjectImplementingArrayAccess(), ObjectAccessTest\getPropertyPathCanRecursivelyGetPropertiesOfAnObject(), AbstractFormFieldViewHelper\getPropertyValue(), and AbstractFinisher\parseOption().

static getSettablePropertyNames (   $object)
static

Returns an array of properties which can be set with the setProperty() method. Includes the following properties:

  • which can be set through a public setter method.
  • public properties which can be directly set.
Parameters
object$objectObject to receive property names for
Exceptions
InvalidArgumentException
Returns
array Array of all settable property names

Definition at line 263 of file ObjectAccess.php.

Referenced by ObjectAccessTest\getSettablePropertyNamesReturnsAllPropertiesWhichAreAvailable(), and ObjectAccessTest\getSettablePropertyNamesReturnsPropertyNamesOfStdClass().

static isPropertyGettable (   $object,
  $propertyName 
)
static

Tells if the value of the specified property can be retrieved by this Object Accessor.

Parameters
object$objectObject containting the property
string$propertyNameName of the property to check
Exceptions
InvalidArgumentException
Returns
bool

Definition at line 314 of file ObjectAccess.php.

Referenced by GenericObjectValidator\getPropertyValue().

static isPropertySettable (   $object,
  $propertyName 
)
static

Tells if the value of the specified property can be set by this Object Accessor.

Parameters
object$objectObject containting the property
string$propertyNameName of the property to check
Exceptions
InvalidArgumentException
Returns
bool

Definition at line 292 of file ObjectAccess.php.

static setProperty ( $subject,
  $propertyName,
  $propertyValue,
  $forceDirectAccess = false 
)
static

Set a property for a given object. Tries to set the property the following ways:

  • if target is an array, set value
  • if super cow powers should be used, set value through reflection
  • if public setter method exists, call it.
  • if public property exists, set it directly.
  • if the target object is an instance of ArrayAccess, it sets the property on it without checking if it existed.
  • else, return FALSE
Parameters
mixed&$subjectThe target object or array
string$propertyNameName of the property to set
mixed$propertyValueValue of the property
bool$forceDirectAccessdirectly access property using reflection(!)
Exceptions
InvalidArgumentException in case $object was not an object or $propertyName was not a string
Returns
bool TRUE if the property could be set, FALSE otherwise

Definition at line 172 of file ObjectAccess.php.

Referenced by CollectionValidatorTest\collectionValidatorIsValidEarlyReturnsOnUnitializedLazyObjectStorages(), PersistentObjectConverter\convertFrom(), ObjectConverter\convertFrom(), ObjectAccessTest\setPropertyCallsASetterMethodToSetThePropertyValueIfOneIsAvailable(), ObjectAccessTest\setPropertyCanDirectlySetValuesInAnArrayObjectOrArray(), ObjectAccessTest\setPropertyThrowsExceptionIfThePropertyNameIsNotAString(), and ObjectAccessTest\setPropertyWorksWithPublicProperty().

Member Data Documentation

const ACCESS_GET = 0

Definition at line 29 of file ObjectAccess.php.

const ACCESS_PUBLIC = 2

Definition at line 33 of file ObjectAccess.php.

const ACCESS_SET = 1

Definition at line 31 of file ObjectAccess.php.