diff --git a/src/Easybook/Util/Slugger.php b/src/Easybook/Util/Slugger.php index d9dca4e5..3bc0f87d 100644 --- a/src/Easybook/Util/Slugger.php +++ b/src/Easybook/Util/Slugger.php @@ -13,31 +13,49 @@ class Slugger { - private $container; + private $app; private $separator; private $prefix; private $unique; - public function __construct($container, $separator = '-', $prefix = '', $unique = true) + public function __construct($app, $separator = '-', $prefix = '', $unique = true) { - $this->container = $container; + $this->app = $app; $this->separator = $separator; $this->prefix = $prefix; $this->unique = $unique; } + // adapted from http://cubiq.org/the-perfect-php-clean-url-generator public function slugify($string) { - // Copied from http://cubiq.org/the-perfect-php-clean-url-generator - $slug = iconv('UTF-8', 'ASCII//TRANSLIT', $string); + $string = strip_tags($string); + + if (function_exists('iconv')) { + $slug = iconv('UTF-8', 'ASCII//TRANSLIT', $string); + } + else if (function_exists('mb_convert_encoding')) { + $slug = mb_convert_encoding($string, 'ASCII'); + } + else { + // if both iconv and mb_* functions are unavailable, use a + // simple method to remove accents + // TODO: Is it better to just throw an exception? + $slug = strtr( + utf8_decode($string), + utf8_decode('ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ'), + 'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy' + ); + } + $slug = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $slug); $slug = strtolower(trim($slug, $this->separator)); $slug = preg_replace("/[\/_|+ -]+/", $this->separator, $slug); - + $slug = $this->prefix.$slug; - + // $slugs array must hold original slugs, without unique substring - $slugs = $this->container->append('publishing.slugs', $slug); + $slugs = $this->app->append('publishing.slugs', $slug); if ($this->unique) { $occurrences = array_count_values($slugs);