JsonBundleWriter.php 1021 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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\Intl\Data\Bundle\Writer;
  11. /**
  12. * Writes .json resource bundles.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. *
  16. * @internal
  17. */
  18. class JsonBundleWriter implements BundleWriterInterface
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function write(string $path, string $locale, $data)
  24. {
  25. if ($data instanceof \Traversable) {
  26. $data = iterator_to_array($data);
  27. }
  28. array_walk_recursive($data, function (&$value) {
  29. if ($value instanceof \Traversable) {
  30. $value = iterator_to_array($value);
  31. }
  32. });
  33. $contents = json_encode($data, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE)."\n";
  34. file_put_contents($path.'/'.$locale.'.json', $contents);
  35. }
  36. }