Skip to content

Commit

Permalink
Merge pull request #11 from keyvanakbary/functional-approach
Browse files Browse the repository at this point in the history
Move towards a more functional approach
  • Loading branch information
keyvanakbary committed Aug 2, 2015
2 parents 3a850c7 + f22144d commit b94dd6c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://secure.travis-ci.org/keyvanakbary/slugifier.svg?branch=master)](http://travis-ci.org/keyvanakbary/slugifier)

A full-featured, simple and clean implementation for creating [slugs](http://en.wikipedia.org/wiki/Semantic_URL#Slug). No third party libraries or extensions needed.
A full-featured, simple, clean and pure functional implementation for creating [slugs](http://en.wikipedia.org/wiki/Semantic_URL#Slug). No third party libraries or extensions needed.

## Installation

Expand All @@ -13,25 +13,27 @@ composer require keyvanakbary/slugifier
## Usage

```php
\Slugifier::slugify('JúST å fëw wørds'); // just-a-few-words
use slugifier as s;

\Slugifier::slugify('Αυτή είναι μια δοκιμή'); // ayti-einai-mia-dokimi
s\slugify('JúST å fëw wørds'); // just-a-few-words

\Slugifier::slugify('Wikipedia style', '_'); // wikipedia_style
s\slugify('Αυτή είναι μια δοκιμή'); // ayti-einai-mia-dokimi

s\slugify('Wikipedia style', '_'); // wikipedia_style
```

### Modifiers

Sometimes the default character map is not accurate enough. Slugifier supports custom *modifiers*

```php
\Slugifier::slugify('Pingüino', '-', array('ü' => 'u'))); // pinguino
s\slugify('Pingüino', '-', array('ü' => 'u'))); // pinguino
```

Some language iso modifiers are supported

```php
\Slugifier::slugify('Estaĵo', '-', \Slugifier::mod('eo')); // estajxo
s\slugify('Estaĵo', '-', s\mod('eo')); // estajxo

\Slugifier::slugify('Örnektir', '-', \Slugifier::mod('tr')); // ornektir
s\slugify('Örnektir', '-', s\mod('tr')); // ornektir
```
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-4": { "": "src/" }
"files": ["src/slugifier.php"]
},
"config": {
"bin-dir": "bin"
}
}
}
41 changes: 17 additions & 24 deletions src/Slugifier.php → src/slugifier.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

class Slugifier
{
const UNWANTED_CHARS = '/([^a-z0-9]|-)+/';
namespace slugifier;

private static $charMap = array(
class char
{
public static $map = array(
// Latin
'°' => '0', 'æ' => 'ae', 'ǽ' => 'ae', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Å' => 'A', 'Ǻ' => 'A',
'Ă' => 'A', 'Ǎ' => 'A', 'Æ' => 'AE', 'Ǽ' => 'AE', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'å' => 'a',
Expand Down Expand Up @@ -92,35 +92,28 @@ class Slugifier
'¹' => '1', '²' => '2', '³' => '3', '' => 'P'
);

private static $modifiers = array(
public static $modifiers = array(
'tr' => array('Ö' => 'O', 'Ü' => 'U', 'ö' => 'o', 'ü' => 'u'),
'eo' => array(
'ĉ' => 'cx', 'ĝ' => 'gx', 'ĥ' => 'hx', 'ĵ' => 'jx', 'ŝ' => 'sx', 'ŭ' => 'ux', 'Ĉ' => 'CX', 'Ĝ' => 'GX',
'Ĥ' => 'HX', 'Ĵ' => 'JX', 'Ŝ' => 'SX', 'Ŭ' => 'UX'
)
);
}

public static function mod($iso)
{
if (!isset(self::$modifiers[$iso])) {
throw new \InvalidArgumentException(sprintf('Iso "%s" not supported', $iso));
}

return self::$modifiers[$iso];
function mod($iso)
{
if (!isset(char::$modifiers[$iso])) {
throw new \InvalidArgumentException(sprintf('Iso "%s" not supported', $iso));
}

public static function slugify($text, $separator = '-', array $modifier = array())
{
return trim(self::replaceUnwantedChars(self::normalize($text, $modifier), $separator), $separator);
}
return char::$modifiers[$iso];
}

private static function normalize($text, array $modifier)
{
return strtolower(strtr($text, $modifier + self::$charMap));
}
function slugify($text, $separator = '-', array $modifier = array())
{
$normalized = strtolower(strtr($text, $modifier + char::$map));
$cleaned = preg_replace($unwantedChars = '/([^a-z0-9]|-)+/', $separator, $normalized);

private static function replaceUnwantedChars($text, $separator)
{
return preg_replace(self::UNWANTED_CHARS, $separator, $text);
}
return trim($cleaned, $separator);
}
18 changes: 10 additions & 8 deletions tests/SlugifierTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace slugifier;

class SlugifierTest extends \PHPUnit_Framework_TestCase
{
/**
Expand All @@ -8,7 +10,7 @@ class SlugifierTest extends \PHPUnit_Framework_TestCase
*/
public function shouldCreateSlug($text, $expectedSlug)
{
$slug = Slugifier::slugify($text);
$slug = slugify($text);

$this->assertEquals($expectedSlug, $slug);
}
Expand Down Expand Up @@ -39,7 +41,7 @@ public function supportedStrings()
*/
public function shouldCreateEmptySlug($text)
{
$this->assertEmpty(Slugifier::slugify($text));
$this->assertEmpty(slugify($text));
}

public function notSupportedStrings()
Expand All @@ -57,7 +59,7 @@ public function notSupportedStrings()
*/
public function shouldCreateSlugWithCustomSeparator()
{
$slug = Slugifier::slugify('Wikipedia style', '_');
$slug = slugify('Wikipedia style', '_');

$this->assertEquals('wikipedia_style', $slug);
}
Expand All @@ -68,16 +70,16 @@ public function shouldCreateSlugWithCustomSeparator()
*/
public function shouldCreateSlugsWithModifiers($text, $modifier, $expectedSlug)
{
$slug = Slugifier::slugify($text, '-', $modifier);
$slug = slugify($text, '-', $modifier);

$this->assertEquals($expectedSlug, $slug);
}

public function slugModifiers()
{
return array(
array('Bu bir örnektir', Slugifier::mod('tr'), 'bu-bir-ornektir'),
array('Supernatura estaĵo', Slugifier::mod('eo'), 'supernatura-estajxo'),
array('Bu bir örnektir', mod('tr'), 'bu-bir-ornektir'),
array('Supernatura estaĵo', mod('eo'), 'supernatura-estajxo'),
array('Interesting flavors', array('o' => 'ou'), 'interesting-flavours')
);
}
Expand All @@ -88,7 +90,7 @@ public function slugModifiers()
*/
public function invalidModifierIsoShouldThrowException()
{
Slugifier::mod('invalid');
mod('invalid');
}

/**
Expand All @@ -97,7 +99,7 @@ public function invalidModifierIsoShouldThrowException()
*/
public function shouldSupportIsoModifiers($iso)
{
$this->assertNotNull(Slugifier::mod($iso));
$this->assertNotNull(mod($iso));
}

public function supportedIsoModifiers()
Expand Down

0 comments on commit b94dd6c

Please sign in to comment.