ServiceSettings.php
9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
* LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* PHP version 5
*
* @category Microsoft
* @package WindowsAzure\Common\Internal
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
* @copyright 2012 Microsoft Corporation
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://github.com/windowsazure/azure-sdk-for-php
*/
namespace WindowsAzure\Common\Internal;
use WindowsAzure\Common\Internal\Resources;
/**
* Base class for all REST services settings.
*
* Derived classes must implement the following members:
* 1- $isInitialized: A static property that indicates whether the class's static
* members have been initialized.
* 2- init(): A protected static method that initializes static members.
* 3- $validSettingKeys: A static property that contains valid setting keys for this
* service.
* 4- createFromConnectionString($connectionString): A public static function that
* takes a connection string and returns the created settings object.
*
* @category Microsoft
* @package WindowsAzure\Common\Internal
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
* @copyright 2012 Microsoft Corporation
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @version Release: 0.4.1_2015-03
* @link https://github.com/windowsazure/azure-sdk-for-php
*/
abstract class ServiceSettings
{
/**
* Throws an exception if the connection string format does not match any of the
* available formats.
*
* @param type $connectionString The invalid formatted connection string.
*
* @return none
*
* @throws \RuntimeException
*/
protected static function noMatch($connectionString)
{
throw new \RuntimeException(
sprintf(Resources::MISSING_CONNECTION_STRING_SETTINGS, $connectionString)
);
}
/**
* Parses the connection string and then validate that the parsed keys belong to
* the $validSettingKeys
*
* @param string $connectionString The user provided connection string.
*
* @return array The tokenized connection string keys.
*
* @throws \RuntimeException
*/
protected static function parseAndValidateKeys($connectionString)
{
// Initialize the static values if they are not initialized yet.
if (!static::$isInitialized) {
static::init();
static::$isInitialized = true;
}
$tokenizedSettings = ConnectionStringParser::parseConnectionString(
'connectionString',
$connectionString
);
// Assure that all given keys are valid.
foreach ($tokenizedSettings as $key => $value) {
if (!Utilities::inArrayInsensitive($key, static::$validSettingKeys) ) {
throw new \RuntimeException(
sprintf(
Resources::INVALID_CONNECTION_STRING_SETTING_KEY,
$key,
implode("\n", static::$validSettingKeys)
)
);
}
}
return $tokenizedSettings;
}
/**
* Creates an anonymous function that acts as predicate.
*
* @param array $requirements The array of conditions to satisfy.
* @param boolean $isRequired Either these conditions are all required or all
* optional.
* @param boolean $atLeastOne Indicates that at least one requirement must
* succeed.
*
* @return callable
*/
protected static function getValidator($requirements, $isRequired, $atLeastOne)
{
// @codingStandardsIgnoreStart
return function ($userSettings)
use ($requirements, $isRequired, $atLeastOne) {
$oneFound = false;
$result = array_change_key_case($userSettings);
foreach ($requirements as $requirement) {
$settingName = strtolower($requirement[Resources::SETTING_NAME]);
// Check if the setting name exists in the provided user settings.
if (array_key_exists($settingName, $result)) {
// Check if the provided user setting value is valid.
$validationFunc = $requirement[Resources::SETTING_CONSTRAINT];
$isValid = $validationFunc($result[$settingName]);
if ($isValid) {
// Remove the setting as indicator for successful validation.
unset($result[$settingName]);
$oneFound = true;
}
} else {
// If required then fail because the setting does not exist
if ($isRequired) {
return null;
}
}
}
if ($atLeastOne) {
// At least one requirement must succeed, otherwise fail.
return $oneFound ? $result : null;
} else {
return $result;
}
};
// @codingStandardsIgnoreEnd
}
/**
* Creates at lease one succeed predicate for the provided list of requirements.
*
* @return callable
*/
protected static function atLeastOne()
{
$allSettings = func_get_args();
return self::getValidator($allSettings, false, true);
}
/**
* Creates an optional predicate for the provided list of requirements.
*
* @return callable
*/
protected static function optional()
{
$optionalSettings = func_get_args();
return self::getValidator($optionalSettings, false, false);
}
/**
* Creates an required predicate for the provided list of requirements.
*
* @return callable
*/
protected static function allRequired()
{
$requiredSettings = func_get_args();
return self::getValidator($requiredSettings, true, false);
}
/**
* Creates a setting value condition using the passed predicate.
*
* @param string $name The setting key name.
* @param callable $predicate The setting value predicate.
*
* @return array
*/
protected static function settingWithFunc($name, $predicate)
{
$requirement = array();
$requirement[Resources::SETTING_NAME] = $name;
$requirement[Resources::SETTING_CONSTRAINT] = $predicate;
return $requirement;
}
/**
* Creates a setting value condition that validates it is one of the
* passed valid values.
*
* @param string $name The setting key name.
*
* @return array
*/
protected static function setting($name)
{
$validValues = func_get_args();
// Remove $name argument.
unset($validValues[0]);
$validValuesCount = func_num_args();
$predicate = function ($settingValue) use ($validValuesCount, $validValues) {
if (empty($validValues)) {
// No restrictions, succeed,
return true;
}
// Check to find if the $settingValue is valid or not. The index must
// start from 1 as unset deletes the value but does not update the array
// indecies.
for ($index = 1; $index < $validValuesCount; $index++) {
if ($settingValue == $validValues[$index]) {
// $settingValue is found in valid values set, succeed.
return true;
}
}
throw new \RuntimeException(
sprintf(
Resources::INVALID_CONFIG_VALUE,
$settingValue,
implode("\n", $validValues)
)
);
// $settingValue is missing in valid values set, fail.
return false;
};
return self::settingWithFunc($name, $predicate);
}
/**
* Tests to see if a given list of settings matches a set of filters exactly.
*
* @param array $settings The settings to check.
*
* @return boolean If any filter returns null, false. If there are any settings
* left over after all filters are processed, false. Otherwise true.
*/
protected static function matchedSpecification($settings)
{
$constraints = func_get_args();
// Remove first element which corresponds to $settings
unset($constraints[0]);
foreach ($constraints as $constraint) {
$remainingSettings = $constraint($settings);
if (is_null($remainingSettings)) {
return false;
} else {
$settings = $remainingSettings;
}
}
if (empty($settings)) {
return true;
}
return false;
}
}