Collator.php 8.8 KB

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