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);
83 if ($subject ===
null || is_scalar($subject)) {
86 if (!$forceDirectAccess && ($subject instanceof \SplObjectStorage || $subject instanceof
ObjectStorage)) {
87 $subject = iterator_to_array(clone $subject,
false);
91 if (($subject instanceof \ArrayAccess && $subject->offsetExists($propertyName)) || is_array($subject)) {
94 if (isset($subject[$propertyName])) {
95 return $subject[$propertyName];
97 } elseif (is_object($subject)) {
98 if ($forceDirectAccess) {
99 if (property_exists($subject, $propertyName)) {
100 $propertyReflection = new \ReflectionProperty($subject, $propertyName);
101 if ($propertyReflection->isPublic()) {
102 return $propertyReflection->getValue($subject);
104 $propertyReflection->setAccessible(
true);
105 return $propertyReflection->getValue($subject);
107 throw new Exception\PropertyNotAccessibleException(
'The property "' . $propertyName .
'" on the subject does not exist.', 1302855001);
109 $upperCasePropertyName = ucfirst($propertyName);
110 $getterMethodName =
'get' . $upperCasePropertyName;
111 if (is_callable([$subject, $getterMethodName])) {
112 return $subject->{$getterMethodName}();
114 $getterMethodName =
'is' . $upperCasePropertyName;
115 if (is_callable([$subject, $getterMethodName])) {
116 return $subject->{$getterMethodName}();
118 $getterMethodName =
'has' . $upperCasePropertyName;
119 if (is_callable([$subject, $getterMethodName])) {
120 return $subject->{$getterMethodName}();
122 if (property_exists($subject, $propertyName)) {
123 return $subject->{$propertyName};
125 throw new Exception\PropertyNotAccessibleException(
'The property "' . $propertyName .
'" on the subject does not exist.', 1476109666);
146 $propertyPathSegments = explode(
'.', $propertyPath);
148 foreach ($propertyPathSegments as $pathSegment) {
149 $subject = self::getPropertyInternal($subject, $pathSegment);
151 }
catch (
Exception\PropertyNotAccessibleException $error) {
176 public static function setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess =
false)
178 if (is_array($subject) || ($subject instanceof \ArrayAccess && !$forceDirectAccess)) {
179 $subject[$propertyName] = $propertyValue;
182 if (!is_object($subject)) {
183 throw new \InvalidArgumentException(
'subject must be an object or array, ' . gettype($subject) .
' given.', 1237301368);
185 if (!is_string($propertyName)) {
186 throw new \InvalidArgumentException(
'Given property name is not of type string.', 1231178878);
189 if ($forceDirectAccess) {
190 if (property_exists($subject, $propertyName)) {
191 $propertyReflection = new \ReflectionProperty($subject, $propertyName);
192 $propertyReflection->setAccessible(
true);
193 $propertyReflection->setValue($subject, $propertyValue);
195 $subject->{$propertyName} = $propertyValue;
199 $setterMethodName = self::buildSetterMethodName($propertyName);
200 if (is_callable([$subject, $setterMethodName])) {
201 $subject->{$setterMethodName}($propertyValue);
202 } elseif (property_exists($subject, $propertyName)) {
203 $reflection = new \ReflectionProperty($subject, $propertyName);
204 if ($reflection->isPublic()) {
205 $subject->{$propertyName} = $propertyValue;
229 if (!is_object($object)) {
230 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1237301369);
232 if ($object instanceof \stdClass) {
233 $properties = array_keys((array)$object);
238 $reflection = new \ReflectionClass($object);
239 $declaredPropertyNames = array_map(
240 function (\ReflectionProperty $property) {
241 return $property->getName();
243 $reflection->getProperties(\ReflectionProperty::IS_PUBLIC)
245 foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
246 $methodParameters = $method->getParameters();
247 if (!empty($methodParameters)) {
248 foreach ($methodParameters as $parameter) {
249 if (!$parameter->isOptional()) {
254 $methodName = $method->getName();
255 if (strpos($methodName,
'is') === 0) {
256 $declaredPropertyNames[] = lcfirst(substr($methodName, 2));
258 if (strpos($methodName,
'get') === 0) {
259 $declaredPropertyNames[] = lcfirst(substr($methodName, 3));
261 if (strpos($methodName,
'has') === 0) {
262 $declaredPropertyNames[] = lcfirst(substr($methodName, 3));
265 $propertyNames = array_unique($declaredPropertyNames);
266 sort($propertyNames);
268 return $propertyNames;
285 if (!is_object($object)) {
286 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1264022994);
288 if ($object instanceof \stdClass) {
289 $declaredPropertyNames = array_keys((array)$object);
291 $declaredPropertyNames = array_keys(get_class_vars(get_class($object)));
293 foreach (get_class_methods($object) as $methodName) {
294 if (strpos($methodName,
'set') === 0 && is_callable([$object, $methodName])) {
295 $declaredPropertyNames[] = lcfirst(substr($methodName, 3));
298 $propertyNames = array_unique($declaredPropertyNames);
299 sort($propertyNames);
300 return $propertyNames;
314 if (!is_object($object)) {
315 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1259828920);
317 if ($object instanceof \stdClass && array_key_exists($propertyName, get_object_vars($object))) {
320 if (array_key_exists($propertyName, get_class_vars(get_class($object)))) {
323 return is_callable([$object, self::buildSetterMethodName($propertyName)]);
337 if (!is_object($object)) {
338 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1259828921);
340 if ($object instanceof \ArrayAccess && isset($object[$propertyName])) {
343 if ($object instanceof \stdClass && isset($object->$propertyName)) {
346 if (is_callable([$object,
'get' . ucfirst($propertyName)])) {
349 if (is_callable([$object,
'has' . ucfirst($propertyName)])) {
352 if (is_callable([$object,
'is' . ucfirst($propertyName)])) {
355 if (property_exists($object, $propertyName)) {
356 $propertyReflection = new \ReflectionProperty($object, $propertyName);
357 return $propertyReflection->isPublic();
374 if (!is_object($object)) {
375 throw new \InvalidArgumentException(
'$object must be an object, ' . gettype($object) .
' given.', 1237301370);
378 foreach (self::getGettablePropertyNames($object) as $propertyName) {
379 $properties[$propertyName] = self::getPropertyInternal($object, $propertyName);
394 return 'set' . ucfirst($propertyName);