Completed
Pull Request — master (#3)
by Бабичев
03:59
created

Attachable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializer() 0 4 1
B attached() 0 13 6
1
<?php
2
3
namespace Bavix\Router;
4
5
use Bavix\Exceptions\Runtime;
6
7
trait Attachable
8
{
9
10
    /**
11
     * @var string
12
     */
13
    protected $_key;
14
15
    /**
16
     * Attachable constructor.
17
     *
18
     * @param string $key
19
     * @param array $storage
20
     */
21
    public function initializer(string $key, array $storage): void
22
    {
23
        $this->_key = $key;
24
        $this->attached($storage);
25
    }
26
27
    /**
28
     * @param array $storage
29
     */
30
    protected function attached(array $storage): void
31
    {
32
        foreach ($storage as $key => $value) {
33
            if ($key{0} === '_' || !\property_exists($this, $key)) {
34
                throw new Runtime(\sprintf('The key `%s` is not registered', $key));
35
            }
36
37
            if (null === $value || $key{0} === '@') {
38
                // skip
39
                continue;
40
            }
41
42
            $this->$key = $value;
43
        }
44
    }
45
46
}
47