mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-12 20:43:25 +00:00
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:
@@ -1,9 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
/**
|
||||||
/*
|
|
||||||
* This file is part of Phraseanet
|
* 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
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
@@ -11,28 +10,46 @@
|
|||||||
|
|
||||||
namespace Alchemy\Phrasea\SearchEngine\Elastic;
|
namespace Alchemy\Phrasea\SearchEngine\Elastic;
|
||||||
|
|
||||||
|
use Cocur\Slugify\Slugify;
|
||||||
|
use Cocur\Slugify\SlugifyInterface;
|
||||||
use Transliterator;
|
use Transliterator;
|
||||||
|
|
||||||
class StringUtils
|
class StringUtils
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var SlugifyInterface|null
|
||||||
|
*/
|
||||||
|
private static $slugifier;
|
||||||
|
|
||||||
private static $transliterator;
|
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 = '-')
|
public static function slugify($string, $separator = '-')
|
||||||
{
|
{
|
||||||
// Replace non letter or digits by _
|
return self::getSlugifier()->slugify($string, $separator);
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function asciiLowerFold($string)
|
public static function asciiLowerFold($string)
|
||||||
|
Reference in New Issue
Block a user