Completed
Pull Request — master (#67)
by
unknown
02:28
created

ObjectContext::getObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace RulerZ\Context;
4
5
use Symfony\Component\PropertyAccess\PropertyAccess;
6
7
class ObjectContext implements \ArrayAccess
8
{
9
    /**
10
     * @var mixed
11
     */
12
    private $object;
13
14
    /**
15
     * @var \Symfony\Component\PropertyAccess\PropertyAccessor
16
     */
17
    private $accessor;
18
19
    /**
20
     * Constructor.
21
     *
22
     * @param mixed $object The object to extract data from.
23
     */
24
    public function __construct($object)
25
    {
26
        $this->object = $object;
27
        $this->accessor = PropertyAccess::createPropertyAccessor();
28
    }
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)) {
48
            return $value;
49
        }
50
51
        return new static($value);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function offsetExists($id)
58
    {
59
        return $this->accessor->isReadable($this->object, $id);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function offsetSet($id, $value)
66
    {
67
        throw new \RuntimeException('Context is read-only.');
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function offsetUnset($id)
74
    {
75
        throw new \RuntimeException('Context is read-only.');
76
    }
77
}
78