Completed
Push — master ( c6e2ca...b2d8c9 )
by Tony
09:34
created

AbstractConfig::depend()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace ClassConfig;
4
5
use ClassConfig\Exceptions\MissingConfigException;
6
7
/**
8
 * Class Config
9
 * @package ClassConfig
10
 */
11
abstract class AbstractConfig
12
{
13
    /**
14
     * @var null|AbstractConfig
15
     */
16
    protected $___parent;
17
18
    /**
19
     * @var null|string
20
     */
21
    protected $___key;
22
23
    /**
24
     * AbstractConfig constructor.
25
     *
26
     * @param AbstractConfig|null $parent
27
     * @param string|null $key
28
     */
29
    public function __construct(AbstractConfig $parent = null, string $key = null)
30
    {
31
        $this->___parent = $parent;
32
        $this->___key = $key;
33
    }
34
35
    /**
36
     * @param string $key
37
     * @return bool
38
     */
39
    public function exists(string $key): bool
40
    {
41
        return property_exists($this, '__' . $key . '__');
42
    }
43
44
    /**
45
     * @param string $key
46
     * @return AbstractConfig
47
     * @throws MissingConfigException
48
     */
49
    public function depend(string $key): AbstractConfig
50
    {
51
        if (!isset($this->$key)) {
52
            $trail = [$key];
53
54
            $config = $this;
55
            while ($config->___parent) {
56
                $trail[] = $config->___key;
57
                $config = $config->___parent;
58
            }
59
60
            throw new MissingConfigException(array_reverse($trail));
61
        }
62
        return $this;
63
    }
64
}