Completed
Push — master ( c15104...eb04a4 )
by Dmitry
14:35
created

EndpointConfig::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.8333
cc 2
nc 2
nop 2
1
<?php
2
3
namespace hiapi\endpoints;
4
5
use hiapi\endpoints\Exception\EndpointBuildingException;
6
7
class EndpointConfig implements \ArrayAccess
8
{
9
    private $config = [];
10
11
    /**
12
     * @param string $property
13
     * @param $value
14
     * @return $this
15
     * @throws EndpointBuildingException
16
     */
17
    public function set(string $property, $value): self
18
    {
19
        if (array_key_exists($property, $this->config)) {
20
            throw new EndpointBuildingException(sprintf(
21
                'Property "%s" is already set, use $config->overwrite() if you want to change it.',
22
                $property
23
            ));
24
        }
25
26
        $this->config[$property] = $value;
27
28
        return $this;
29
    }
30
31
    public function overwrite(string $property, $value): self
32
    {
33
        $this->unset($property);
34
35
        return $this->set($property, $value);
36
    }
37
38
    public function unset(string $property): self
39
    {
40
        unset($this->config[$property]);
41
42
        return $this;
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function offsetExists($offset)
49
    {
50
        return isset($this->config);
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function offsetGet($offset)
57
    {
58
        return $this->config[$offset];
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function offsetSet($offset, $value)
65
    {
66
        $this->set($offset, $value);
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function offsetUnset($offset)
73
    {
74
        $this->unset($offset);
75
    }
76
}
77