53 static public function getProperty($subject, $propertyName, $forceDirectAccess = FALSE) {
54 if (!is_object($subject) && !is_array($subject)) {
55 throw new \InvalidArgumentException(
'$subject must be an object or array, ' . gettype($subject) .
' given.', 1237301367);
57 if (!is_string($propertyName) && (!is_array($subject) && !$subject instanceof \ArrayAccess)) {
58 throw new \InvalidArgumentException(
'Given property name is not of type string.', 1231178303);
60 $propertyExists = FALSE;
61 $propertyValue = self::getPropertyInternal($subject, $propertyName, $forceDirectAccess, $propertyExists);
62 if ($propertyExists === TRUE) {
63 return $propertyValue;
65 throw new \TYPO3\CMS\Extbase\Reflection\Exception\PropertyNotAccessibleException(
'The property "' . $propertyName .
'" on the subject was not accessible.', 1263391473);
84 static public function getPropertyInternal($subject, $propertyName, $forceDirectAccess, &$propertyExists) {
85 if ($subject === NULL || is_scalar($subject)) {
88 $propertyExists = TRUE;
89 if (is_array($subject)) {
90 if (array_key_exists($propertyName, $subject)) {
91 return $subject[$propertyName];
93 $propertyExists = FALSE;
96 if ($forceDirectAccess === TRUE) {
97 if (property_exists(get_class($subject), $propertyName)) {
98 $propertyReflection = new \TYPO3\CMS\Extbase\Reflection\PropertyReflection(get_class($subject), $propertyName);
99 return $propertyReflection->getValue($subject);
100 } elseif (property_exists($subject, $propertyName)) {
101 return $subject->{$propertyName};
103 throw new \TYPO3\CMS\Extbase\Reflection\Exception\PropertyNotAccessibleException(
'The property "' . $propertyName .
'" on the subject does not exist.', 1302855001);
106 if ($subject instanceof \SplObjectStorage || $subject instanceof \
TYPO3\CMS\Extbase\Persistence\ObjectStorage) {
109 foreach ($subject as $value) {
110 if ($index === (
int)$propertyName) {
115 $propertyExists = FALSE;
118 } elseif ($subject instanceof \ArrayAccess && isset($subject[$propertyName])) {
119 return $subject[$propertyName];
121 $getterMethodName =
'get' . ucfirst($propertyName);
122 if (is_callable(array($subject, $getterMethodName))) {
123 return $subject->{$getterMethodName}();
125 $getterMethodName =
'is' . ucfirst($propertyName);
126 if (is_callable(array($subject, $getterMethodName))) {
127 return $subject->{$getterMethodName}();
129 if (is_object($subject) && array_key_exists($propertyName, get_object_vars($subject))) {
130 return $subject->{$propertyName};
132 $propertyExists = FALSE;
150 $propertyPathSegments = explode(
'.', $propertyPath);
151 foreach ($propertyPathSegments as $pathSegment) {
152 $propertyExists = FALSE;
153 $subject = self::getPropertyInternal($subject, $pathSegment, FALSE, $propertyExists);
154 if (!$propertyExists || $subject === NULL) {
180 static public function setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess = FALSE) {
181 if (is_array($subject)) {
182 $subject[$propertyName] = $propertyValue;
185 if (!is_object($subject)) {
186 throw new \InvalidArgumentException(
'subject must be an object or array, ' . gettype($subject) .
' given.', 1237301368);
188 if (!is_string($propertyName)) {
189 throw new \InvalidArgumentException(
'Given property name is not of type string.', 1231178878);
191 if ($forceDirectAccess === TRUE) {
192 if (property_exists(get_class($subject), $propertyName)) {
193 $propertyReflection = new \TYPO3\CMS\Extbase\Reflection\PropertyReflection(get_class($subject), $propertyName);
194 $propertyReflection->setAccessible(TRUE);
195 $propertyReflection->setValue($subject, $propertyValue);
197 $subject->{$propertyName} = $propertyValue;
199 } elseif (is_callable(array($subject, $setterMethodName = self::buildSetterMethodName($propertyName)))) {
200 $subject->{$setterMethodName}($propertyValue);
201 } elseif ($subject instanceof \ArrayAccess) {
202 $subject[$propertyName] = $propertyValue;
203 } elseif (array_key_exists($propertyName, get_object_vars($subject))) {
204 $subject->{$propertyName} = $propertyValue;
224 if (!is_object($object)) {
225 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1237301369);
227 if ($object instanceof \stdClass) {
228 $declaredPropertyNames = array_keys(get_object_vars($object));
230 $declaredPropertyNames = array_keys(get_class_vars(get_class($object)));
232 foreach (get_class_methods($object) as $methodName) {
233 if (is_callable(array($object, $methodName))) {
234 if (substr($methodName, 0, 2) ===
'is') {
235 $declaredPropertyNames[] = lcfirst(substr($methodName, 2));
237 if (substr($methodName, 0, 3) ===
'get') {
238 $declaredPropertyNames[] = lcfirst(substr($methodName, 3));
242 $propertyNames = array_unique($declaredPropertyNames);
243 sort($propertyNames);
244 return $propertyNames;
260 if (!is_object($object)) {
261 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1264022994);
263 if ($object instanceof \stdClass) {
264 $declaredPropertyNames = array_keys(get_object_vars($object));
266 $declaredPropertyNames = array_keys(get_class_vars(get_class($object)));
268 foreach (get_class_methods($object) as $methodName) {
269 if (substr($methodName, 0, 3) ===
'set' && is_callable(array($object, $methodName))) {
270 $declaredPropertyNames[] = lcfirst(substr($methodName, 3));
273 $propertyNames = array_unique($declaredPropertyNames);
274 sort($propertyNames);
275 return $propertyNames;
288 if (!is_object($object)) {
289 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1259828920);
291 if ($object instanceof \stdClass && array_search($propertyName, array_keys(get_object_vars($object))) !== FALSE) {
293 } elseif (array_search($propertyName, array_keys(get_class_vars(get_class($object)))) !== FALSE) {
296 return is_callable(array($object, self::buildSetterMethodName($propertyName)));
309 if (!is_object($object)) {
310 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1259828921);
312 if ($object instanceof \ArrayAccess && isset($object[$propertyName]) === TRUE) {
314 } elseif ($object instanceof \stdClass && array_search($propertyName, array_keys(get_object_vars($object))) !== FALSE) {
316 } elseif ($object instanceof \ArrayAccess && isset($object[$propertyName]) === TRUE) {
319 if (is_callable(array($object,
'get' . ucfirst($propertyName)))) {
322 if (is_callable(array($object,
'is' . ucfirst($propertyName)))) {
325 return array_search($propertyName, array_keys(get_class_vars(get_class($object)))) !== FALSE;
339 if (!is_object($object)) {
340 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1237301370);
342 $properties = array();
343 foreach (self::getGettablePropertyNames($object) as $propertyName) {
344 $propertyExists = FALSE;
345 $propertyValue = self::getPropertyInternal($object, $propertyName, FALSE, $propertyExists);
346 if ($propertyExists === TRUE) {
347 $properties[$propertyName] = $propertyValue;
362 return 'set' . ucfirst($propertyName);
static getPropertyInternal($subject, $propertyName, $forceDirectAccess, &$propertyExists)
static canBeInterpretedAsInteger($var)
static isPropertyGettable($object, $propertyName)
static isPropertySettable($object, $propertyName)
static getSettablePropertyNames($object)
static getGettableProperties($object)
static buildSetterMethodName($propertyName)
static setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess=FALSE)
static getPropertyPath($subject, $propertyPath)
static getGettablePropertyNames($object)
static getProperty($subject, $propertyName, $forceDirectAccess=FALSE)