Skip to content

Commit

Permalink
fixed #26 - Make Slugger work when iconv() function is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Feb 29, 2012
1 parent 5f3f07d commit df58f42
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/Easybook/Util/Slugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit df58f42

Please sign in to comment.