From f22144d7daae15b241ed7fcae14aada871f077c3 Mon Sep 17 00:00:00 2001 From: Keyvan Akbary Date: Sun, 2 Aug 2015 21:37:11 +0100 Subject: [PATCH] Move towards a more functional approach --- README.md | 16 ++++++----- composer.json | 4 +-- src/{Slugifier.php => slugifier.php} | 41 ++++++++++++---------------- tests/SlugifierTest.php | 18 ++++++------ 4 files changed, 38 insertions(+), 41 deletions(-) rename src/{Slugifier.php => slugifier.php} (89%) diff --git a/README.md b/README.md index dd51ca6..3ef15fd 100644 --- a/README.md +++ b/README.md @@ -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 @@ -13,11 +13,13 @@ 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 @@ -25,13 +27,13 @@ composer require keyvanakbary/slugifier 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 ``` diff --git a/composer.json b/composer.json index 0779f7c..62596e6 100644 --- a/composer.json +++ b/composer.json @@ -17,9 +17,9 @@ "phpunit/phpunit": "~4.0" }, "autoload": { - "psr-4": { "": "src/" } + "files": ["src/slugifier.php"] }, "config": { "bin-dir": "bin" } -} \ No newline at end of file +} diff --git a/src/Slugifier.php b/src/slugifier.php similarity index 89% rename from src/Slugifier.php rename to src/slugifier.php index 257688b..e87e107 100644 --- a/src/Slugifier.php +++ b/src/slugifier.php @@ -1,10 +1,10 @@ '0', 'æ' => 'ae', 'ǽ' => 'ae', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Å' => 'A', 'Ǻ' => 'A', 'Ă' => 'A', 'Ǎ' => 'A', 'Æ' => 'AE', 'Ǽ' => 'AE', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'å' => 'a', @@ -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); } diff --git a/tests/SlugifierTest.php b/tests/SlugifierTest.php index 5514a09..215579d 100644 --- a/tests/SlugifierTest.php +++ b/tests/SlugifierTest.php @@ -1,5 +1,7 @@ assertEquals($expectedSlug, $slug); } @@ -39,7 +41,7 @@ public function supportedStrings() */ public function shouldCreateEmptySlug($text) { - $this->assertEmpty(Slugifier::slugify($text)); + $this->assertEmpty(slugify($text)); } public function notSupportedStrings() @@ -57,7 +59,7 @@ public function notSupportedStrings() */ public function shouldCreateSlugWithCustomSeparator() { - $slug = Slugifier::slugify('Wikipedia style', '_'); + $slug = slugify('Wikipedia style', '_'); $this->assertEquals('wikipedia_style', $slug); } @@ -68,7 +70,7 @@ public function shouldCreateSlugWithCustomSeparator() */ public function shouldCreateSlugsWithModifiers($text, $modifier, $expectedSlug) { - $slug = Slugifier::slugify($text, '-', $modifier); + $slug = slugify($text, '-', $modifier); $this->assertEquals($expectedSlug, $slug); } @@ -76,8 +78,8 @@ public function shouldCreateSlugsWithModifiers($text, $modifier, $expectedSlug) 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') ); } @@ -88,7 +90,7 @@ public function slugModifiers() */ public function invalidModifierIsoShouldThrowException() { - Slugifier::mod('invalid'); + mod('invalid'); } /** @@ -97,7 +99,7 @@ public function invalidModifierIsoShouldThrowException() */ public function shouldSupportIsoModifiers($iso) { - $this->assertNotNull(Slugifier::mod($iso)); + $this->assertNotNull(mod($iso)); } public function supportedIsoModifiers()