Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Yaml; Chris@0: Chris@0: use Symfony\Component\Yaml\Exception\ParseException; Chris@0: Chris@0: /** Chris@0: * Yaml offers convenience methods to load and dump YAML. Chris@0: * Chris@0: * @author Fabien Potencier Chris@14: * Chris@14: * @final since version 3.4 Chris@0: */ Chris@0: class Yaml Chris@0: { Chris@0: const DUMP_OBJECT = 1; Chris@0: const PARSE_EXCEPTION_ON_INVALID_TYPE = 2; Chris@0: const PARSE_OBJECT = 4; Chris@0: const PARSE_OBJECT_FOR_MAP = 8; Chris@0: const DUMP_EXCEPTION_ON_INVALID_TYPE = 16; Chris@0: const PARSE_DATETIME = 32; Chris@0: const DUMP_OBJECT_AS_MAP = 64; Chris@0: const DUMP_MULTI_LINE_LITERAL_BLOCK = 128; Chris@0: const PARSE_CONSTANT = 256; Chris@14: const PARSE_CUSTOM_TAGS = 512; Chris@14: const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024; Chris@14: Chris@14: /** Chris@14: * @deprecated since version 3.4, to be removed in 4.0. Quote your evaluable keys instead. Chris@14: */ Chris@14: const PARSE_KEYS_AS_STRINGS = 2048; Chris@14: Chris@14: /** Chris@14: * Parses a YAML file into a PHP value. Chris@14: * Chris@17: * Usage: Chris@17: * Chris@17: * $array = Yaml::parseFile('config.yml'); Chris@17: * print_r($array); Chris@14: * Chris@14: * @param string $filename The path to the YAML file to be parsed Chris@14: * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior Chris@14: * Chris@14: * @return mixed The YAML converted to a PHP value Chris@14: * Chris@14: * @throws ParseException If the file could not be read or the YAML is not valid Chris@14: */ Chris@14: public static function parseFile($filename, $flags = 0) Chris@14: { Chris@14: $yaml = new Parser(); Chris@14: Chris@14: return $yaml->parseFile($filename, $flags); Chris@14: } Chris@0: Chris@0: /** Chris@0: * Parses YAML into a PHP value. Chris@0: * Chris@0: * Usage: Chris@0: * Chris@0: * $array = Yaml::parse(file_get_contents('config.yml')); Chris@0: * print_r($array); Chris@0: * Chris@0: * Chris@0: * @param string $input A string containing YAML Chris@0: * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior Chris@0: * Chris@0: * @return mixed The YAML converted to a PHP value Chris@0: * Chris@0: * @throws ParseException If the YAML is not valid Chris@0: */ Chris@0: public static function parse($input, $flags = 0) Chris@0: { Chris@17: if (\is_bool($flags)) { Chris@14: @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the PARSE_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if ($flags) { Chris@0: $flags = self::PARSE_EXCEPTION_ON_INVALID_TYPE; Chris@0: } else { Chris@0: $flags = 0; Chris@0: } Chris@0: } Chris@0: Chris@17: if (\func_num_args() >= 3) { Chris@14: @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the PARSE_OBJECT flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if (func_get_arg(2)) { Chris@0: $flags |= self::PARSE_OBJECT; Chris@0: } Chris@0: } Chris@0: Chris@17: if (\func_num_args() >= 4) { Chris@14: @trigger_error('Passing a boolean flag to toggle object for map support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT_FOR_MAP flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if (func_get_arg(3)) { Chris@0: $flags |= self::PARSE_OBJECT_FOR_MAP; Chris@0: } Chris@0: } Chris@0: Chris@0: $yaml = new Parser(); Chris@0: Chris@0: return $yaml->parse($input, $flags); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Dumps a PHP value to a YAML string. Chris@0: * Chris@0: * The dump method, when supplied with an array, will do its best Chris@0: * to convert the array into friendly YAML. Chris@0: * Chris@0: * @param mixed $input The PHP value Chris@0: * @param int $inline The level where you switch to inline YAML Chris@0: * @param int $indent The amount of spaces to use for indentation of nested nodes Chris@0: * @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string Chris@0: * Chris@0: * @return string A YAML string representing the original PHP value Chris@0: */ Chris@0: public static function dump($input, $inline = 2, $indent = 4, $flags = 0) Chris@0: { Chris@17: if (\is_bool($flags)) { Chris@14: @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if ($flags) { Chris@0: $flags = self::DUMP_EXCEPTION_ON_INVALID_TYPE; Chris@0: } else { Chris@0: $flags = 0; Chris@0: } Chris@0: } Chris@0: Chris@17: if (\func_num_args() >= 5) { Chris@14: @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the DUMP_OBJECT flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if (func_get_arg(4)) { Chris@0: $flags |= self::DUMP_OBJECT; Chris@0: } Chris@0: } Chris@0: Chris@0: $yaml = new Dumper($indent); Chris@0: Chris@0: return $yaml->dump($input, $inline, 0, $flags); Chris@0: } Chris@0: }