1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Config Class. |
4
|
|
|
* |
5
|
|
|
* @package phpfeature |
6
|
|
|
* @author Alain Schlesser <[email protected]> |
7
|
|
|
* @license GPL-2.0+ |
8
|
|
|
* @link http://www.brightnucleus.com/ |
9
|
|
|
* @copyright 2016 Alain Schlesser, Bright Nucleus |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Config loader used to load config PHP files as objects. |
14
|
|
|
* |
15
|
|
|
* @since 0.1.0 |
16
|
|
|
* |
17
|
|
|
* @author Alain Schlesser <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Config extends ArrayObject implements ConfigInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Instantiate the Config object. |
24
|
|
|
* |
25
|
|
|
* @since 0.1.0 |
26
|
|
|
* |
27
|
|
|
* @param array $config Array with settings. |
28
|
|
|
*/ |
29
|
|
|
public function __construct(array $config) |
30
|
|
|
{ |
31
|
|
|
// Make sure the config entries can be accessed as properties. |
32
|
|
|
parent::__construct($config, ArrayObject::ARRAY_AS_PROPS); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Magic method that enables the use of normal array_* functions on the |
37
|
|
|
* Config object. |
38
|
|
|
* |
39
|
|
|
* @since 0.1.0 |
40
|
|
|
* |
41
|
|
|
* @param string $function The function that was called on this object. |
42
|
|
|
* @param mixed $arguments The arguments that were used for the function |
43
|
|
|
* call. |
44
|
|
|
* @return mixed |
45
|
|
|
* @throws BadMethodCallException |
46
|
|
|
*/ |
47
|
|
|
public function __call($function, $arguments) |
48
|
|
|
{ |
49
|
|
|
if ( ! is_callable($function) || substr($function, 0, 6) !== 'array_') { |
50
|
|
|
throw new BadMethodCallException(__CLASS__ . '->' . $function); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return call_user_func_array($function, array_merge(array($this->getArrayCopy()), $arguments)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Check whether the Config has a specific key. |
58
|
|
|
* |
59
|
|
|
* @since 0.1.0 |
60
|
|
|
* |
61
|
|
|
* @param string $key The key to check the existence for. |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function hasKey($key) |
65
|
|
|
{ |
66
|
|
|
return array_key_exists($key, (array)$this); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the value of a specific key. |
71
|
|
|
* |
72
|
|
|
* @since 0.1.0 |
73
|
|
|
* |
74
|
|
|
* @param string $key The key to get the value for. |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function getKey($key) |
78
|
|
|
{ |
79
|
|
|
return $this[$key]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the an array with all the keys |
84
|
|
|
* |
85
|
|
|
* @since 0.1.0 |
86
|
|
|
* @return mixed |
|
|
|
|
87
|
|
|
*/ |
88
|
|
|
public function getKeys() |
89
|
|
|
{ |
90
|
|
|
return array_keys((array)$this); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.