Completed
Push — master ( cd4d8a...a00a01 )
by Kévin
07:15
created

ExecutionContext::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace RulerZ\Context;
4
5
class ExecutionContext implements \ArrayAccess
6
{
7
    /**
8
     * @var array
9
     */
10
    private $data = [];
11
12
    /**
13
     * @param array $data The context data.
14
     */
15
    public function __construct(array $data = [])
16
    {
17
        $this->data = $data;
18
    }
19
20
    public function get($key, $default = null)
21
    {
22
        if (isset($this->data[$key])) {
23
            return $this->data[$key];
24
        }
25
26
        return $default;
27
    }
28
29
    /**
30
     * Get a data.
31
     *
32
     * @param string $key Key.
33
     *
34
     * @return mixed
35
     */
36
    public function offsetGet($key)
37
    {
38
        if (!array_key_exists($key, $this->data)) {
39
            throw new \RuntimeException(sprintf('Identifier "%s" does not exist in the context.', $key));
40
        }
41
42
        return $this->data[$key];
43
    }
44
45
    /**
46
     * Check if a data exists.
47
     *
48
     * @param string $key Key.
49
     *
50
     * @return bool
51
     */
52
    public function offsetExists($key)
53
    {
54
        return array_key_exists($key, $this->data);
55
    }
56
57
    /**
58
     * Set a data.
59
     *
60
     * @param string $key   Key.
61
     * @param mixed  $value Value.
62
     */
63
    public function offsetSet($key, $value)
64
    {
65
        throw new \LogicException('The execution context is read-only.');
66
    }
67
68
    /**
69
     * Unset a data.
70
     *
71
     * @param string $key Key.
72
     */
73
    public function offsetUnset($key)
74
    {
75
        throw new \LogicException('The execution context is read-only.');
76
    }
77
}
78