Passed
Pull Request — master (#5)
by
unknown
02:25
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
use Yiisoft\Yii\Cycle\Config\Exception\PropertyNotFoundException;
6
7
class BaseConfig
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
        if (method_exists($this, $getter)) {
18
            return $this->$getter();
19
        }
20
        if (property_exists($this, $name)) {
21
            return $this->$name;
22
        }
23
        throw new PropertyNotFoundException("The `$name` property not found in a " . static::class . " object");
24
    }
25
26
    public function __call($name, $arguments)
27
    {
28
        $prefix = substr($name, 0, 3);
29
        if ($prefix === 'get') {
30
            $prop = lcfirst(substr($name, 3));
31
            if (!property_exists($this, $prop)) {
32
                throw new PropertyNotFoundException("The `$name` property was not found when the `$name` method was called");
33
            }
34
            return $this->$prop;
35
        }
36
        throw new \BadMethodCallException();
37
    }
38
39
    public function configure(array $params): void
40
    {
41
        foreach ($params as $name => $value) {
42
            $setter = 'set' . ucfirst($name);
43
            if (method_exists($this, $setter)) {
44
                $this->$setter($value);
45
            } elseif (property_exists($this, $name)) {
46
                $this->$name = $value;
47
            } else {
48
                throw new PropertyNotFoundException("The `$name` property not found when configure " . static::class . " object");
49
            }
50
        }
51
    }
52
53
    /**
54
     * Return all public and protected properties as array.
55
     * Property values will be obtained using getter methods.
56
     * Note: all private properties will be ignored!
57
     * @return array
58
     * @throws PropertyNotFoundException
59
     */
60
    public function toArray(): array
61
    {
62
        $result = [];
63
        $arrayed = (array)$this;
64
        $keys = array_keys($arrayed);
65
        foreach ($keys as $key) {
66
            if ($key[0] !== chr(0)) {
67
                // public property
68
69
                $result[$key] = $this->__get($key);
70
            } elseif ($key[1] === '*') {
71
                // protected property
72
73
                $key = substr($key, 3);
74
                $result[$key] = $this->__get($key);
75
            } else {
76
                // private property
77
78
                // ignore
79
            }
80
        }
81
        return $result;
82
    }
83
}
84