composer package updates
[openemr.git] / vendor / symfony / yaml / Yaml.php
blob595fbef07f8894f8f34b6c22bea1b1ba8813a912
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Yaml;
14 use Symfony\Component\Yaml\Exception\ParseException;
16 /**
17 * Yaml offers convenience methods to load and dump YAML.
19 * @author Fabien Potencier <fabien@symfony.com>
21 class Yaml
23 /**
24 * Parses YAML into a PHP value.
26 * Usage:
27 * <code>
28 * $array = Yaml::parse(file_get_contents('config.yml'));
29 * print_r($array);
30 * </code>
32 * As this method accepts both plain strings and file names as an input,
33 * you must validate the input before calling this method. Passing a file
34 * as an input is a deprecated feature and will be removed in 3.0.
36 * Note: the ability to pass file names to the Yaml::parse method is deprecated since Symfony 2.2 and will be removed in 3.0. Pass the YAML contents of the file instead.
38 * @param string $input Path to a YAML file or a string containing YAML
39 * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
40 * @param bool $objectSupport True if object support is enabled, false otherwise
41 * @param bool $objectForMap True if maps should return a stdClass instead of array()
43 * @return mixed The YAML converted to a PHP value
45 * @throws ParseException If the YAML is not valid
47 public static function parse($input, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
49 // if input is a file, process it
50 $file = '';
51 if (false === strpos($input, "\n") && is_file($input)) {
52 @trigger_error('The ability to pass file names to the '.__METHOD__.' method is deprecated since Symfony 2.2 and will be removed in 3.0. Pass the YAML contents of the file instead.', E_USER_DEPRECATED);
54 if (false === is_readable($input)) {
55 throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $input));
58 $file = $input;
59 $input = file_get_contents($file);
62 $yaml = new Parser();
64 try {
65 return $yaml->parse($input, $exceptionOnInvalidType, $objectSupport, $objectForMap);
66 } catch (ParseException $e) {
67 if ($file) {
68 $e->setParsedFile($file);
71 throw $e;
75 /**
76 * Dumps a PHP value to a YAML string.
78 * The dump method, when supplied with an array, will do its best
79 * to convert the array into friendly YAML.
81 * @param mixed $input The PHP value
82 * @param int $inline The level where you switch to inline YAML
83 * @param int $indent The amount of spaces to use for indentation of nested nodes
84 * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
85 * @param bool $objectSupport True if object support is enabled, false otherwise
87 * @return string A YAML string representing the original PHP value
89 public static function dump($input, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
91 if ($indent < 1) {
92 throw new \InvalidArgumentException('The indentation must be greater than zero.');
95 $yaml = new Dumper();
96 $yaml->setIndentation($indent);
98 return $yaml->dump($input, $inline, 0, $exceptionOnInvalidType, $objectSupport);