Completed
Branch develop (c2aa4c)
by Anton
05:17
created

InjectableConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Core;
9
10
use Spiral\Core\Exceptions\ConfigException;
11
use Spiral\Validation\ValidatorInterface;
12
13
/**
14
 * Generic implementation of array based configuration.
15
 *
16
 * Attention! Config has to be serialiable and be depdended ONLY on enviroment or runtime
17
 * modifications/requests. No custom logic is allowed to initiate config, in other case config cache
18
 * will be invalid.
19
 */
20
class InjectableConfig extends Component implements ConfigInterface, \ArrayAccess, \IteratorAggregate
21
{
22
    /**
23
     * Spiral provides ability to automatically inject configs using configurator.
24
     */
25
    const INJECTOR = ConfiguratorInterface::class;
26
27
    /**
28
     * Configuration data.
29
     *
30
     * @var array
31
     */
32
    protected $config = [];
33
34
    /**
35
     * At this moment on array based configs can be supported.
36
     *
37
     * @param array $config
38
     */
39
    public function __construct(array $config = [])
40
    {
41
        $this->config = $config;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function toArray()
48
    {
49
        return $this->config;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function validate(ValidatorInterface $validator)
56
    {
57
        return $validator;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function offsetExists($offset)
64
    {
65
        return array_key_exists($offset, $this->config);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function offsetGet($offset)
72
    {
73
        if (!$this->offsetExists($offset)) {
74
            throw new ConfigException("Undefined configuration key '{$offset}'.");
75
        }
76
77
        return $this->config[$offset];
78
    }
79
80
    /**
81
     *{@inheritdoc}
82
     *
83
     * @throws ConfigException
84
     */
85
    public function offsetSet($offset, $value)
86
    {
87
        throw new ConfigException(
88
            "Unable to change configuration data, configs are treated as immutable."
89
        );
90
    }
91
92
    /**
93
     *{@inheritdoc}
94
     *
95
     * @throws ConfigException
96
     */
97
    public function offsetUnset($offset)
98
    {
99
        throw new ConfigException(
100
            "Unable to change configuration data, configs are treated as immutable."
101
        );
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function getIterator()
108
    {
109
        return new \ArrayIterator($this->config);
110
    }
111
112
    /**
113
     * Restoring state.
114
     *
115
     * @param array $an_array
116
     * @return static
117
     */
118
    public static function __set_state($an_array)
119
    {
120
        return new static($an_array['config']);
121
    }
122
}