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