1 | <?php |
||
32 | class Config extends AbstractConfig |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * The schema of the Config file. |
||
37 | * |
||
38 | * @var Schema |
||
39 | */ |
||
40 | protected $schema; |
||
41 | |||
42 | /** |
||
43 | * The Validator class that gets asked to do the validation of the config. |
||
44 | * |
||
45 | * @since 0.1.0 |
||
46 | * |
||
47 | * @var Validator |
||
48 | */ |
||
49 | protected $validator; |
||
50 | |||
51 | /** |
||
52 | * Instantiate the Config object. |
||
53 | * |
||
54 | * It accepts either an array with the configuration settings, or a |
||
55 | * filename pointing to a PHP file it can include. |
||
56 | * |
||
57 | * @since 0.1.0 |
||
58 | * @since 0.1.6 Accepts a delimiter to parse configuration keys. |
||
59 | * |
||
60 | * @param array|string $config Array with settings or filename for the |
||
61 | * settings file. |
||
62 | * @param Schema|null $schema Optional. Config that contains default |
||
63 | * values that can get overwritten. |
||
64 | * @param Validator|null $validator Optional. Validator class that does the |
||
65 | * actual validation. |
||
66 | * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse |
||
67 | * configuration keys. Defaults to "\", "/" & ".". |
||
68 | * |
||
69 | * @throws InvalidConfigurationSourceException If the config source is not a string or array. |
||
70 | * @throws FailedToInstantiateParentException If the parent class could not be instantiated. |
||
71 | * @throws FailedToLoadConfigException If loading of the config source failed. |
||
72 | * @throws FailedToResolveConfigException If the config file could not be resolved. |
||
73 | * @throws InvalidConfigException If the config file is not valid. |
||
74 | */ |
||
75 | 11 | public function __construct( |
|
76 | $config, |
||
77 | Schema $schema = null, |
||
78 | Validator $validator = null, |
||
79 | $delimiter = null |
||
80 | ) { |
||
81 | 11 | $this->schema = $schema; |
|
82 | 11 | $this->validator = $validator; |
|
83 | |||
84 | // Make sure $config is either a string or array. |
||
85 | 11 | if (! (is_string($config) || is_array($config))) { |
|
86 | 1 | throw new InvalidConfigurationSourceException( |
|
87 | 1 | sprintf( |
|
88 | 1 | _('Invalid configuration source: %1$s'), |
|
89 | 1 | print_r($config, true) |
|
90 | ) |
||
91 | ); |
||
92 | } |
||
93 | |||
94 | 10 | if (is_string($config)) { |
|
95 | 6 | $config = Loader::load($config); |
|
96 | } |
||
97 | |||
98 | // Run the $config through the OptionsResolver. |
||
99 | 8 | $config = $this->resolveOptions($config); |
|
|
|||
100 | |||
101 | // Instantiate the parent class. |
||
102 | try { |
||
103 | 8 | parent::__construct($config, $delimiter); |
|
104 | } catch (Exception $exception) { |
||
105 | throw new FailedToInstantiateParentException( |
||
106 | sprintf( |
||
107 | _('Could not instantiate the configuration through its parent. Reason: %1$s'), |
||
108 | $exception->getMessage() |
||
109 | ) |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | // Finally, validate the resulting config. |
||
114 | 8 | if (! $this->isValid()) { |
|
115 | 1 | throw new InvalidConfigException( |
|
116 | 1 | sprintf( |
|
117 | 1 | _('ConfigInterface file is not valid: %1$s'), |
|
118 | 1 | print_r($config, true) |
|
119 | ) |
||
120 | ); |
||
121 | } |
||
122 | 8 | } |
|
123 | |||
124 | /** |
||
125 | * Validate the Config file. |
||
126 | * |
||
127 | * @since 0.1.0 |
||
128 | * |
||
129 | * @return boolean |
||
130 | */ |
||
131 | 4 | public function isValid() |
|
139 | |||
140 | /** |
||
141 | * Process the passed-in defaults and merge them with the new values, while |
||
142 | * checking that all required options are set. |
||
143 | * |
||
144 | * @since 0.1.0 |
||
145 | * |
||
146 | * @param array $config Configuration settings to resolve. |
||
147 | * |
||
148 | * @return array Resolved configuration settings. |
||
149 | * @throws FailedToResolveConfigException If there are errors while resolving the options. |
||
150 | */ |
||
151 | 6 | protected function resolveOptions($config) |
|
173 | |||
174 | /** |
||
175 | * Configure the possible and required options for the Config. |
||
176 | * |
||
177 | * This should return a bool to let the resolve_options() know whether the |
||
178 | * actual resolving needs to be done or not. |
||
179 | * |
||
180 | * @since 0.1.0 |
||
181 | * |
||
182 | * @param OptionsResolver $resolver Reference to the OptionsResolver |
||
183 | * instance. |
||
184 | * |
||
185 | * @return bool Whether to do the resolving. |
||
186 | * @throws FailedToResolveConfigException If there are errors while processing. |
||
187 | */ |
||
188 | 2 | protected function configureOptions(OptionsResolver $resolver) |
|
219 | } |
||
220 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.