]>
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\Collator; | |
13 | ||
14 | use Symfony\Component\Intl\Exception\MethodNotImplementedException; | |
15 | use Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException; | |
16 | use Symfony\Component\Intl\Globals\IntlGlobals; | |
17 | use Symfony\Component\Intl\Locale\Locale; | |
18 | ||
19 | /** | |
20 | * Replacement for PHP's native {@link \Collator} class. | |
21 | * | |
22 | * The only methods currently supported in this class are: | |
23 | * | |
24 | * - {@link \__construct} | |
25 | * - {@link create} | |
26 | * - {@link asort} | |
27 | * - {@link getErrorCode} | |
28 | * - {@link getErrorMessage} | |
29 | * - {@link getLocale} | |
30 | * | |
31 | * @author Igor Wiedler <igor@wiedler.ch> | |
32 | * @author Bernhard Schussek <bschussek@gmail.com> | |
33 | */ | |
34 | class Collator | |
35 | { | |
36 | /* Attribute constants */ | |
37 | const FRENCH_COLLATION = 0; | |
38 | const ALTERNATE_HANDLING = 1; | |
39 | const CASE_FIRST = 2; | |
40 | const CASE_LEVEL = 3; | |
41 | const NORMALIZATION_MODE = 4; | |
42 | const STRENGTH = 5; | |
43 | const HIRAGANA_QUATERNARY_MODE = 6; | |
44 | const NUMERIC_COLLATION = 7; | |
45 | ||
46 | /* Attribute constants values */ | |
47 | const DEFAULT_VALUE = -1; | |
48 | ||
49 | const PRIMARY = 0; | |
50 | const SECONDARY = 1; | |
51 | const TERTIARY = 2; | |
52 | const DEFAULT_STRENGTH = 2; | |
53 | const QUATERNARY = 3; | |
54 | const IDENTICAL = 15; | |
55 | ||
56 | const OFF = 16; | |
57 | const ON = 17; | |
58 | ||
59 | const SHIFTED = 20; | |
60 | const NON_IGNORABLE = 21; | |
61 | ||
62 | const LOWER_FIRST = 24; | |
63 | const UPPER_FIRST = 25; | |
64 | ||
65 | /* Sorting options */ | |
66 | const SORT_REGULAR = 0; | |
67 | const SORT_NUMERIC = 2; | |
68 | const SORT_STRING = 1; | |
69 | ||
70 | /** | |
71 | * Constructor | |
72 | * | |
73 | * @param string $locale The locale code. The only currently supported locale is "en". | |
74 | * | |
75 | * @throws MethodArgumentValueNotImplementedException When $locale different than "en" is passed | |
76 | */ | |
77 | public function __construct($locale) | |
78 | { | |
79 | if ('en' != $locale) { | |
80 | throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the locale "en" is supported'); | |
81 | } | |
82 | } | |
83 | ||
84 | /** | |
85 | * Static constructor | |
86 | * | |
87 | * @param string $locale The locale code. The only currently supported locale is "en". | |
88 | * | |
89 | * @return Collator | |
90 | * | |
91 | * @throws MethodArgumentValueNotImplementedException When $locale different than "en" is passed | |
92 | */ | |
93 | public static function create($locale) | |
94 | { | |
95 | return new self($locale); | |
96 | } | |
97 | ||
98 | /** | |
99 | * Sort array maintaining index association | |
100 | * | |
101 | * @param array &$array Input array | |
102 | * @param integer $sortFlag Flags for sorting, can be one of the following: | |
103 | * Collator::SORT_REGULAR - compare items normally (don't change types) | |
104 | * Collator::SORT_NUMERIC - compare items numerically | |
105 | * Collator::SORT_STRING - compare items as strings | |
106 | * | |
107 | * @return Boolean True on success or false on failure | |
108 | */ | |
109 | public function asort(&$array, $sortFlag = self::SORT_REGULAR) | |
110 | { | |
111 | $intlToPlainFlagMap = array( | |
112 | self::SORT_REGULAR => \SORT_REGULAR, | |
113 | self::SORT_NUMERIC => \SORT_NUMERIC, | |
114 | self::SORT_STRING => \SORT_STRING, | |
115 | ); | |
116 | ||
117 | $plainSortFlag = isset($intlToPlainFlagMap[$sortFlag]) ? $intlToPlainFlagMap[$sortFlag] : self::SORT_REGULAR; | |
118 | ||
119 | return asort($array, $plainSortFlag); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Not supported. Compare two Unicode strings | |
124 | * | |
125 | * @param string $str1 The first string to compare | |
126 | * @param string $str2 The second string to compare | |
127 | * | |
128 | * @return Boolean|int Return the comparison result or false on failure: | |
129 | * 1 if $str1 is greater than $str2 | |
130 | * 0 if $str1 is equal than $str2 | |
131 | * -1 if $str1 is less than $str2 | |
132 | * | |
133 | * @see http://www.php.net/manual/en/collator.compare.php | |
134 | * | |
135 | * @throws MethodNotImplementedException | |
136 | */ | |
137 | public function compare($str1, $str2) | |
138 | { | |
139 | throw new MethodNotImplementedException(__METHOD__); | |
140 | } | |
141 | ||
142 | /** | |
143 | * Not supported. Get a value of an integer collator attribute | |
144 | * | |
145 | * @param int $attr An attribute specifier, one of the attribute constants | |
146 | * | |
147 | * @return Boolean|int The attribute value on success or false on error | |
148 | * | |
149 | * @see http://www.php.net/manual/en/collator.getattribute.php | |
150 | * | |
151 | * @throws MethodNotImplementedException | |
152 | */ | |
153 | public function getAttribute($attr) | |
154 | { | |
155 | throw new MethodNotImplementedException(__METHOD__); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Returns collator's last error code. Always returns the U_ZERO_ERROR class constant value | |
160 | * | |
161 | * @return int The error code from last collator call | |
162 | */ | |
163 | public function getErrorCode() | |
164 | { | |
165 | return IntlGlobals::U_ZERO_ERROR; | |
166 | } | |
167 | ||
168 | /** | |
169 | * Returns collator's last error message. Always returns the U_ZERO_ERROR_MESSAGE class constant value | |
170 | * | |
171 | * @return string The error message from last collator call | |
172 | */ | |
173 | public function getErrorMessage() | |
174 | { | |
175 | return 'U_ZERO_ERROR'; | |
176 | } | |
177 | ||
178 | /** | |
179 | * Returns the collator's locale | |
180 | * | |
181 | * @param int $type Not supported. The locale name type to return (Locale::VALID_LOCALE or Locale::ACTUAL_LOCALE) | |
182 | * | |
183 | * @return string The locale used to create the collator. Currently always | |
184 | * returns "en". | |
185 | */ | |
186 | public function getLocale($type = Locale::ACTUAL_LOCALE) | |
187 | { | |
188 | return 'en'; | |
189 | } | |
190 | ||
191 | /** | |
192 | * Not supported. Get sorting key for a string | |
193 | * | |
194 | * @param string $string The string to produce the key from | |
195 | * | |
196 | * @return string The collation key for $string | |
197 | * | |
198 | * @see http://www.php.net/manual/en/collator.getsortkey.php | |
199 | * | |
200 | * @throws MethodNotImplementedException | |
201 | */ | |
202 | public function getSortKey($string) | |
203 | { | |
204 | throw new MethodNotImplementedException(__METHOD__); | |
205 | } | |
206 | ||
207 | /** | |
208 | * Not supported. Get current collator's strength | |
209 | * | |
210 | * @return Boolean|int The current collator's strength or false on failure | |
211 | * | |
212 | * @see http://www.php.net/manual/en/collator.getstrength.php | |
213 | * | |
214 | * @throws MethodNotImplementedException | |
215 | */ | |
216 | public function getStrength() | |
217 | { | |
218 | throw new MethodNotImplementedException(__METHOD__); | |
219 | } | |
220 | ||
221 | /** | |
222 | * Not supported. Set a collator's attribute | |
223 | * | |
224 | * @param int $attr An attribute specifier, one of the attribute constants | |
225 | * @param int $val The attribute value, one of the attribute value constants | |
226 | * | |
227 | * @return Boolean True on success or false on failure | |
228 | * | |
229 | * @see http://www.php.net/manual/en/collator.setattribute.php | |
230 | * | |
231 | * @throws MethodNotImplementedException | |
232 | */ | |
233 | public function setAttribute($attr, $val) | |
234 | { | |
235 | throw new MethodNotImplementedException(__METHOD__); | |
236 | } | |
237 | ||
238 | /** | |
239 | * Not supported. Set the collator's strength | |
240 | * | |
241 | * @param int $strength Strength to set, possible values: | |
242 | * Collator::PRIMARY | |
243 | * Collator::SECONDARY | |
244 | * Collator::TERTIARY | |
245 | * Collator::QUATERNARY | |
246 | * Collator::IDENTICAL | |
247 | * Collator::DEFAULT | |
248 | * | |
249 | * @return Boolean True on success or false on failure | |
250 | * | |
251 | * @see http://www.php.net/manual/en/collator.setstrength.php | |
252 | * | |
253 | * @throws MethodNotImplementedException | |
254 | */ | |
255 | public function setStrength($strength) | |
256 | { | |
257 | throw new MethodNotImplementedException(__METHOD__); | |
258 | } | |
259 | ||
260 | /** | |
261 | * Not supported. Sort array using specified collator and sort keys | |
262 | * | |
263 | * @param array &$arr Array of strings to sort | |
264 | * | |
265 | * @return Boolean True on success or false on failure | |
266 | * | |
267 | * @see http://www.php.net/manual/en/collator.sortwithsortkeys.php | |
268 | * | |
269 | * @throws MethodNotImplementedException | |
270 | */ | |
271 | public function sortWithSortKeys(&$arr) | |
272 | { | |
273 | throw new MethodNotImplementedException(__METHOD__); | |
274 | } | |
275 | ||
276 | /** | |
277 | * Not supported. Sort array using specified collator | |
278 | * | |
279 | * @param array &$arr Array of string to sort | |
280 | * @param int $sortFlag Optional sorting type, one of the following: | |
281 | * Collator::SORT_REGULAR | |
282 | * Collator::SORT_NUMERIC | |
283 | * Collator::SORT_STRING | |
284 | * | |
285 | * @return Boolean True on success or false on failure | |
286 | * | |
287 | * @see http://www.php.net/manual/en/collator.sort.php | |
288 | * | |
289 | * @throws MethodNotImplementedException | |
290 | */ | |
291 | public function sort(&$arr, $sortFlag = self::SORT_REGULAR) | |
292 | { | |
293 | throw new MethodNotImplementedException(__METHOD__); | |
294 | } | |
295 | } |