AbstractFormatter.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @package Grav\Framework\File\Formatter
  5. *
  6. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  7. * @license MIT License; see LICENSE file for details.
  8. */
  9. namespace Grav\Framework\File\Formatter;
  10. use Grav\Framework\File\Interfaces\FileFormatterInterface;
  11. /**
  12. * Abstract file formatter.
  13. *
  14. * @package Grav\Framework\File\Formatter
  15. */
  16. abstract class AbstractFormatter implements FileFormatterInterface
  17. {
  18. /** @var array */
  19. private $config;
  20. /**
  21. * IniFormatter constructor.
  22. * @param array $config
  23. */
  24. public function __construct(array $config = [])
  25. {
  26. $this->config = $config;
  27. }
  28. /**
  29. * @return string
  30. */
  31. public function serialize(): string
  32. {
  33. return serialize($this->doSerialize());
  34. }
  35. /**
  36. * @param string $serialized
  37. */
  38. public function unserialize($serialized): void
  39. {
  40. $this->doUnserialize(unserialize($serialized, ['allowed_classes' => false]));
  41. }
  42. /**
  43. * {@inheritdoc}
  44. * @see FileFormatterInterface::getDefaultFileExtension()
  45. */
  46. public function getDefaultFileExtension(): string
  47. {
  48. $extensions = $this->getSupportedFileExtensions();
  49. // Call fails on bad configuration.
  50. return reset($extensions);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. * @see FileFormatterInterface::getSupportedFileExtensions()
  55. */
  56. public function getSupportedFileExtensions(): array
  57. {
  58. $extensions = $this->getConfig('file_extension');
  59. // Call fails on bad configuration.
  60. return \is_string($extensions) ? [$extensions] : $extensions;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. * @see FileFormatterInterface::encode()
  65. */
  66. abstract public function encode($data): string;
  67. /**
  68. * {@inheritdoc}
  69. * @see FileFormatterInterface::decode()
  70. */
  71. abstract public function decode($data);
  72. /**
  73. * Get either full configuration or a single option.
  74. *
  75. * @param string|null $name Configuration option (optional)
  76. * @return mixed
  77. */
  78. protected function getConfig(string $name = null)
  79. {
  80. if (null !== $name) {
  81. return $this->config[$name] ?? null;
  82. }
  83. return $this->config;
  84. }
  85. /**
  86. * @return array
  87. */
  88. protected function doSerialize(): array
  89. {
  90. return ['config' => $this->config];
  91. }
  92. /**
  93. * Note: if overridden, make sure you call parent::doUnserialize()
  94. *
  95. * @param array $serialized
  96. */
  97. protected function doUnserialize(array $serialized): void
  98. {
  99. $this->config = $serialized['config'];
  100. }
  101. }