Collator.php 8.7 KB

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