collator_api.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. #############################################################################
  3. # Object-oriented API
  4. #############################################################################
  5. /**
  6. * Collator class.
  7. *
  8. * This is a wrapper around ICU Collator C API (declared in ucol.h).
  9. *
  10. * Example:
  11. * <code>
  12. *
  13. * </code>
  14. *
  15. * @see http://www.icu-project.org/apiref/icu4c/ucol_8h.html
  16. * @see http://www.icu-project.org/apiref/icu4c/classCollator.html
  17. *
  18. */
  19. class Collator {
  20. #############################################################################
  21. # Common constants.
  22. #############################################################################
  23. /**
  24. * Locale-related constants.
  25. *
  26. * These will be moved out of Collator when Locale class is created.
  27. */
  28. const ULOC_ACTUAL_LOCALE = 0;
  29. const ULOC_VALID_LOCALE = 1;
  30. const ULOC_REQUESTED_LOCALE = 2;
  31. /*
  32. * WARNING:
  33. * The values described here are NOT the actual values in PHP code.
  34. * They are references to the ICU C definitions, so the line
  35. * const DEFAULT_STRENGTH = 'UCOL_DEFAULT_STRENGTH';
  36. * actually means that Collator::DEFAULT_STRENGTH is the same as
  37. * UCOL_DEFAULT_STRENGTH constant in the ICU library.
  38. */
  39. /**
  40. * Valid attribute values.
  41. *
  42. * @see Collator::setAttribute()
  43. * @see collator_set_attribute()
  44. */
  45. const DEFAULT_VALUE = 'UCOL_DEFAULT';
  46. const PRIMARY = 'UCOL_PRIMARY';
  47. const SECONDARY = 'UCOL_SECONDARY';
  48. const TERTIARY = 'UCOL_TERTIARY';
  49. const DEFAULT_STRENGTH = 'UCOL_DEFAULT_STRENGTH';
  50. const QUATERNARY = 'UCOL_QUATERNARY';
  51. const IDENTICAL = 'UCOL_IDENTICAL';
  52. const OFF = 'UCOL_OFF';
  53. const ON = 'UCOL_ON';
  54. const SHIFTED = 'UCOL_SHIFTED';
  55. const NON_IGNORABLE = 'UCOL_NON_IGNORABLE';
  56. const LOWER_FIRST = 'UCOL_LOWER_FIRST';
  57. const UPPER_FIRST = 'UCOL_UPPER_FIRST';
  58. /**
  59. * Valid attribute names.
  60. *
  61. * @see Collator::setAttribute()
  62. * @see collator_set_attribute()
  63. */
  64. const FRENCH_COLLATION = 'UCOL_FRENCH_COLLATION';
  65. const ALTERNATE_HANDLING = 'UCOL_ALTERNATE_HANDLING';
  66. const CASE_FIRST = 'UCOL_CASE_FIRST';
  67. const CASE_LEVEL = 'UCOL_CASE_LEVEL';
  68. const NORMALIZATION_MODE = 'UCOL_NORMALIZATION_MODE';
  69. const STRENGTH = 'UCOL_STRENGTH';
  70. const HIRAGANA_QUATERNARY_MODE = 'UCOL_HIRAGANA_QUATERNARY_MODE';
  71. const NUMERIC_COLLATION = 'UCOL_NUMERIC_COLLATION';
  72. /**
  73. * Create a collator
  74. *
  75. * @param string $locale The locale whose collation rules
  76. * should be used. Special values for
  77. * locales can be passed in - if null is
  78. * passed for the locale, the default
  79. * locale collation rules will be used. If
  80. * empty string ("") or "root" are passed,
  81. * UCA rules will be used.
  82. *
  83. * @return Collator New instance of Collator object.
  84. */
  85. public function __construct( $locale ) {}
  86. /**
  87. * Create a collator
  88. *
  89. * Creates a new instance of Collator.
  90. *
  91. * This method is useful when you prefer just to get null on error,
  92. * as if you called collator_create().
  93. *
  94. * @return Collator Newly created Collator instance,
  95. * or null on error.
  96. *
  97. * @see __construct()
  98. * @see collator_create()
  99. */
  100. public static function create( $locale ) {}
  101. /**
  102. * Get collator's last error code.
  103. *
  104. * @return int Error code returned by the last
  105. * Collator method call.
  106. */
  107. public function getErrorCode() {}
  108. /**
  109. * Return error text for the last ICU operation.
  110. *
  111. * @return string Description of an error occurred in the last
  112. * Collator method call.
  113. */
  114. public function getErrorMessage() {}
  115. /**
  116. * Compare two strings using PHP strcmp() semantics.
  117. *
  118. * Wrapper around ICU ucol_strcoll().
  119. *
  120. * @param string $str1 First string to compare.
  121. * @param string $str2 Second string to compare.
  122. *
  123. * @return int 1 if $str1 is greater than $str2;
  124. * 0 if $str1 is equal to $str2;
  125. * -1 if $str1 is less than $str2.
  126. * On error false is returned.
  127. */
  128. public function compare( $str1, $str2 ) {}
  129. /**
  130. * Equivalent to standard PHP sort() using Collator.
  131. *
  132. * @param array $arr Array of strings to sort
  133. * @param int $sort_flags Optional sorting type, one of the following:
  134. * - SORT_REGULAR - compare items normally (don't change types)
  135. * - SORT_NUMERIC - compare items numerically
  136. * - SORT_STRING - compare items as strings
  137. * Default sorting type is SORT_REGULAR.
  138. *
  139. * @return bool true on success or false on failure.
  140. */
  141. public function sort( $arr, $sort_flags ) {}
  142. /**
  143. * Sort array maintaining index association.
  144. *
  145. * Equivalent to standard PHP asort() using Collator.
  146. *
  147. * @param array $arr Array of strings to sort
  148. * @param int $sort_flags Optional sorting type
  149. *
  150. * @return bool true on success or false on failure.
  151. *
  152. * @see Collator::sort()
  153. */
  154. public function asort( $arr, $sort_flags ) {}
  155. /**
  156. * Equivalent to standard PHP sort() using Collator.
  157. *
  158. * Similar to Collator::sort().
  159. * Uses ICU ucol_getSortKey() to gain more speed on large arrays.
  160. *
  161. * @param array $arr Array of strings to sort
  162. *
  163. * @return bool true on success or false on failure.
  164. */
  165. public function sortWithSortKeys( $arr ) {}
  166. /**
  167. * @todo Do we want to support other standard PHP sort functions: ksort, rsort, asort?
  168. */
  169. /**
  170. * Get collation attribute value.
  171. *
  172. * Wrapper around ICU ucol_getAttribute().
  173. *
  174. * @param int $attr Attribute to get value for.
  175. *
  176. * @return int Attribute value, or false on error.
  177. */
  178. public function getAttribute( $attr ) {}
  179. /**
  180. * Set collation attribute.
  181. *
  182. * Wrapper around ICU ucol_setAttribute().
  183. *
  184. * @param int $attr Attribute.
  185. * @param int $val Attribute value.
  186. *
  187. * @return bool true on success, false otherwise.
  188. */
  189. public function setAttribute( $attr, $val ) {}
  190. /**
  191. * Get current collation strength.
  192. *
  193. * Wrapper around ICU ucol_getStrength().
  194. *
  195. * @return int Current collation strength, or false on error.
  196. */
  197. public function getStrength() {}
  198. /**
  199. * Set collation strength.
  200. *
  201. * Wrapper around ICU ucol_setStrength().
  202. *
  203. * @param int $strength Strength to set.
  204. *
  205. * @return bool true on success, false otherwise.
  206. */
  207. public function setStrength( $strength ) {}
  208. /**
  209. * Get the locale name of the collator.
  210. *
  211. * Wrapper around ICU ucol_getLocaleByType().
  212. *
  213. * @param int $type You can choose between requested, valid
  214. * and actual locale
  215. * (ULOC_REQUESTED_LOCALE,
  216. * ULOC_VALID_LOCALE, ULOC_ACTUAL_LOCALE,
  217. * respectively).
  218. *
  219. * @return string Real locale name from which the
  220. * collation data comes. If the collator
  221. * was instantiated from rules or an error occurred,
  222. * returns false.
  223. */
  224. public function getLocale( $type ) {}
  225. }
  226. #############################################################################
  227. # Procedural API
  228. #############################################################################
  229. /**
  230. * Create collator.
  231. *
  232. * @param string $locale The locale containing the required
  233. * collation rules. Special values for
  234. * locales can be passed in - if null is
  235. * passed for the locale, the default
  236. * locale collation rules will be used. If
  237. * empty string ("") or "root" are passed,
  238. * UCA rules will be used.
  239. *
  240. * @return Collator New instance of Collator object, or null on error.
  241. */
  242. function collator_create( $locale ) {}
  243. /**
  244. * Compare two strings.
  245. *
  246. * The strings will be compared using the options already
  247. * specified.
  248. *
  249. * @param Collator $coll Collator object.
  250. * @param string $str1 The first string to compare.
  251. * @param string $str2 The second string to compare.
  252. *
  253. * @return int 1 if $str1 is greater than $str2;
  254. * 0 if $str1 is equal to $str2;
  255. * -1 if $str1 is less than $str2.
  256. * On error false is returned.
  257. *
  258. */
  259. function collator_compare( $coll, $str1, $str2 ) {}
  260. /**
  261. * Sort array using specified collator.
  262. *
  263. * @param Collator $coll Collator object.
  264. * @param array $arr Array of strings to sort.
  265. * @param int $sort_flags Optional sorting type, one of the following:
  266. * - SORT_REGULAR - compare items normally (don't change types)
  267. * - SORT_NUMERIC - compare items numerically
  268. * - SORT_STRING - compare items as strings
  269. * Default sorting type is SORT_REGULAR.
  270. *
  271. * @return bool true on success or false on failure.
  272. */
  273. function collator_sort( $coll, $arr, $sort_flags ) {}
  274. /**
  275. * Sort array maintaining index association.
  276. *
  277. * @param Collator $coll Collator object.
  278. * @param array $arr Array of strings to sort.
  279. * @param int $sort_flags Optional sorting type.
  280. *
  281. * @return bool true on success or false on failure.
  282. *
  283. * @see collator_sort()
  284. */
  285. function collator_asort( $coll, $arr, $sort_flags ) {}
  286. /**
  287. * Sort array using specified collator.
  288. *
  289. * Similar to collator_sort().
  290. * Uses ICU ucol_getSortKey() to gain more speed on large arrays.
  291. *
  292. * @param Collator $coll Collator object.
  293. * @param array $arr Array of strings to sort
  294. *
  295. * @return bool true on success or false on failure.
  296. */
  297. function collator_sort_with_sort_keys( $coll, $arr ) {}
  298. /**
  299. * Get the locale name of the collator.
  300. *
  301. * @param Collator $coll Collator object.
  302. * @param int $type You can choose between valid and
  303. * actual locale
  304. * (ULOC_VALID_LOCALE, ULOC_ACTUAL_LOCALE
  305. * respectively).
  306. *
  307. * @return string Real locale name from which the
  308. * collation data comes. If the collator
  309. * was instantiated from rules or an error occurred,
  310. * returns false.
  311. */
  312. function collator_get_locale( $coll, $type ) {}
  313. /**
  314. * Get collation attribute value.
  315. *
  316. * @param Collator $coll Collator object.
  317. * @param int $attr Attribute to get value for.
  318. *
  319. * @return int Attribute value, or false on error.
  320. */
  321. function collator_get_attribute( $coll, $attr ) {}
  322. /**
  323. * Get current collation strength.
  324. *
  325. * @param Collator $coll Collator object.
  326. *
  327. * @return int Current collation strength, or false on error.
  328. */
  329. function collator_get_strength( $coll ) {}
  330. /**
  331. * Set collation strength.
  332. *
  333. * @param Collator $coll Collator object.
  334. * @param int $strength Strength to set.
  335. *
  336. * @return bool true on success, false otherwise.
  337. */
  338. function collator_set_strength( $coll, $strength ) {}
  339. /**
  340. * Set collation attribute.
  341. *
  342. * @param Collator $coll Collator object.
  343. * @param int $attr Attribute.
  344. * @param int $val Attribute value.
  345. *
  346. * @return bool true on success, false otherwise.
  347. */
  348. function collator_set_attribute( $coll, $attr, $val ) {}
  349. /**
  350. * Get collator's last error code.
  351. *
  352. * @param Collator $coll Collator object.
  353. *
  354. * @return int Error code returned by the last
  355. * Collator API function call.
  356. */
  357. function collator_get_error_code( $coll ) {}
  358. /**
  359. * Get text for collator's last error code.
  360. *
  361. * @param Collator $coll Collator object.
  362. *
  363. * @return string Description of an error occurred in the last
  364. * Collator API function call.
  365. */
  366. function collator_get_error_message( $coll ) {}