Export.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace RocketTheme\Toolbox\ArrayTraits;
  3. use Symfony\Component\Yaml\Exception\DumpException;
  4. use Symfony\Component\Yaml\Yaml;
  5. /**
  6. * Implements ExportInterface.
  7. *
  8. * @package RocketTheme\Toolbox\ArrayTraits
  9. * @author RocketTheme
  10. * @license MIT
  11. *
  12. * @property array $items
  13. */
  14. trait Export
  15. {
  16. /**
  17. * Convert object into an array.
  18. *
  19. * @return array
  20. */
  21. public function toArray()
  22. {
  23. return $this->items;
  24. }
  25. /**
  26. * Convert object into YAML string.
  27. *
  28. * @param int $inline The level where you switch to inline YAML.
  29. * @param int $indent The amount of spaces to use for indentation of nested nodes.
  30. *
  31. * @return string A YAML string representing the object.
  32. * @throws DumpException
  33. */
  34. public function toYaml($inline = 3, $indent = 2)
  35. {
  36. return Yaml::dump($this->toArray(), $inline, $indent, true, false);
  37. }
  38. /**
  39. * Convert object into JSON string.
  40. *
  41. * @return string
  42. */
  43. public function toJson()
  44. {
  45. return json_encode($this->toArray());
  46. }
  47. }