TextBundleWriter.php 5.8 KB

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