Yaml.php 982 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * @package Grav.Common
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common;
  9. use Grav\Framework\File\Formatter\YamlFormatter;
  10. abstract class Yaml
  11. {
  12. /** @var YamlFormatter */
  13. private static $yaml;
  14. public static function parse($data)
  15. {
  16. if (null === static::$yaml) {
  17. static::init();
  18. }
  19. return static::$yaml->decode($data);
  20. }
  21. public static function dump($data, $inline = null, $indent = null)
  22. {
  23. if (null === static::$yaml) {
  24. static::init();
  25. }
  26. return static::$yaml->encode($data, $inline, $indent);
  27. }
  28. private static function init()
  29. {
  30. $config = [
  31. 'inline' => 5,
  32. 'indent' => 2,
  33. 'native' => true,
  34. 'compat' => true
  35. ];
  36. static::$yaml = new YamlFormatter($config);
  37. }
  38. }