replace StringUtils::slugify by a proper instance

Previous implementation had a flaw when using a different separating character. New implementation should be well tested.
This commit is contained in:
Benoît Burnichon
2016-03-15 16:59:53 +01:00
parent 210920c31f
commit daf2cf6150

View File

@@ -1,9 +1,8 @@
<?php
/*
/**
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
@@ -11,28 +10,46 @@
namespace Alchemy\Phrasea\SearchEngine\Elastic;
use Cocur\Slugify\Slugify;
use Cocur\Slugify\SlugifyInterface;
use Transliterator;
class StringUtils
{
/**
* @var SlugifyInterface|null
*/
private static $slugifier;
private static $transliterator;
/**
* Prevent instantiation of the class
*/
private function __construct()
{
}
public static function setSlugify(SlugifyInterface $slugify = null)
{
self::$slugifier = $slugify;
}
/**
* @return SlugifyInterface
*/
private static function getSlugifier()
{
if (null === self::$slugifier) {
self::$slugifier = new Slugify();
}
return self::$slugifier;
}
public static function slugify($string, $separator = '-')
{
// Replace non letter or digits by _
$string = preg_replace('/[^\\pL\d]+/u', $separator, $string);
$string = trim($string, $separator);
// Transliterate
if (function_exists('iconv')) {
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
}
// Remove non wording characters
$string = preg_replace('/[^-\w]+/', '', $string);
$string = strtolower($string);
return $string;
return self::getSlugifier()->slugify($string, $separator);
}
public static function asciiLowerFold($string)