| @@ 73-96 (lines=24) @@ | ||
| 70 | * @return mixed|null |
|
| 71 | * @throws ReflectionException |
|
| 72 | */ |
|
| 73 | protected function getFromObject($object, string $field) |
|
| 74 | { |
|
| 75 | $reflectionClass = new ReflectionClass($object); |
|
| 76 | ||
| 77 | $getterMethod = $this->findGetterMethod($reflectionClass, $field); |
|
| 78 | if ($getterMethod !== null) { |
|
| 79 | return $getterMethod->invoke($object); |
|
| 80 | } |
|
| 81 | ||
| 82 | $property = $this->findProperty($reflectionClass, $field); |
|
| 83 | if ($property !== null) { |
|
| 84 | return $property->getValue($object); |
|
| 85 | } |
|
| 86 | ||
| 87 | if ($object instanceof ArrayAccess && $object->offsetExists($field)) { |
|
| 88 | return $object->offsetGet($field); |
|
| 89 | } |
|
| 90 | ||
| 91 | if ($this->throwOnFail) { |
|
| 92 | throw new ReflectionException("cannot extract field '$field' from object"); |
|
| 93 | } else { |
|
| 94 | return null; |
|
| 95 | } |
|
| 96 | } |
|
| 97 | ||
| 98 | /** |
|
| 99 | * @param ReflectionClass $reflectionClass |
|
| @@ 55-79 (lines=25) @@ | ||
| 52 | $object[$field] = $value; |
|
| 53 | } |
|
| 54 | ||
| 55 | protected function setInObject(&$object, string $field, $value) |
|
| 56 | { |
|
| 57 | $reflectionClass = new ReflectionClass($object); |
|
| 58 | ||
| 59 | $setterMethod = $this->findSetterMethod($reflectionClass, $field); |
|
| 60 | if ($setterMethod !== null) { |
|
| 61 | $setterMethod->invoke($object, $value); |
|
| 62 | return; |
|
| 63 | } |
|
| 64 | ||
| 65 | $property = $this->findProperty($reflectionClass, $field); |
|
| 66 | if ($property !== null) { |
|
| 67 | $property->setValue($object, $value); |
|
| 68 | return; |
|
| 69 | } |
|
| 70 | ||
| 71 | if ($object instanceof ArrayAccess) { |
|
| 72 | $object->offsetSet($field, $value); |
|
| 73 | return; |
|
| 74 | } |
|
| 75 | ||
| 76 | if ($this->throwOnFail) { |
|
| 77 | throw new ReflectionException("cannot set field '$field' in object"); |
|
| 78 | } |
|
| 79 | } |
|
| 80 | ||
| 81 | /** |
|
| 82 | * @param ReflectionClass $reflectionClass |
|