ObjectContext   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 69
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A offsetExists() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A getObject() 0 4 1
A offsetGet() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RulerZ\Context;
6
7
use Symfony\Component\PropertyAccess\PropertyAccess;
8
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)
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) || $value === null) {
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