Completed
Push — master ( c727d7...a919fe )
by Anton
06:03 queued 01:14
created

InjectableConfig::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
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
12
/**
13
 * Generic implementation of array based configuration.
14
 *
15
 * Attention! Config has to be serialiable and be depdended ONLY on enviroment or runtime
16
 * modifications/requests. No custom logic is allowed to initiate config, in other case config cache
17
 * will be invalid.
18
 */
19
class InjectableConfig extends Component implements ConfigInterface, \ArrayAccess, \IteratorAggregate
20
{
21
    /**
22
     * Spiral provides ability to automatically inject configs using configurator.
23
     */
24
    const INJECTOR = ConfiguratorInterface::class;
25
26
    /**
27
     * Configuration data.
28
     *
29
     * @var array
30
     */
31
    protected $config = [];
32
33
    /**
34
     * At this moment on array based configs can be supported.
35
     *
36
     * @param array $config
37
     */
38
    final public function __construct(array $config = [])
39
    {
40
        $this->config = $config;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function toArray()
47
    {
48
        return $this->config;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function offsetExists($offset)
55
    {
56
        return array_key_exists($offset, $this->config);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function offsetGet($offset)
63
    {
64
        if (!$this->offsetExists($offset)) {
65
            throw new ConfigException("Undefined configuration key '{$offset}'.");
66
        }
67
68
        return $this->config[$offset];
69
    }
70
71
    /**
72
     *{@inheritdoc}
73
     *
74
     * @throws ConfigException
75
     */
76
    public function offsetSet($offset, $value)
77
    {
78
        throw new ConfigException(
79
            "Unable to change configuration data, configs are treated as immutable."
80
        );
81
    }
82
83
    /**
84
     *{@inheritdoc}
85
     *
86
     * @throws ConfigException
87
     */
88
    public function offsetUnset($offset)
89
    {
90
        throw new ConfigException(
91
            "Unable to change configuration data, configs are treated as immutable."
92
        );
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getIterator()
99
    {
100
        return new \ArrayIterator($this->config);
101
    }
102
103
    /**
104
     * Restoring state.
105
     *
106
     * @param array $an_array
107
     * @return static
108
     */
109
    public static function __set_state($an_array)
110
    {
111
        return new static($an_array['config']);
112
    }
113
}