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