-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
SlugGenerator.php
376 lines (321 loc) · 9.3 KB
/
SlugGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/*
* This file is part of the ausi/slug-generator package.
*
* (c) Martin Auswöger <martin@auswoeger.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Ausi\SlugGenerator;
/**
* Slug generator.
*
* @author Martin Auswöger <martin@auswoeger.com>
*/
class SlugGenerator implements SlugGeneratorInterface
{
/**
* @var SlugOptions
*/
private $options;
/**
* @var \Transliterator[]
*/
private $transliterators = [];
/**
* @param iterable $options SlugOptions object or options array
*/
public function __construct(iterable $options = [])
{
static::checkPcreSupport();
if (!$options instanceof SlugOptions) {
$options = new SlugOptions($options);
}
$this->options = $options;
}
/**
* @throws \RuntimeException If PCRE unicode support is missing
*
* @internal
*/
public static function checkPcreSupport(): void
{
static $supported = null;
if ($supported === null) {
$supported = @preg_match('/\pL/u', 'a') === 1;
}
if (!$supported) {
throw new \RuntimeException(sprintf('Missing PCRE unicode support'));
}
}
/**
* {@inheritdoc}
*/
public function generate(string $text, iterable $options = []): string
{
if (preg_match('//u', $text) !== 1) {
throw new \InvalidArgumentException('Text is invalid UTF-8');
}
$options = $this->options->merge($options);
if ($options->getValidChars() === '') {
return '';
}
$text = \Normalizer::normalize($text, \Normalizer::FORM_C);
$text = $this->removeIgnored($text, $options->getIgnoreChars());
$text = $this->transform($text, $options->getValidChars(), $options->getTransforms(), $options->getLocale());
$text = $this->removeIgnored($text, $options->getIgnoreChars());
$text = $this->replaceWithDelimiter($text, $options->getValidChars(), $options->getDelimiter());
return $text;
}
/**
* Remove ignored characters from text.
*
* @param string $text
* @param string $ignore
*
* @return string
*/
private function removeIgnored(string $text, string $ignore): string
{
if ($ignore === '') {
return $text;
}
return preg_replace('(['.$ignore.'])us', '', $text);
}
/**
* Replace all invalid characters with a delimiter
* and strip the delimiter from the beginning and the end.
*
* @param string $text
* @param string $valid
* @param string $delimiter
*
* @return string
*/
private function replaceWithDelimiter(string $text, string $valid, string $delimiter): string
{
$quoted = preg_quote($delimiter);
// Replace all invalid characters with a single delimiter
$text = preg_replace(
'((?:[^'.$valid.']|'.$quoted.')+)us',
$delimiter,
$text
);
// Remove delimiters from the beginning and the end
$text = preg_replace('(^(?:'.$quoted.')+|(?:'.$quoted.')+$)us', '', $text);
return $text;
}
/**
* Apply all transforms with the specified locale
* to the invalid parts of the text.
*
* @param string $text
* @param string $valid
* @param iterable $transforms
* @param string $locale
*
* @return string
*/
private function transform(string $text, string $valid, iterable $transforms, string $locale): string
{
$regexRegular = '([^'.$valid.']+)us';
$regexCase = $this->createCaseRegex($valid);
foreach ($transforms as $transform) {
$regex = ($transform === 'Lower' || $transform === 'Upper') ? $regexCase : $regexRegular;
if ($locale) {
$text = $this->applyTransformRule($text, $transform, $locale, $regex);
}
$text = $this->applyTransformRule($text, $transform, '', $regex);
}
return $text;
}
/**
* Create a regular expression that matches all characters that are invalid
* but whose lower/upper-case counterparts are valid.
*
* @param string $valid
*
* @return string
*/
private function createCaseRegex(string $valid): string
{
$insensitive = $valid;
// Fix case insensitive matching of turkish “I” characters
if (preg_match('(['.$valid.'])us', 'İı')) {
$insensitive .= 'İı';
}
$insensitive = preg_replace_callback('(\\\\([pP])\{L([lu])\})s', function ($match) {
return '\\'.$match[1].'{L'.($match[2] === 'l' ? 'u' : 'l').'}';
}, $insensitive);
return '((?:(?=(?i)['.$insensitive.'])[^'.$valid.'])+)us';
}
/**
* Apply a transform rule with the specified locale
* to the parts that match the regular expression.
*
* @param string $text
* @param string $rule
* @param string $locale
* @param string $regex
*
* @return string
*/
private function applyTransformRule(string $text, string $rule, string $locale, string $regex): string
{
$transliterator = $this->getTransliterator($rule, $locale);
$newText = '';
$offset = 0;
foreach ($this->getRanges($text, $regex) as $range) {
$newText .= substr($text, $offset, $range[0] - $offset);
$newText .= $this->transformWithContext($transliterator, $text, $range[0], $range[1]);
$offset = $range[0] + $range[1];
}
$newText .= substr($text, $offset);
return $newText;
}
/**
* Transform the text at the specified position
* and use a one character context if possible.
*
* `Transliterator::transliterate()` doesn’t yet support context parameters
* of the underlying ICU implementation.
* Because of that, we add the context before the transform
* and check afterwards that the context didn’t change.
*
* @param \Transliterator $transliterator
* @param string $text
* @param int $index
* @param int $length
*
* @return string
*/
private function transformWithContext(\Transliterator $transliterator, string $text, int $index, int $length): string
{
$left = mb_substr(substr($text, 0, $index), -1, null, 'UTF-8');
$right = mb_substr(substr($text, $index + $length), 0, 1, 'UTF-8');
$leftLength = strlen($left);
$rightLength = strlen($right);
$text = substr($text, $index, $length);
$transformed = $transliterator->transliterate($left.$text.$right);
if (
(!$leftLength || strncmp($transformed, $left, $leftLength) === 0)
&& (!$rightLength || substr_compare($transformed, $right, -$rightLength) === 0)
) {
return substr($transformed, $leftLength, $rightLength ? -$rightLength : strlen($transformed));
} else {
return $transliterator->transliterate($text);
}
}
/**
* Get the Transliterator for the specified transform rule and locale.
*
* @param string $rule
* @param string $locale
*
* @return \Transliterator
*/
private function getTransliterator(string $rule, string $locale): \Transliterator
{
$key = $rule.'|'.$locale;
if (!isset($this->transliterators[$key])) {
$this->transliterators[$key] = $this->findMatchingTransliterator($rule, $locale);
}
return $this->transliterators[$key];
}
/**
* Find the best matching Transliterator
* for the specified transform rule and locale.
*
* @param string $rule
* @param string $locale
*
* @return \Transliterator
*/
private function findMatchingTransliterator(string $rule, string $locale): \Transliterator
{
$candidates = [
'Latin-'.$rule,
$rule,
];
if ($locale) {
array_unshift(
$candidates,
$locale.'-'.$rule,
\Locale::getPrimaryLanguage($locale).'-'.$rule
);
}
$errorLevel = ini_set('intl.error_level', '0');
$useExceptions = ini_set('intl.use_exceptions', '0');
try {
foreach ($candidates as $candidate) {
$candidate = $this->fixTransliteratorRule($candidate);
if ($transliterator = \Transliterator::create($candidate)) {
return $transliterator;
}
if ($transliterator = \Transliterator::createFromRules($candidate)) {
return $transliterator;
}
}
} finally {
ini_set('intl.error_level', $errorLevel);
ini_set('intl.use_exceptions', $useExceptions);
}
throw new \InvalidArgumentException(
sprintf('No Transliterator transform rule found for "%s" with locale "%s".', $rule, $locale)
);
}
/**
* Apply fixes to a transform rule for older versions of the Intl extension.
*
* @param string $rule
*
* @return string
*/
private function fixTransliteratorRule(string $rule): string
{
static $latinAsciiFix;
static $deAsciiFix;
if ($latinAsciiFix === null) {
$latinAsciiFix = \in_array('Latin-ASCII', \Transliterator::listIDs(), true)
? false
: file_get_contents(__DIR__.'/Resources/Latin-ASCII.txt')
;
}
if ($deAsciiFix === null) {
$deAsciiFix = \in_array('de-ASCII', \Transliterator::listIDs(), true)
? false
: file_get_contents(__DIR__.'/Resources/de-ASCII.txt')
;
if ($latinAsciiFix) {
$deAsciiFix = str_replace('::Latin-ASCII;', $latinAsciiFix, $deAsciiFix);
}
}
// Add the de-ASCII transform if a CLDR version lower than 32.0 is used.
if ($deAsciiFix && $rule === 'de-ASCII') {
return $deAsciiFix;
}
// Add the Latin-ASCII transform if a CLDR version lower than 1.9 is used.
if ($latinAsciiFix && $rule === 'Latin-ASCII') {
return $latinAsciiFix;
}
return $rule;
}
/**
* Get all matching ranges.
*
* @param string $text
* @param string $regex
*
* @return array Array of range arrays, each consisting of index and length
*/
private function getRanges(string $text, string $regex): array
{
preg_match_all($regex, $text, $matches, PREG_OFFSET_CAPTURE);
$ranges = array_map(function ($match) {
return [$match[1], strlen($match[0])];
}, $matches[0]);
return $ranges;
}
}