Fix #17863: \yii\helpers\BaseInflector::slug() doesn't work with an empty string as a replacement argument

This commit is contained in:
Viktor Pikaev
2020-02-19 03:26:33 +03:00
committed by GitHub
parent 93f74bfd04
commit c901608af7
3 changed files with 10 additions and 3 deletions

View File

@ -31,7 +31,7 @@ Yii Framework 2 Change Log
- Bug #17803: Fix `ErrorHandler` unregister and register to only change global state when applicable (SamMousa)
- Enh #17729: Path alias support was added to `yii\web\UploadedFile::saveAs()` (sup-ham)
- Enh #17792: Add support for `aria` attributes to `yii\helpers\BaseHtml::renderTagAttributes()` (brandonkelly)
- Bug #17863: `\yii\helpers\BaseInflector::slug()` doesn't work with an empty string as a replacement argument (haruatari)
2.0.31 December 18, 2019
------------------------

View File

@ -477,7 +477,11 @@ class BaseInflector
*/
public static function slug($string, $replacement = '-', $lowercase = true)
{
$parts = explode($replacement, static::transliterate($string));
if ((string)$replacement !== '') {
$parts = explode($replacement, static::transliterate($string));
} else {
$parts = [static::transliterate($string)];
}
$replaced = array_map(function ($element) use ($replacement) {
$element = preg_replace('/[^a-zA-Z0-9=\s—-]+/u', '', $element);
@ -485,7 +489,9 @@ class BaseInflector
}, $parts);
$string = trim(implode($replacement, $replaced), $replacement);
$string = preg_replace('#' . preg_quote($replacement) . '+#', $replacement, $string);
if ((string)$replacement !== '') {
$string = preg_replace('#' . preg_quote($replacement) . '+#', $replacement, $string);
}
return $lowercase ? strtolower($string) : $string;
}

View File

@ -197,6 +197,7 @@ class InflectorTest extends TestCase
$this->assertEquals('remove_excess_replacements', Inflector::slug(' _ _ remove excess _ _ replacements_', '_'));
$this->assertEquals('thisrepisreprepreplacement', Inflector::slug('this is REP-lacement', 'REP'));
$this->assertEquals('0_100_kmh', Inflector::slug('0-100 Km/h', '_'));
$this->assertEquals('testtext', Inflector::slug('test text', ''));
}
public function testSlugIntl()