Files
Phraseanet/lib/Alchemy/Phrasea/SearchEngine/Elastic/StringUtils.php
Benoît Burnichon daf2cf6150 replace StringUtils::slugify by a proper instance
Previous implementation had a flaw when using a different separating character. New implementation should be well tested.
2016-03-15 16:59:53 +01:00

67 lines
1.4 KiB
PHP

<?php
/**
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
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 = '-')
{
return self::getSlugifier()->slugify($string, $separator);
}
public static function asciiLowerFold($string)
{
// '北京' -> 'bei jing'
if (!self::$transliterator) {
$id = 'Any-Latin; Latin-ASCII; Any-Lower';
self::$transliterator = Transliterator::create($id);
}
return self::$transliterator->transliterate($string);
}
}