1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Symfony\Component\Yaml;
12:
13: use Symfony\Component\Yaml\Exception\ParseException;
14: use Symfony\Component\Yaml\Exception\DumpException;
15:
16: 17: 18: 19: 20:
21: class Inline
22: {
23: const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\']*(?:\'\'[^\']*)*)\')';
24:
25: private static $exceptionOnInvalidType = false;
26: private static $objectSupport = false;
27:
28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38:
39: public static function parse($value, $exceptionOnInvalidType = false, $objectSupport = false)
40: {
41: self::$exceptionOnInvalidType = $exceptionOnInvalidType;
42: self::$objectSupport = $objectSupport;
43:
44: $value = trim($value);
45:
46: if (0 == strlen($value)) {
47: return '';
48: }
49:
50: if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
51: $mbEncoding = mb_internal_encoding();
52: mb_internal_encoding('ASCII');
53: }
54:
55: $i = 0;
56: switch ($value[0]) {
57: case '[':
58: $result = self::parseSequence($value, $i);
59: ++$i;
60: break;
61: case '{':
62: $result = self::parseMapping($value, $i);
63: ++$i;
64: break;
65: default:
66: $result = self::parseScalar($value, null, array('"', "'"), $i);
67: }
68:
69:
70: if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) {
71: throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)));
72: }
73:
74: if (isset($mbEncoding)) {
75: mb_internal_encoding($mbEncoding);
76: }
77:
78: return $result;
79: }
80:
81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91:
92: public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false)
93: {
94: switch (true) {
95: case is_resource($value):
96: if ($exceptionOnInvalidType) {
97: throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
98: }
99:
100: return 'null';
101: case is_object($value):
102: if ($objectSupport) {
103: return '!!php/object:'.serialize($value);
104: }
105:
106: if ($exceptionOnInvalidType) {
107: throw new DumpException('Object support when dumping a YAML file has been disabled.');
108: }
109:
110: return 'null';
111: case is_array($value):
112: return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport);
113: case null === $value:
114: return 'null';
115: case true === $value:
116: return 'true';
117: case false === $value:
118: return 'false';
119: case ctype_digit($value):
120: return is_string($value) ? "'$value'" : (int) $value;
121: case is_numeric($value):
122: $locale = setlocale(LC_NUMERIC, 0);
123: if (false !== $locale) {
124: setlocale(LC_NUMERIC, 'C');
125: }
126: $repr = is_string($value) ? "'$value'" : (is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : strval($value));
127:
128: if (false !== $locale) {
129: setlocale(LC_NUMERIC, $locale);
130: }
131:
132: return $repr;
133: case Escaper::requiresDoubleQuoting($value):
134: return Escaper::escapeWithDoubleQuotes($value);
135: case Escaper::requiresSingleQuoting($value):
136: return Escaper::escapeWithSingleQuotes($value);
137: case '' == $value:
138: return "''";
139: case preg_match(self::getTimestampRegex(), $value):
140: case in_array(strtolower($value), array('null', '~', 'true', 'false')):
141: return "'$value'";
142: default:
143: return $value;
144: }
145: }
146:
147: 148: 149: 150: 151: 152: 153: 154: 155:
156: private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport)
157: {
158:
159: $keys = array_keys($value);
160: if ((1 == count($keys) && '0' == $keys[0])
161: || (count($keys) > 1 && array_reduce($keys, function ($v, $w) { return (integer) $v + $w; }, 0) == count($keys) * (count($keys) - 1) / 2)
162: ) {
163: $output = array();
164: foreach ($value as $val) {
165: $output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport);
166: }
167:
168: return sprintf('[%s]', implode(', ', $output));
169: }
170:
171:
172: $output = array();
173: foreach ($value as $key => $val) {
174: $output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport));
175: }
176:
177: return sprintf('{ %s }', implode(', ', $output));
178: }
179:
180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192:
193: public static function parseScalar($scalar, $delimiters = null, $stringDelimiters = array('"', "'"), &$i = 0, $evaluate = true)
194: {
195: if (in_array($scalar[$i], $stringDelimiters)) {
196:
197: $output = self::parseQuotedScalar($scalar, $i);
198:
199: if (null !== $delimiters) {
200: $tmp = ltrim(substr($scalar, $i), ' ');
201: if (!in_array($tmp[0], $delimiters)) {
202: throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)));
203: }
204: }
205: } else {
206:
207: if (!$delimiters) {
208: $output = substr($scalar, $i);
209: $i += strlen($output);
210:
211:
212: if (false !== $strpos = strpos($output, ' #')) {
213: $output = rtrim(substr($output, 0, $strpos));
214: }
215: } elseif (preg_match('/^(.+?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) {
216: $output = $match[1];
217: $i += strlen($output);
218: } else {
219: throw new ParseException(sprintf('Malformed inline YAML string (%s).', $scalar));
220: }
221:
222: $output = $evaluate ? self::evaluateScalar($output) : $output;
223: }
224:
225: return $output;
226: }
227:
228: 229: 230: 231: 232: 233: 234: 235: 236: 237:
238: private static function parseQuotedScalar($scalar, &$i)
239: {
240: if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
241: throw new ParseException(sprintf('Malformed inline YAML string (%s).', substr($scalar, $i)));
242: }
243:
244: $output = substr($match[0], 1, strlen($match[0]) - 2);
245:
246: $unescaper = new Unescaper();
247: if ('"' == $scalar[$i]) {
248: $output = $unescaper->unescapeDoubleQuotedString($output);
249: } else {
250: $output = $unescaper->unescapeSingleQuotedString($output);
251: }
252:
253: $i += strlen($match[0]);
254:
255: return $output;
256: }
257:
258: 259: 260: 261: 262: 263: 264: 265: 266: 267:
268: private static function parseSequence($sequence, &$i = 0)
269: {
270: $output = array();
271: $len = strlen($sequence);
272: $i += 1;
273:
274:
275: while ($i < $len) {
276: switch ($sequence[$i]) {
277: case '[':
278:
279: $output[] = self::parseSequence($sequence, $i);
280: break;
281: case '{':
282:
283: $output[] = self::parseMapping($sequence, $i);
284: break;
285: case ']':
286: return $output;
287: case ',':
288: case ' ':
289: break;
290: default:
291: $isQuoted = in_array($sequence[$i], array('"', "'"));
292: $value = self::parseScalar($sequence, array(',', ']'), array('"', "'"), $i);
293:
294: if (!$isQuoted && false !== strpos($value, ': ')) {
295:
296: try {
297: $value = self::parseMapping('{'.$value.'}');
298: } catch (\InvalidArgumentException $e) {
299:
300: }
301: }
302:
303: $output[] = $value;
304:
305: --$i;
306: }
307:
308: ++$i;
309: }
310:
311: throw new ParseException(sprintf('Malformed inline YAML string %s', $sequence));
312: }
313:
314: 315: 316: 317: 318: 319: 320: 321: 322: 323:
324: private static function parseMapping($mapping, &$i = 0)
325: {
326: $output = array();
327: $len = strlen($mapping);
328: $i += 1;
329:
330:
331: while ($i < $len) {
332: switch ($mapping[$i]) {
333: case ' ':
334: case ',':
335: ++$i;
336: continue 2;
337: case '}':
338: return $output;
339: }
340:
341:
342: $key = self::parseScalar($mapping, array(':', ' '), array('"', "'"), $i, false);
343:
344:
345: $done = false;
346: while ($i < $len) {
347: switch ($mapping[$i]) {
348: case '[':
349:
350: $output[$key] = self::parseSequence($mapping, $i);
351: $done = true;
352: break;
353: case '{':
354:
355: $output[$key] = self::parseMapping($mapping, $i);
356: $done = true;
357: break;
358: case ':':
359: case ' ':
360: break;
361: default:
362: $output[$key] = self::parseScalar($mapping, array(',', '}'), array('"', "'"), $i);
363: $done = true;
364: --$i;
365: }
366:
367: ++$i;
368:
369: if ($done) {
370: continue 2;
371: }
372: }
373: }
374:
375: throw new ParseException(sprintf('Malformed inline YAML string %s', $mapping));
376: }
377:
378: 379: 380: 381: 382: 383: 384:
385: private static function evaluateScalar($scalar)
386: {
387: $scalar = trim($scalar);
388:
389: switch (true) {
390: case 'null' == strtolower($scalar):
391: case '' == $scalar:
392: case '~' == $scalar:
393: return null;
394: case 0 === strpos($scalar, '!str'):
395: return (string) substr($scalar, 5);
396: case 0 === strpos($scalar, '! '):
397: return intval(self::parseScalar(substr($scalar, 2)));
398: case 0 === strpos($scalar, '!!php/object:'):
399: if (self::$objectSupport) {
400: return unserialize(substr($scalar, 13));
401: }
402:
403: if (self::$exceptionOnInvalidType) {
404: throw new ParseException('Object support when parsing a YAML file has been disabled.');
405: }
406:
407: return null;
408: case ctype_digit($scalar):
409: $raw = $scalar;
410: $cast = intval($scalar);
411:
412: return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
413: case '-' === $scalar[0] && ctype_digit(substr($scalar, 1)):
414: $raw = $scalar;
415: $cast = intval($scalar);
416:
417: return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
418: case 'true' === strtolower($scalar):
419: return true;
420: case 'false' === strtolower($scalar):
421: return false;
422: case is_numeric($scalar):
423: return '0x' == $scalar[0].$scalar[1] ? hexdec($scalar) : floatval($scalar);
424: case 0 == strcasecmp($scalar, '.inf'):
425: case 0 == strcasecmp($scalar, '.NaN'):
426: return -log(0);
427: case 0 == strcasecmp($scalar, '-.inf'):
428: return log(0);
429: case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
430: return floatval(str_replace(',', '', $scalar));
431: case preg_match(self::getTimestampRegex(), $scalar):
432: return strtotime($scalar);
433: default:
434: return (string) $scalar;
435: }
436: }
437:
438: 439: 440: 441: 442: 443: 444:
445: private static function getTimestampRegex()
446: {
447: return <<<EOF
448: ~^
449: (?P<year>[0-9][0-9][0-9][0-9])
450: -(?P<month>[0-9][0-9]?)
451: -(?P<day>[0-9][0-9]?)
452: (?:(?:[Tt]|[ \t]+)
453: (?P<hour>[0-9][0-9]?)
454: :(?P<minute>[0-9][0-9])
455: :(?P<second>[0-9][0-9])
456: (?:\.(?P<fraction>[0-9]*))?
457: (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
458: (?::(?P<tz_minute>[0-9][0-9]))?))?)?
459: $~x
460: EOF;
461: }
462: }
463: