| 1 | <?php |
||
| 9 | class ObjectContext implements \ArrayAccess |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var mixed |
||
| 13 | */ |
||
| 14 | private $object; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var \Symfony\Component\PropertyAccess\PropertyAccessor |
||
| 18 | */ |
||
| 19 | private $accessor; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @param mixed $object The object to extract data from. |
||
| 23 | */ |
||
| 24 | public function __construct($object) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Returns the object of the context. |
||
| 32 | * |
||
| 33 | * @return mixed |
||
| 34 | */ |
||
| 35 | public function getObject() |
||
| 36 | { |
||
| 37 | return $this->object; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | public function offsetGet($id) |
||
| 44 | { |
||
| 45 | $value = $this->accessor->getValue($this->object, $id); |
||
| 46 | |||
| 47 | if (is_scalar($value) || $value === null) { |
||
| 48 | return $value; |
||
| 49 | } |
||
| 50 | |||
| 51 | return new static($value); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | public function offsetExists($id) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * {@inheritdoc} |
||
| 64 | */ |
||
| 65 | public function offsetSet($id, $value) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | public function offsetUnset($id) |
||
| 77 | } |
||
| 78 |