]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of the Symfony package. | |
5 | * | |
6 | * (c) Fabien Potencier <fabien@symfony.com> | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | namespace Symfony\Component\Intl\ResourceBundle\Writer; | |
13 | ||
14 | /** | |
15 | * Writes .txt resource bundles. | |
16 | * | |
17 | * The resulting files can be converted to binary .res files using the | |
18 | * {@link \Symfony\Component\Intl\ResourceBundle\Transformer\BundleCompiler}. | |
19 | * | |
20 | * @author Bernhard Schussek <bschussek@gmail.com> | |
21 | * | |
22 | * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt | |
23 | */ | |
24 | class TextBundleWriter implements BundleWriterInterface | |
25 | { | |
26 | /** | |
27 | * {@inheritdoc} | |
28 | */ | |
29 | public function write($path, $locale, $data) | |
30 | { | |
31 | $file = fopen($path.'/'.$locale.'.txt', 'w'); | |
32 | ||
33 | $this->writeResourceBundle($file, $locale, $data); | |
34 | ||
35 | fclose($file); | |
36 | } | |
37 | ||
38 | /** | |
39 | * Writes a "resourceBundle" node. | |
40 | * | |
41 | * @param resource $file The file handle to write to. | |
42 | * @param string $bundleName The name of the bundle. | |
43 | * @param mixed $value The value of the node. | |
44 | * | |
45 | * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt | |
46 | */ | |
47 | private function writeResourceBundle($file, $bundleName, $value) | |
48 | { | |
49 | fwrite($file, $bundleName); | |
50 | ||
51 | $this->writeTable($file, $value, 0); | |
52 | ||
53 | fwrite($file, "\n"); | |
54 | } | |
55 | ||
56 | /** | |
57 | * Writes a "resource" node. | |
58 | * | |
59 | * @param resource $file The file handle to write to. | |
60 | * @param mixed $value The value of the node. | |
61 | * @param integer $indentation The number of levels to indent. | |
62 | * @param Boolean $requireBraces Whether to require braces to be printed | |
63 | * around the value. | |
64 | * | |
65 | * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt | |
66 | */ | |
67 | private function writeResource($file, $value, $indentation, $requireBraces = true) | |
68 | { | |
69 | if (is_int($value)) { | |
70 | $this->writeInteger($file, $value); | |
71 | ||
72 | return; | |
73 | } | |
74 | ||
75 | if (is_array($value)) { | |
76 | if (count($value) === count(array_filter($value, 'is_int'))) { | |
77 | $this->writeIntVector($file, $value, $indentation); | |
78 | ||
79 | return; | |
80 | } | |
81 | ||
82 | $keys = array_keys($value); | |
83 | ||
84 | if (count($keys) === count(array_filter($keys, 'is_int'))) { | |
85 | $this->writeArray($file, $value, $indentation); | |
86 | ||
87 | return; | |
88 | } | |
89 | ||
90 | $this->writeTable($file, $value, $indentation); | |
91 | ||
92 | return; | |
93 | } | |
94 | ||
95 | if (is_bool($value)) { | |
96 | $value = $value ? 'true' : 'false'; | |
97 | } | |
98 | ||
99 | $this->writeString($file, (string) $value, $requireBraces); | |
100 | } | |
101 | ||
102 | /** | |
103 | * Writes an "integer" node. | |
104 | * | |
105 | * @param resource $file The file handle to write to. | |
106 | * @param integer $value The value of the node. | |
107 | * | |
108 | * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt | |
109 | */ | |
110 | private function writeInteger($file, $value) | |
111 | { | |
112 | fprintf($file, ':int{%d}', $value); | |
113 | } | |
114 | ||
115 | /** | |
116 | * Writes an "intvector" node. | |
117 | * | |
118 | * @param resource $file The file handle to write to. | |
119 | * @param array $value The value of the node. | |
120 | * @param integer $indentation The number of levels to indent. | |
121 | * | |
122 | * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt | |
123 | */ | |
124 | private function writeIntVector($file, array $value, $indentation) | |
125 | { | |
126 | fwrite($file, ":intvector{\n"); | |
127 | ||
128 | foreach ($value as $int) { | |
129 | fprintf($file, "%s%d,\n", str_repeat(' ', $indentation + 1), $int); | |
130 | } | |
131 | ||
132 | fprintf($file, "%s}", str_repeat(' ', $indentation)); | |
133 | } | |
134 | ||
135 | /** | |
136 | * Writes a "string" node. | |
137 | * | |
138 | * @param resource $file The file handle to write to. | |
139 | * @param string $value The value of the node. | |
140 | * @param Boolean $requireBraces Whether to require braces to be printed | |
141 | * around the value. | |
142 | * | |
143 | * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt | |
144 | */ | |
145 | private function writeString($file, $value, $requireBraces = true) | |
146 | { | |
147 | if ($requireBraces) { | |
148 | fprintf($file, '{"%s"}', $value); | |
149 | ||
150 | return; | |
151 | } | |
152 | ||
153 | fprintf($file, '"%s"', $value); | |
154 | } | |
155 | ||
156 | /** | |
157 | * Writes an "array" node. | |
158 | * | |
159 | * @param resource $file The file handle to write to. | |
160 | * @param array $value The value of the node. | |
161 | * @param integer $indentation The number of levels to indent. | |
162 | * | |
163 | * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt | |
164 | */ | |
165 | private function writeArray($file, array $value, $indentation) | |
166 | { | |
167 | fwrite($file, "{\n"); | |
168 | ||
169 | foreach ($value as $entry) { | |
170 | fwrite($file, str_repeat(' ', $indentation + 1)); | |
171 | ||
172 | $this->writeResource($file, $entry, $indentation + 1, false); | |
173 | ||
174 | fwrite($file, ",\n"); | |
175 | } | |
176 | ||
177 | fprintf($file, '%s}', str_repeat(' ', $indentation)); | |
178 | } | |
179 | ||
180 | /** | |
181 | * Writes a "table" node. | |
182 | * | |
183 | * @param resource $file The file handle to write to. | |
184 | * @param array $value The value of the node. | |
185 | * @param integer $indentation The number of levels to indent. | |
186 | */ | |
187 | private function writeTable($file, array $value, $indentation) | |
188 | { | |
189 | fwrite($file, "{\n"); | |
190 | ||
191 | foreach ($value as $key => $entry) { | |
192 | fwrite($file, str_repeat(' ', $indentation + 1)); | |
193 | fwrite($file, $key); | |
194 | ||
195 | $this->writeResource($file, $entry, $indentation + 1); | |
196 | ||
197 | fwrite($file, "\n"); | |
198 | } | |
199 | ||
200 | fprintf($file, '%s}', str_repeat(' ', $indentation)); | |
201 | } | |
202 | } |