2 namespace TYPO3\CMS\Extbase\Reflection;
54 public static function getProperty($subject, $propertyName, $forceDirectAccess =
false)
56 if (!is_object($subject) && !is_array($subject)) {
57 throw new \InvalidArgumentException(
'$subject must be an object or array, ' . gettype($subject) .
' given.', 1237301367);
59 if (!is_string($propertyName) && (!is_array($subject) && !$subject instanceof \ArrayAccess)) {
60 throw new \InvalidArgumentException(
'Given property name is not of type string.', 1231178303);
62 $propertyExists =
false;
63 $propertyValue = self::getPropertyInternal($subject, $propertyName, $forceDirectAccess, $propertyExists);
64 if ($propertyExists ===
true) {
65 return $propertyValue;
67 throw new Exception\PropertyNotAccessibleException(
'The property "' . $propertyName .
'" on the subject was not accessible.', 1263391473);
86 public static function getPropertyInternal($subject, $propertyName, $forceDirectAccess, &$propertyExists)
88 if ($subject === null || is_scalar($subject)) {
91 $propertyExists =
true;
92 if (is_array($subject)) {
93 if (array_key_exists($propertyName, $subject)) {
94 return $subject[$propertyName];
96 $propertyExists =
false;
99 if ($forceDirectAccess ===
true) {
100 if (property_exists(get_class($subject), $propertyName)) {
102 return $propertyReflection->getValue($subject);
103 }
elseif (property_exists($subject, $propertyName)) {
104 return $subject->{$propertyName};
106 throw new Exception\PropertyNotAccessibleException(
'The property "' . $propertyName .
'" on the subject does not exist.', 1302855001);
109 if ($subject instanceof \SplObjectStorage || $subject instanceof \TYPO3\CMS\Extbase\Persistence\ObjectStorage) {
112 foreach ($subject as $value) {
113 if ($index === (
int)$propertyName) {
118 $propertyExists =
false;
121 }
elseif ($subject instanceof \ArrayAccess && isset($subject[$propertyName])) {
122 return $subject[$propertyName];
124 $getterMethodName =
'get' . ucfirst($propertyName);
125 if (is_callable(array($subject, $getterMethodName))) {
126 return $subject->{$getterMethodName}();
128 $getterMethodName =
'is' . ucfirst($propertyName);
129 if (is_callable(array($subject, $getterMethodName))) {
130 return $subject->{$getterMethodName}();
132 $getterMethodName =
'has' . ucfirst($propertyName);
133 if (is_callable(array($subject, $getterMethodName))) {
134 return $subject->{$getterMethodName}();
136 if (is_object($subject) && array_key_exists($propertyName, get_object_vars($subject))) {
137 return $subject->{$propertyName};
139 $propertyExists =
false;
156 public static function getPropertyPath($subject, $propertyPath)
158 $propertyPathSegments = explode(
'.', $propertyPath);
159 foreach ($propertyPathSegments as $pathSegment) {
160 $propertyExists =
false;
161 $subject = self::getPropertyInternal($subject, $pathSegment,
false, $propertyExists);
162 if (!$propertyExists || $subject === null) {
188 public static function setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess =
false)
190 if (is_array($subject)) {
191 $subject[$propertyName] = $propertyValue;
194 if (!is_object($subject)) {
195 throw new \InvalidArgumentException(
'subject must be an object or array, ' . gettype($subject) .
' given.', 1237301368);
197 if (!is_string($propertyName)) {
198 throw new \InvalidArgumentException(
'Given property name is not of type string.', 1231178878);
200 if ($forceDirectAccess ===
true) {
201 if (property_exists(get_class($subject), $propertyName)) {
203 $propertyReflection->setAccessible(
true);
204 $propertyReflection->setValue($subject, $propertyValue);
206 $subject->{$propertyName} = $propertyValue;
208 }
elseif (is_callable(array($subject, $setterMethodName = self::buildSetterMethodName($propertyName)))) {
209 $subject->{$setterMethodName}($propertyValue);
210 }
elseif ($subject instanceof \ArrayAccess) {
211 $subject[$propertyName] = $propertyValue;
212 }
elseif (array_key_exists($propertyName, get_object_vars($subject))) {
213 $subject->{$propertyName} = $propertyValue;
232 public static function getGettablePropertyNames($object)
234 if (!is_object($object)) {
235 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1237301369);
237 if ($object instanceof \stdClass) {
238 $declaredPropertyNames = array_keys(get_object_vars($object));
240 $declaredPropertyNames = array_keys(get_class_vars(get_class($object)));
242 foreach (get_class_methods($object) as $methodName) {
243 if (is_callable(array($object, $methodName))) {
244 if (substr($methodName, 0, 2) ===
'is') {
245 $declaredPropertyNames[] = lcfirst(substr($methodName, 2));
247 if (substr($methodName, 0, 3) ===
'get') {
248 $declaredPropertyNames[] = lcfirst(substr($methodName, 3));
250 if (substr($methodName, 0, 3) ===
'has') {
251 $declaredPropertyNames[] = lcfirst(substr($methodName, 3));
255 $propertyNames = array_unique($declaredPropertyNames);
256 sort($propertyNames);
257 return $propertyNames;
272 public static function getSettablePropertyNames($object)
274 if (!is_object($object)) {
275 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1264022994);
277 if ($object instanceof \stdClass) {
278 $declaredPropertyNames = array_keys(get_object_vars($object));
280 $declaredPropertyNames = array_keys(get_class_vars(get_class($object)));
282 foreach (get_class_methods($object) as $methodName) {
283 if (substr($methodName, 0, 3) ===
'set' && is_callable(array($object, $methodName))) {
284 $declaredPropertyNames[] = lcfirst(substr($methodName, 3));
287 $propertyNames = array_unique($declaredPropertyNames);
288 sort($propertyNames);
289 return $propertyNames;
301 public static function isPropertySettable($object, $propertyName)
303 if (!is_object($object)) {
304 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1259828920);
306 if ($object instanceof \stdClass && array_search($propertyName, array_keys(get_object_vars($object))) !==
false) {
308 }
elseif (array_search($propertyName, array_keys(get_class_vars(get_class($object)))) !==
false) {
311 return is_callable(array($object, self::buildSetterMethodName($propertyName)));
323 public static function isPropertyGettable($object, $propertyName)
325 if (!is_object($object)) {
326 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1259828921);
328 if ($object instanceof \ArrayAccess && isset($object[$propertyName]) ===
true) {
330 }
elseif ($object instanceof \stdClass && array_search($propertyName, array_keys(get_object_vars($object))) !==
false) {
332 }
elseif ($object instanceof \ArrayAccess && isset($object[$propertyName]) ===
true) {
335 if (is_callable(array($object,
'get' . ucfirst($propertyName)))) {
338 if (is_callable(array($object,
'has' . ucfirst($propertyName)))) {
341 if (is_callable(array($object,
'is' . ucfirst($propertyName)))) {
344 return array_search($propertyName, array_keys(get_class_vars(get_class($object)))) !==
false;
357 public static function getGettableProperties($object)
359 if (!is_object($object)) {
360 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1237301370);
362 $properties = array();
363 foreach (self::getGettablePropertyNames($object) as $propertyName) {
364 $propertyExists =
false;
365 $propertyValue = self::getPropertyInternal($object, $propertyName,
false, $propertyExists);
366 if ($propertyExists ===
true) {
367 $properties[$propertyName] = $propertyValue;
381 public static function buildSetterMethodName($propertyName)
383 return 'set' . ucfirst($propertyName);