Attachable::filter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 $_name;
14
15
    /**
16
     * Attachable constructor.
17
     *
18
     * @param string $key
19
     * @param array $storage
20
     */
21 8
    protected function initializer(string $key, array $storage): void
22
    {
23 8
        $this->_name = $key;
24 8
        $this->attached($storage);
25 8
    }
26
27
    /**
28
     * @param array $storage
29
     */
30 8
    protected function attached(array $storage): void
31
    {
32 8
        foreach ($this->filter($storage) as $key => $value) {
33 8
            $this->$key = $value;
34
        }
35 8
    }
36
37
    /**
38
     * @param array $storage
39
     * @return array
40
     */
41
    protected function filter(array $storage): array
42
    {
43 8
        return \array_filter($storage, function (string $key) {
44 8
            $this->checkProperty($key);
45 8
            return $key{0} !== '@'; // for xml
46 8
        }, \ARRAY_FILTER_USE_KEY);
47
    }
48
49
    /**
50
     * @param string $key
51
     * @throws
52
     */
53 8
    protected function checkProperty(string $key): void
54
    {
55 8
        if (!\property_exists($this, $key) || \strpos($key, '_') === 0) {
56
            throw new Runtime(\sprintf('The key `%s` is not registered', $key));
57
        }
58 8
    }
59
60
}
61