TextBundleWriter.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 .txt resource bundles.
  13. *
  14. * The resulting files can be converted to binary .res files using a
  15. * {@link \Symfony\Component\Intl\ResourceBundle\Compiler\BundleCompilerInterface}
  16. * implementation.
  17. *
  18. * @author Bernhard Schussek <bschussek@gmail.com>
  19. *
  20. * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
  21. *
  22. * @internal
  23. */
  24. class TextBundleWriter implements BundleWriterInterface
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function write(string $path, string $locale, $data, bool $fallback = true)
  30. {
  31. $file = fopen($path.'/'.$locale.'.txt', 'w');
  32. $this->writeResourceBundle($file, $locale, $data, $fallback);
  33. fclose($file);
  34. }
  35. /**
  36. * Writes a "resourceBundle" node.
  37. *
  38. * @param resource $file The file handle to write to
  39. * @param mixed $value The value of the node
  40. *
  41. * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
  42. */
  43. private function writeResourceBundle($file, string $bundleName, $value, bool $fallback)
  44. {
  45. fwrite($file, $bundleName);
  46. $this->writeTable($file, $value, 0, $fallback);
  47. fwrite($file, "\n");
  48. }
  49. /**
  50. * Writes a "resource" node.
  51. *
  52. * @param resource $file The file handle to write to
  53. * @param mixed $value The value of the node
  54. *
  55. * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
  56. */
  57. private function writeResource($file, $value, int $indentation, bool $requireBraces = true)
  58. {
  59. if (\is_int($value)) {
  60. $this->writeInteger($file, $value);
  61. return;
  62. }
  63. if ($value instanceof \Traversable) {
  64. $value = iterator_to_array($value);
  65. }
  66. if (\is_array($value)) {
  67. $intValues = \count($value) === \count(array_filter($value, 'is_int'));
  68. $keys = array_keys($value);
  69. // check that the keys are 0-indexed and ascending
  70. $intKeys = $keys === range(0, \count($keys) - 1);
  71. if ($intValues && $intKeys) {
  72. $this->writeIntVector($file, $value, $indentation);
  73. return;
  74. }
  75. if ($intKeys) {
  76. $this->writeArray($file, $value, $indentation);
  77. return;
  78. }
  79. $this->writeTable($file, $value, $indentation);
  80. return;
  81. }
  82. if (\is_bool($value)) {
  83. $value = $value ? 'true' : 'false';
  84. }
  85. $this->writeString($file, (string) $value, $requireBraces);
  86. }
  87. /**
  88. * Writes an "integer" node.
  89. *
  90. * @param resource $file The file handle to write to
  91. *
  92. * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
  93. */
  94. private function writeInteger($file, int $value)
  95. {
  96. fprintf($file, ':int{%d}', $value);
  97. }
  98. /**
  99. * Writes an "intvector" node.
  100. *
  101. * @param resource $file The file handle to write to
  102. *
  103. * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
  104. */
  105. private function writeIntVector($file, array $value, int $indentation)
  106. {
  107. fwrite($file, ":intvector{\n");
  108. foreach ($value as $int) {
  109. fprintf($file, "%s%d,\n", str_repeat(' ', $indentation + 1), $int);
  110. }
  111. fprintf($file, '%s}', str_repeat(' ', $indentation));
  112. }
  113. /**
  114. * Writes a "string" node.
  115. *
  116. * @param resource $file The file handle to write to
  117. *
  118. * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
  119. */
  120. private function writeString($file, string $value, bool $requireBraces = true)
  121. {
  122. if ($requireBraces) {
  123. fprintf($file, '{"%s"}', $value);
  124. return;
  125. }
  126. fprintf($file, '"%s"', $value);
  127. }
  128. /**
  129. * Writes an "array" node.
  130. *
  131. * @param resource $file The file handle to write to
  132. *
  133. * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
  134. */
  135. private function writeArray($file, array $value, int $indentation)
  136. {
  137. fwrite($file, "{\n");
  138. foreach ($value as $entry) {
  139. fwrite($file, str_repeat(' ', $indentation + 1));
  140. $this->writeResource($file, $entry, $indentation + 1, false);
  141. fwrite($file, ",\n");
  142. }
  143. fprintf($file, '%s}', str_repeat(' ', $indentation));
  144. }
  145. /**
  146. * Writes a "table" node.
  147. *
  148. * @param resource $file The file handle to write to
  149. */
  150. private function writeTable($file, iterable $value, int $indentation, bool $fallback = true)
  151. {
  152. if (!$fallback) {
  153. fwrite($file, ':table(nofallback)');
  154. }
  155. fwrite($file, "{\n");
  156. foreach ($value as $key => $entry) {
  157. fwrite($file, str_repeat(' ', $indentation + 1));
  158. // escape colons, otherwise they are interpreted as resource types
  159. if (false !== strpos($key, ':') || false !== strpos($key, ' ')) {
  160. $key = '"'.$key.'"';
  161. }
  162. fwrite($file, $key);
  163. $this->writeResource($file, $entry, $indentation + 1);
  164. fwrite($file, "\n");
  165. }
  166. fprintf($file, '%s}', str_repeat(' ', $indentation));
  167. }
  168. }