mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	Fix #17863: \yii\helpers\BaseInflector::slug() doesn't work with an empty string as a replacement argument
				
					
				
			This commit is contained in:
		@ -31,7 +31,7 @@ Yii Framework 2 Change Log
 | 
				
			|||||||
- Bug #17803: Fix `ErrorHandler` unregister and register to only change global state when applicable (SamMousa)
 | 
					- 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 #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)
 | 
					- 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
 | 
					2.0.31 December 18, 2019
 | 
				
			||||||
------------------------
 | 
					------------------------
 | 
				
			||||||
 | 
				
			|||||||
@ -477,7 +477,11 @@ class BaseInflector
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    public static function slug($string, $replacement = '-', $lowercase = true)
 | 
					    public static function slug($string, $replacement = '-', $lowercase = true)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        if ((string)$replacement !== '') {
 | 
				
			||||||
            $parts = explode($replacement, static::transliterate($string));
 | 
					            $parts = explode($replacement, static::transliterate($string));
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            $parts = [static::transliterate($string)];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $replaced = array_map(function ($element) use ($replacement) {
 | 
					        $replaced = array_map(function ($element) use ($replacement) {
 | 
				
			||||||
            $element = preg_replace('/[^a-zA-Z0-9=\s—–-]+/u', '', $element);
 | 
					            $element = preg_replace('/[^a-zA-Z0-9=\s—–-]+/u', '', $element);
 | 
				
			||||||
@ -485,7 +489,9 @@ class BaseInflector
 | 
				
			|||||||
        }, $parts);
 | 
					        }, $parts);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $string = trim(implode($replacement, $replaced), $replacement);
 | 
					        $string = trim(implode($replacement, $replaced), $replacement);
 | 
				
			||||||
 | 
					        if ((string)$replacement !== '') {
 | 
				
			||||||
            $string = preg_replace('#' . preg_quote($replacement) . '+#', $replacement, $string);
 | 
					            $string = preg_replace('#' . preg_quote($replacement) . '+#', $replacement, $string);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $lowercase ? strtolower($string) : $string;
 | 
					        return $lowercase ? strtolower($string) : $string;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -197,6 +197,7 @@ class InflectorTest extends TestCase
 | 
				
			|||||||
        $this->assertEquals('remove_excess_replacements', Inflector::slug(' _ _ remove excess _ _ replacements_', '_'));
 | 
					        $this->assertEquals('remove_excess_replacements', Inflector::slug(' _ _ remove excess _ _ replacements_', '_'));
 | 
				
			||||||
        $this->assertEquals('thisrepisreprepreplacement', Inflector::slug('this is REP-lacement', 'REP'));
 | 
					        $this->assertEquals('thisrepisreprepreplacement', Inflector::slug('this is REP-lacement', 'REP'));
 | 
				
			||||||
        $this->assertEquals('0_100_kmh', Inflector::slug('0-100 Km/h', '_'));
 | 
					        $this->assertEquals('0_100_kmh', Inflector::slug('0-100 Km/h', '_'));
 | 
				
			||||||
 | 
					        $this->assertEquals('testtext', Inflector::slug('test text', ''));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function testSlugIntl()
 | 
					    public function testSlugIntl()
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user