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