This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
/
ConnectionStringParser.php
346 lines (301 loc) · 9.41 KB
/
ConnectionStringParser.php
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?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
*
* @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;
/**
* Helper methods for parsing connection strings. The rules for formatting connection
* strings are defined here:
* www.connectionstrings.com/articles/show/important-rules-for-connection-strings.
*
* @category Microsoft
*
* @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.5.0_2016-11
*
* @link https://github.com/windowsazure/azure-sdk-for-php
*/
class ConnectionStringParser
{
/**
* @var string
*/
private $_argumentName;
/**
* @var string
*/
private $_value;
/**
* @var int
*/
private $_pos;
/**
* @var string
*/
private $_state;
/**
* Parses the connection string into a collection of key/value pairs.
*
* @param string $argumentName Name of the argument to be used in error
* messages
* @param string $connectionString Connection string
*
* @return array
*
* @static
*/
public static function parseConnectionString($argumentName, $connectionString)
{
Validate::isString($argumentName, 'argumentName');
Validate::notNullOrEmpty($argumentName, 'argumentName');
Validate::isString($connectionString, 'connectionString');
Validate::notNullOrEmpty($connectionString, 'connectionString');
$parser = new self($argumentName, $connectionString);
return $parser->_parse();
}
/**
* Initializes the object.
*
* @param string $argumentName Name of the argument to be used in error
* messages
* @param string $value Connection string
*/
private function __construct($argumentName, $value)
{
$this->_argumentName = $argumentName;
$this->_value = $value;
$this->_pos = 0;
$this->_state = ParserState::EXPECT_KEY;
}
/**
* Parses the connection string.
*
* @return array
*
* @throws \RuntimeException
*/
private function _parse()
{
$key = null;
$value = null;
$connectionStringValues = [];
while (true) {
$this->_skipWhiteSpaces();
if ($this->_pos == strlen($this->_value)
&& $this->_state != ParserState::EXPECT_VALUE
) {
// Not stopping after the end has been reached and a value is
// expected results in creating an empty value, which we expect.
break;
}
switch ($this->_state) {
case ParserState::EXPECT_KEY:
$key = $this->_extractKey();
$this->_state = ParserState::EXPECT_ASSIGNMENT;
break;
case ParserState::EXPECT_ASSIGNMENT:
$this->_skipOperator('=');
$this->_state = ParserState::EXPECT_VALUE;
break;
case ParserState::EXPECT_VALUE:
$value = $this->_extractValue();
$this->_state = ParserState::EXPECT_SEPARATOR;
$connectionStringValues[$key] = $value;
$key = null;
$value = null;
break;
default:
$this->_skipOperator(';');
$this->_state = ParserState::EXPECT_KEY;
break;
}
}
// Must end parsing in the valid state (expected key or separator)
if ($this->_state == ParserState::EXPECT_ASSIGNMENT) {
throw $this->_createException(
$this->_pos,
Resources::MISSING_CONNECTION_STRING_CHAR,
'='
);
}
return $connectionStringValues;
}
/**
*Generates an invalid connection string exception with the detailed error
* message.
*
* @param int $position The position of the error
* @param string $errorString The short error formatting string
*
* @return \RuntimeException
*/
private function _createException($position, $errorString)
{
$arguments = func_get_args();
// Remove first argument (position)
unset($arguments[0]);
// Create a short error message.
$errorString = sprintf($errorString, $arguments);
// Add position.
$errorString = sprintf(
Resources::ERROR_PARSING_STRING,
$errorString,
$position
);
// Create final error message.
$errorString = sprintf(
Resources::INVALID_CONNECTION_STRING,
$this->_argumentName,
$errorString
);
return new \RuntimeException($errorString);
}
/**
* Skips whitespaces at the current position.
*/
private function _skipWhiteSpaces()
{
while ($this->_pos < strlen($this->_value)
&& ctype_space($this->_value[$this->_pos])
) {
++$this->_pos;
}
}
/**
* Extracts the key's value.
*
* @return string
*/
private function _extractValue()
{
$value = Resources::EMPTY_STRING;
if ($this->_pos < strlen($this->_value)) {
$ch = $this->_value[$this->_pos];
if ($ch == '"' || $ch == '\'') {
// Value is contained between double quotes or skipped single quotes.
++$this->_pos;
$value = $this->_extractString($ch);
} else {
$firstPos = $this->_pos;
$isFound = false;
while ($this->_pos < strlen($this->_value) && !$isFound) {
$ch = $this->_value[$this->_pos];
if ($ch == ';') {
$isFound = true;
} else {
++$this->_pos;
}
}
$value = rtrim(
substr($this->_value, $firstPos, $this->_pos - $firstPos)
);
}
}
return $value;
}
/**
* Extracts key at the current position.
*
* @return string
*/
private function _extractKey()
{
$key = null;
$firstPos = $this->_pos;
$ch = $this->_value[$this->_pos];
if ($ch == '"' || $ch == '\'') {
++$this->_pos;
$key = $this->_extractString($ch);
} elseif ($ch == ';' || $ch == '=') {
// Key name was expected.
throw $this->_createException(
$firstPos,
Resources::ERROR_CONNECTION_STRING_MISSING_KEY
);
} else {
while ($this->_pos < strlen($this->_value)) {
$ch = $this->_value[$this->_pos];
// At this point we've read the key, break.
if ($ch == '=') {
break;
}
++$this->_pos;
}
$key = rtrim(substr($this->_value, $firstPos, $this->_pos - $firstPos));
}
if (strlen($key) == 0) {
// Empty key name.
throw $this->_createException(
$firstPos,
Resources::ERROR_CONNECTION_STRING_EMPTY_KEY
);
}
return $key;
}
/**
* Extracts the string until the given quotation mark.
*
* @param string $quote The quotation mark terminating the string
*
* @return string
*/
private function _extractString($quote)
{
$firstPos = $this->_pos;
while ($this->_pos < strlen($this->_value)
&& $this->_value[$this->_pos] != $quote
) {
++$this->_pos;
}
if ($this->_pos == strlen($this->_value)) {
// Runaway string.
throw $this->_createException(
$this->_pos,
Resources::ERROR_CONNECTION_STRING_MISSING_CHARACTER,
$quote
);
}
return substr($this->_value, $firstPos, $this->_pos++ - $firstPos);
}
/**
* Skips specified operator.
*
* @param string $operatorChar The operator character
*
* @throws \RuntimeException
*/
private function _skipOperator($operatorChar)
{
if ($this->_value[$this->_pos] != $operatorChar) {
// Character was expected.
throw $this->_createException(
$this->_pos,
Resources::MISSING_CONNECTION_STRING_CHAR,
$operatorChar
);
}
++$this->_pos;
}
}