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 DUMP_NUMERIC_KEY_AS_STRING = 4096;
38 public const DUMP_NULL_AS_EMPTY = 8192;
39 public const DUMP_COMPACT_NESTED_MAPPING = 16384;
40 public const DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES = 32768;
41 public const PARSE_EXCEPTION_ON_ALIAS = 65536;
42
43 /**
44 * Parses a YAML file into a PHP value.
45 *
46 * Usage:
47 *
48 * $array = Yaml::parseFile('config.yml');
49 * print_r($array);
50 *
51 * @param string $filename The path to the YAML file to be parsed
52 * @param int-mask-of<self::PARSE_*> $flags A bit field of PARSE_* constants to customize the YAML parser behavior
53 * @param int $maxNestingLevel The maximum nesting depth for nested YAML blocks
54 * @param int $maxAliasesForCollections The maximum number of collection aliases to resolve
55 *
56 * @throws ParseException If the file could not be read or the YAML is not valid
57 */
58 public static function parseFile(string $filename, int $flags = 0, int $maxNestingLevel = Parser::DEFAULT_MAX_NESTING_LEVEL, int $maxAliasesForCollections = Parser::DEFAULT_MAX_ALIASES_FOR_COLLECTIONS): mixed
59 {
60 $yaml = new Parser($maxNestingLevel, $maxAliasesForCollections);
61
62 return $yaml->parseFile($filename, $flags);
63 }
64
65 /**
66 * Parses YAML into a PHP value.
67 *
68 * Usage:
69 * <code>
70 * $array = Yaml::parse(file_get_contents('config.yml'));
71 * print_r($array);
72 * </code>
73 *
74 * @param string $input A string containing YAML
75 * @param int-mask-of<self::PARSE_*> $flags A bit field of PARSE_* constants to customize the YAML parser behavior
76 * @param int $maxNestingLevel The maximum nesting depth for nested YAML blocks
77 * @param int $maxAliasesForCollections The maximum number of collection aliases to resolve
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): mixed
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-mask-of<self::DUMP_*> $flags A bit field of DUMP_* constants to customize the dumped YAML string
98 */
99 public static function dump(mixed $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 }