Yaml.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml;
  11. use Symfony\Component\Yaml\Exception\ParseException;
  12. /**
  13. * Yaml offers convenience methods to load and dump YAML.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @final
  18. */
  19. class Yaml
  20. {
  21. public const DUMP_OBJECT = 1;
  22. public const PARSE_EXCEPTION_ON_INVALID_TYPE = 2;
  23. public const PARSE_OBJECT = 4;
  24. public const PARSE_OBJECT_FOR_MAP = 8;
  25. public const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
  26. public const PARSE_DATETIME = 32;
  27. public const DUMP_OBJECT_AS_MAP = 64;
  28. public const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
  29. public const PARSE_CONSTANT = 256;
  30. public const PARSE_CUSTOM_TAGS = 512;
  31. public const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
  32. public const DUMP_NULL_AS_TILDE = 2048;
  33. public const DUMP_NUMERIC_KEY_AS_STRING = 4096;
  34. public const DUMP_NULL_AS_EMPTY = 8192;
  35. public const DUMP_COMPACT_NESTED_MAPPING = 16384;
  36. public const DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES = 32768;
  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-mask-of<self::PARSE_*> $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  47. *
  48. * @throws ParseException If the file could not be read or the YAML is not valid
  49. */
  50. public static function parseFile(string $filename, int $flags = 0): mixed
  51. {
  52. $yaml = new Parser();
  53. return $yaml->parseFile($filename, $flags);
  54. }
  55. /**
  56. * Parses YAML into a PHP value.
  57. *
  58. * Usage:
  59. * <code>
  60. * $array = Yaml::parse(file_get_contents('config.yml'));
  61. * print_r($array);
  62. * </code>
  63. *
  64. * @param string $input A string containing YAML
  65. * @param int-mask-of<self::PARSE_*> $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  66. *
  67. * @throws ParseException If the YAML is not valid
  68. */
  69. public static function parse(string $input, int $flags = 0): mixed
  70. {
  71. $yaml = new Parser();
  72. return $yaml->parse($input, $flags);
  73. }
  74. /**
  75. * Dumps a PHP value to a YAML string.
  76. *
  77. * The dump method, when supplied with an array, will do its best
  78. * to convert the array into friendly YAML.
  79. *
  80. * @param mixed $input The PHP value
  81. * @param int $inline The level where you switch to inline YAML
  82. * @param int $indent The amount of spaces to use for indentation of nested nodes
  83. * @param int-mask-of<self::DUMP_*> $flags A bit field of DUMP_* constants to customize the dumped YAML string
  84. */
  85. public static function dump(mixed $input, int $inline = 2, int $indent = 4, int $flags = 0): string
  86. {
  87. $yaml = new Dumper($indent);
  88. return $yaml->dump($input, $inline, 0, $flags);
  89. }
  90. }