TextBundleWriter.php 7.0 KB

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