Collator.php 7.3 KB

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