Debian Codegraph @

vendor/symfony/yaml/Yaml.php

raw
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 <[email protected]>
20 *
21 * @final
22 */
23 class Yaml
24 {
25 public const DUMP_OBJECT = 1;
26 public const PARSE_EXCEPTION_ON_INVALID_TYPE = 2;
27 public const PARSE_OBJECT = 4;
28 public const PARSE_OBJECT_FOR_MAP = 8;
29 public const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
30 public const PARSE_DATETIME = 32;
31 public const DUMP_OBJECT_AS_MAP = 64;
32 public const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
33 public const PARSE_CONSTANT = 256;
34 public const PARSE_CUSTOM_TAGS = 512;
35 public const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
36 public const DUMP_NULL_AS_TILDE = 2048;
37 public const PARSE_EXCEPTION_ON_ALIAS = 8192;
38
39 /**
40 * Parses a YAML file into a PHP value.
41 *
42 * Usage:
43 *
44 * $array = Yaml::parseFile('config.yml');
45 * print_r($array);
46 *
47 * @param string $filename The path to the YAML file to be parsed
48 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
49 * @param int $maxNestingLevel The maximum nesting depth for nested YAML blocks
50 * @param int $maxAliasesForCollections The maximum number of collection aliases to resolve
51 *
52 * @return mixed
53 *
54 * @throws ParseException If the file could not be read or the YAML is not valid
55 */
56 public static function parseFile(string $filename, int $flags = 0, int $maxNestingLevel = Parser::DEFAULT_MAX_NESTING_LEVEL, int $maxAliasesForCollections = Parser::DEFAULT_MAX_ALIASES_FOR_COLLECTIONS)
57 {
58 $yaml = new Parser($maxNestingLevel, $maxAliasesForCollections);
59
60 return $yaml->parseFile($filename, $flags);
61 }
62
63 /**
64 * Parses YAML into a PHP value.
65 *
66 * Usage:
67 * <code>
68 * $array = Yaml::parse(file_get_contents('config.yml'));
69 * print_r($array);
70 * </code>
71 *
72 * @param string $input A string containing YAML
73 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
74 * @param int $maxNestingLevel The maximum nesting depth for nested YAML blocks
75 * @param int $maxAliasesForCollections The maximum number of collection aliases to resolve
76 *
77 * @return mixed
78 *
79 * @throws ParseException If the YAML is not valid
80 */
81 public static function parse(string $input, int $flags = 0, int $maxNestingLevel = Parser::DEFAULT_MAX_NESTING_LEVEL, int $maxAliasesForCollections = Parser::DEFAULT_MAX_ALIASES_FOR_COLLECTIONS)
82 {
83 $yaml = new Parser($maxNestingLevel, $maxAliasesForCollections);
84
85 return $yaml->parse($input, $flags);
86 }
87
88 /**
89 * Dumps a PHP value to a YAML string.
90 *
91 * The dump method, when supplied with an array, will do its best
92 * to convert the array into friendly YAML.
93 *
94 * @param mixed $input The PHP value
95 * @param int $inline The level where you switch to inline YAML
96 * @param int $indent The amount of spaces to use for indentation of nested nodes
97 * @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
98 */
99 public static function dump($input, int $inline = 2, int $indent = 4, int $flags = 0): string
100 {
101 $yaml = new Dumper($indent);
102
103 return $yaml->dump($input, $inline, 0, $flags);
104 }
105 }