1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\Yaml;
13:
14: use Symfony\Component\Yaml\Exception\ParseException;
15:
16: /**
17: * Yaml offers convenience methods to load and dump YAML.
18: *
19: * @author Fabien Potencier <fabien@symfony.com>
20: *
21: * @api
22: */
23: class Yaml
24: {
25: /**
26: * Parses YAML into a PHP array.
27: *
28: * The parse method, when supplied with a YAML stream (string or file),
29: * will do its best to convert YAML in a file into a PHP array.
30: *
31: * Usage:
32: * <code>
33: * $array = Yaml::parse('config.yml');
34: * print_r($array);
35: * </code>
36: *
37: * As this method accepts both plain strings and file names as an input,
38: * you must validate the input before calling this method. Passing a file
39: * as an input is a deprecated feature and will be removed in 3.0.
40: *
41: * @param string $input Path to a YAML file or a string containing YAML
42: * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
43: * @param bool $objectSupport True if object support is enabled, false otherwise
44: *
45: * @return array The YAML converted to a PHP array
46: *
47: * @throws ParseException If the YAML is not valid
48: *
49: * @api
50: */
51: public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false)
52: {
53: // if input is a file, process it
54: $file = '';
55: if (strpos($input, "\n") === false && is_file($input)) {
56: if (false === is_readable($input)) {
57: throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $input));
58: }
59:
60: $file = $input;
61: $input = file_get_contents($file);
62: }
63:
64: $yaml = new Parser();
65:
66: try {
67: return $yaml->parse($input, $exceptionOnInvalidType, $objectSupport);
68: } catch (ParseException $e) {
69: if ($file) {
70: $e->setParsedFile($file);
71: }
72:
73: throw $e;
74: }
75: }
76:
77: /**
78: * Dumps a PHP array to a YAML string.
79: *
80: * The dump method, when supplied with an array, will do its best
81: * to convert the array into friendly YAML.
82: *
83: * @param array $array PHP array
84: * @param int $inline The level where you switch to inline YAML
85: * @param int $indent The amount of spaces to use for indentation of nested nodes.
86: * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
87: * @param bool $objectSupport true if object support is enabled, false otherwise
88: *
89: * @return string A YAML string representing the original PHP array
90: *
91: * @api
92: */
93: public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
94: {
95: $yaml = new Dumper();
96: $yaml->setIndentation($indent);
97:
98: return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport);
99: }
100: }
101: