Passed
Pull Request — master (#5)
by
unknown
03:01
created

BaseConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Cycle\Config;
4
5
class BaseConfig
6
{
7
    protected $data = [];
8
9
    public function __construct(Params $params)
10
    {
11
        $this->configure($params->get(static::class, []));
12
    }
13
14
    public function __get($name)
15
    {
16
        $getter = 'get' . ucfirst($name);
17
        return method_exists($this, $getter) ? $this->$getter() : ($this->data[$name] ?? null);
18
    }
19
20
    public function __set($name, $value): void
21
    {
22
        $setter = 'set' . ucfirst($name);
23
        if (method_exists($this, $setter)) {
24
            $this->$setter($value);
25
        } elseif (key_exists($name, $this->data)) {
26
            $this->data[$name] = $value;
27
        }
28
    }
29
30
    public function configure(array $params): void
31
    {
32
        foreach ($params as $k => $v) {
33
            $this->__set($k, $v);
34
        }
35
    }
36
37
    public function toArray(): array
38
    {
39
        $result = [];
40
        foreach ($this->data as $k => $v) {
41
            $result[$k] = $this->__get($k);
42
        }
43
        return $result;
44
    }
45
}
46