MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
Inline.php
Ir para a documentação deste ficheiro.
1
<?php
2
3
namespace
Symfony\Component\Yaml
;
4
5
/*
6
* This file is part of the symfony package.
7
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
8
*
9
* For the full copyright and license information, please view the LICENSE
10
* file that was distributed with this source code.
11
*/
12
20
class
Inline
21
{
22
const
REGEX_QUOTED_STRING
=
'(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\']*(?:\'\'[^\']*)*)\')'
;
23
31
static
public
function
load
($value)
32
{
33
$value = trim($value);
34
35
if
(0 == strlen($value))
36
{
37
return
''
;
38
}
39
40
switch
($value[0])
41
{
42
case
'['
:
43
return
self::parseSequence
($value);
44
case
'{'
:
45
return
self::parseMapping
($value);
46
default
:
47
return
self::parseScalar
($value);
48
}
49
}
50
58
static
public
function
dump
($value)
59
{
60
$trueValues =
'1.1'
==
Yaml::getSpecVersion
() ? array(
'true'
,
'on'
,
'+'
,
'yes'
,
'y'
) : array(
'true'
);
61
$falseValues =
'1.1'
==
Yaml::getSpecVersion
() ? array(
'false'
,
'off'
,
'-'
,
'no'
,
'n'
) : array(
'false'
);
62
63
switch
(
true
)
64
{
65
case
is_resource($value):
66
throw
new
Exception
(
'Unable to dump PHP resources in a YAML file.'
);
67
case
is_object($value):
68
return
'!!php/object:'
.serialize($value);
69
case
is_array($value):
70
return
self::dumpArray
($value);
71
case
null
=== $value:
72
return
'null'
;
73
case
true
=== $value:
74
return
'true'
;
75
case
false
=== $value:
76
return
'false'
;
77
case
ctype_digit($value):
78
return
is_string($value) ?
"'$value'"
: (int) $value;
79
case
is_numeric($value):
80
return
is_infinite($value) ? str_ireplace(
'INF'
,
'.Inf'
, strval($value)) : (is_string($value) ?
"'$value'"
: $value);
81
case
false
!== strpos($value,
"\n"
) ||
false
!== strpos($value,
"\r"
):
82
return
sprintf(
'"%s"'
, str_replace(array(
'"'
,
"\n"
,
"\r"
), array(
'\\"'
,
'\n'
,
'\r'
), $value));
83
case
preg_match(
'/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ - ? | < > = ! % @ ` ]/x'
, $value):
84
return
sprintf(
"'%s'"
, str_replace(
'\''
,
'\'
\
''
, $value));
85
case
''
== $value:
86
return
"''"
;
87
case
preg_match(self::getTimestampRegex(), $value):
88
return
"'$value'"
;
89
case
in_array(strtolower($value), $trueValues):
90
return
"'$value'"
;
91
case
in_array(strtolower($value), $falseValues):
92
return
"'$value'"
;
93
case
in_array(strtolower($value), array(
'null'
,
'~'
)):
94
return
"'$value'"
;
95
default
:
96
return
$value;
97
}
98
}
99
107
static
protected
function
dumpArray
($value)
108
{
109
// array
110
$keys = array_keys($value);
111
if
(
112
(1 == count($keys) &&
'0'
== $keys[0])
113
||
114
(count($keys) > 1 && array_reduce($keys,
function
($v, $w) {
return
(integer) $v + $w; }, 0) == count($keys) * (count($keys) - 1) / 2))
115
{
116
$output = array();
117
foreach
($value as $val)
118
{
119
$output[] =
self::dump
($val);
120
}
121
122
return
sprintf(
'[%s]'
, implode(
', '
, $output));
123
}
124
125
// mapping
126
$output = array();
127
foreach
($value as $key => $val)
128
{
129
$output[] = sprintf(
'%s: %s'
, self::dump($key), self::dump($val));
130
}
131
132
return
sprintf(
'{ %s }'
, implode(
', '
, $output));
133
}
134
146
static
public
function
parseScalar
($scalar, $delimiters =
null
, $stringDelimiters = array(
'"'
,
"'"
), &$i = 0, $evaluate =
true
)
147
{
148
if
(in_array($scalar[$i], $stringDelimiters))
149
{
150
// quoted scalar
151
$output =
self::parseQuotedScalar
($scalar, $i);
152
}
153
else
154
{
155
// "normal" string
156
if
(!$delimiters)
157
{
158
$output = substr($scalar, $i);
159
$i += strlen($output);
160
161
// remove comments
162
if
(
false
!== $strpos = strpos($output,
' #'
))
163
{
164
$output = rtrim(substr($output, 0, $strpos));
165
}
166
}
167
else
if
(preg_match(
'/^(.+?)('
.implode(
'|'
, $delimiters).
')/'
, substr($scalar, $i), $match))
168
{
169
$output = $match[1];
170
$i += strlen($output);
171
}
172
else
173
{
174
throw
new
ParserException
(sprintf(
'Malformed inline YAML string (%s).'
, $scalar));
175
}
176
177
$output = $evaluate ?
self::evaluateScalar
($output) : $output;
178
}
179
180
return
$output;
181
}
182
191
static
protected
function
parseQuotedScalar
($scalar, &$i)
192
{
193
if
(!preg_match(
'/'
.self::REGEX_QUOTED_STRING.
'/A'
, substr($scalar, $i), $match))
194
{
195
throw
new
ParserException
(sprintf(
'Malformed inline YAML string (%s).'
, substr($scalar, $i)));
196
}
197
198
$output = substr($match[0], 1, strlen($match[0]) - 2);
199
200
if
(
'"'
== $scalar[$i])
201
{
202
// evaluate the string
203
$output = str_replace(array(
'\\"'
,
'\\n'
,
'\\r'
), array(
'"'
,
"\n"
,
"\r"
), $output);
204
}
205
else
206
{
207
// unescape '
208
$output = str_replace(
'\'
\
''
,
'\''
, $output);
209
}
210
211
$i += strlen($match[0]);
212
213
return
$output;
214
}
215
224
static
protected
function
parseSequence
($sequence, &$i = 0)
225
{
226
$output = array();
227
$len = strlen($sequence);
228
$i += 1;
229
230
// [foo, bar, ...]
231
while
($i < $len)
232
{
233
switch
($sequence[$i])
234
{
235
case
'['
:
236
// nested sequence
237
$output[] =
self::parseSequence
($sequence, $i);
238
break
;
239
case
'{'
:
240
// nested mapping
241
$output[] =
self::parseMapping
($sequence, $i);
242
break
;
243
case
']'
:
244
return
$output;
245
case
','
:
246
case
' '
:
247
break
;
248
default
:
249
$isQuoted = in_array($sequence[$i], array(
'"'
,
"'"
));
250
$value =
self::parseScalar
($sequence, array(
','
,
']'
), array(
'"'
,
"'"
), $i);
251
252
if
(!$isQuoted &&
false
!== strpos($value,
': '
))
253
{
254
// embedded mapping?
255
try
256
{
257
$value =
self::parseMapping
(
'{'
.$value.
'}'
);
258
}
259
catch
(\InvalidArgumentException $e)
260
{
261
// no, it's not
262
}
263
}
264
265
$output[] = $value;
266
267
--$i;
268
}
269
270
++$i;
271
}
272
273
throw
new
ParserException
(sprintf(
'Malformed inline YAML string %s'
, $sequence));
274
}
275
284
static
protected
function
parseMapping
($mapping, &$i = 0)
285
{
286
$output = array();
287
$len = strlen($mapping);
288
$i += 1;
289
290
// {foo: bar, bar:foo, ...}
291
while
($i < $len)
292
{
293
switch
($mapping[$i])
294
{
295
case
' '
:
296
case
','
:
297
++$i;
298
continue
2;
299
case
'}'
:
300
return
$output;
301
}
302
303
// key
304
$key =
self::parseScalar
($mapping, array(
':'
,
' '
), array(
'"'
,
"'"
), $i,
false
);
305
306
// value
307
$done =
false
;
308
while
($i < $len)
309
{
310
switch
($mapping[$i])
311
{
312
case
'['
:
313
// nested sequence
314
$output[$key] =
self::parseSequence
($mapping, $i);
315
$done =
true
;
316
break
;
317
case
'{'
:
318
// nested mapping
319
$output[$key] =
self::parseMapping
($mapping, $i);
320
$done =
true
;
321
break
;
322
case
':'
:
323
case
' '
:
324
break
;
325
default
:
326
$output[$key] =
self::parseScalar
($mapping, array(
','
,
'}'
), array(
'"'
,
"'"
), $i);
327
$done =
true
;
328
--$i;
329
}
330
331
++$i;
332
333
if
($done)
334
{
335
continue
2;
336
}
337
}
338
}
339
340
throw
new
ParserException
(sprintf(
'Malformed inline YAML string %s'
, $mapping));
341
}
342
350
static
protected
function
evaluateScalar
($scalar)
351
{
352
$scalar = trim($scalar);
353
354
$trueValues =
'1.1'
==
Yaml::getSpecVersion
() ? array(
'true'
,
'on'
,
'+'
,
'yes'
,
'y'
) : array(
'true'
);
355
$falseValues =
'1.1'
==
Yaml::getSpecVersion
() ? array(
'false'
,
'off'
,
'-'
,
'no'
,
'n'
) : array(
'false'
);
356
357
switch
(
true
)
358
{
359
case
'null'
== strtolower($scalar):
360
case
''
== $scalar:
361
case
'~'
== $scalar:
362
return
null
;
363
case
0 === strpos($scalar,
'!str'
):
364
return
(
string
) substr($scalar, 5);
365
case
0 === strpos($scalar,
'! '
):
366
return
intval(self::parseScalar(substr($scalar, 2)));
367
case
0 === strpos($scalar,
'!!php/object:'
):
368
return
unserialize(substr($scalar, 13));
369
case
ctype_digit($scalar):
370
$raw = $scalar;
371
$cast = intval($scalar);
372
return
'0'
== $scalar[0] ? octdec($scalar) : (((string) $raw == (
string
) $cast) ? $cast : $raw);
373
case
in_array(strtolower($scalar), $trueValues):
374
return
true
;
375
case
in_array(strtolower($scalar), $falseValues):
376
return
false
;
377
case
is_numeric($scalar):
378
return
'0x'
== $scalar[0].$scalar[1] ? hexdec($scalar) : floatval($scalar);
379
case
0 == strcasecmp($scalar,
'.inf'
):
380
case
0 == strcasecmp($scalar,
'.NaN'
):
381
return
-log(0);
382
case
0 == strcasecmp($scalar,
'-.inf'
):
383
return
log(0);
384
case
preg_match(
'/^(-|\+)?[0-9,]+(\.[0-9]+)?$/'
, $scalar):
385
return
floatval(str_replace(
','
,
''
, $scalar));
386
case
preg_match(self::getTimestampRegex(), $scalar):
387
return
strtotime($scalar);
388
default
:
389
return
(
string
) $scalar;
390
}
391
}
392
393
static
protected
function
getTimestampRegex
()
394
{
395
return
<<<EOF
396
~^
397
(?P<year>[0-9][0-9][0-9][0-9])
398
-(?P<month>[0-9][0-9]?)
399
-(?P<day>[0-9][0-9]?)
400
(?:(?:[Tt]|[ \t]+)
401
(?P<hour>[0-9][0-9]?)
402
:(?P<minute>[0-9][0-9])
403
:(?P<second>[0-9][0-9])
404
(?:\.(?P<fraction>[0-9]*))?
405
(?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
406
(?::(?P<tz_minute>[0-9][0-9]))?))?)?
407
$~x
408
EOF;
409
}
410
}
Symfony\Component\Yaml\Exception
Definição
Exception.php:22
Symfony\Component\Yaml\Inline
Definição
Inline.php:21
Symfony\Component\Yaml\Inline\dumpArray
static dumpArray($value)
Definição
Inline.php:107
Symfony\Component\Yaml\Inline\getTimestampRegex
static getTimestampRegex()
Definição
Inline.php:393
Symfony\Component\Yaml\Inline\parseQuotedScalar
static parseQuotedScalar($scalar, &$i)
Definição
Inline.php:191
Symfony\Component\Yaml\Inline\load
static load($value)
Definição
Inline.php:31
Symfony\Component\Yaml\Inline\evaluateScalar
static evaluateScalar($scalar)
Definição
Inline.php:350
Symfony\Component\Yaml\Inline\parseMapping
static parseMapping($mapping, &$i=0)
Definição
Inline.php:284
Symfony\Component\Yaml\Inline\parseSequence
static parseSequence($sequence, &$i=0)
Definição
Inline.php:224
Symfony\Component\Yaml\Inline\REGEX_QUOTED_STRING
const REGEX_QUOTED_STRING
Definição
Inline.php:22
Symfony\Component\Yaml\Inline\dump
static dump($value)
Definição
Inline.php:58
Symfony\Component\Yaml\Inline\parseScalar
static parseScalar($scalar, $delimiters=null, $stringDelimiters=array('"', "'"), &$i = 0, $evaluate = true)
Definição
Inline.php:146
Symfony\Component\Yaml\ParserException
Definição
ParserException.php:22
Symfony\Component\Yaml\Yaml\getSpecVersion
static getSpecVersion()
Definição
Yaml.php:44
Symfony\Component\Yaml
Definição
Dumper.php:3
classes
extensions
doctrine-dbal
Doctrine
Symfony
Component
Yaml
Inline.php
Gerado por
1.10.0