Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvanakbary committed Dec 27, 2014
0 parents commit 1d2a455
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
composer.lock
composer.phar
bin/
vendor/
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- hhvm

before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev

script: bin/phpunit
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2014 Keyvan Akbary

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Slugifier

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

Just a simple and clean implementation for creating [slugs](http://en.wikipedia.org/wiki/Semantic_URL#Slug)

## Setup and Configuration
Add the following to your `composer.json` file
```json
{
"require": {
"keyvanakbary/slugifier": "*"
}
}
```

Update the vendor libraries

curl -s http://getcomposer.org/installer | php
php composer.phar install

## Usage

```php
<?php

include 'vendor/autoload.php';

$slugifier = new \Slugifier();

echo $slugifier->slugify('This is an awesome slug');
//this-is-an-awesome-slug

echo $slugifier->slugify('Cumpleaños del murciélago');
//cumpleanos-del-murcielago

echo $slugifier->slugify('Wikipedia style', '_');
//wikipedia_style
```
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "keyvanakbary/slugifier",
"description": "Just a simple implementation for creating slugs",
"keywords": ["slugifier", "slug", "slugify", "url"],
"license": "MIT",
"minimum-stability": "stable",
"authors": [
{
"name": "Keyvan Akbary",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-4": { "": "src/" }
},
"config": {
"bin-dir": "bin"
}
}
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "true"
syntaxCheck = "false"
bootstrap = "vendor/autoload.php" >

<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

</phpunit>
38 changes: 38 additions & 0 deletions src/Slugifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

private static $table = array(
'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C',
'č'=>'c', 'Ć'=>'C', 'ć'=>'c', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A',
'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E',
'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O',
'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a',
'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e',
'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i',
'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o',
'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y',
'Ŕ'=>'R', 'ŕ'=>'r', 'ü' => 'u', 'ƒ' => 'f',
);

public function slugify($text, $separator = '-')
{
$text = $this->normalize($text);
$text = $this->replaceUnwantedChars($text, $separator);

return trim($text, $separator);
}

private function normalize($text)
{
return strtolower(strtr($text, self::$table));
}

private function replaceUnwantedChars($text, $separator)
{
return preg_replace(self::UNWANTED_CHARS, $separator, $text);
}
}
52 changes: 52 additions & 0 deletions tests/SlugifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

class SlugifierTest extends \PHPUnit_Framework_TestCase
{
private $slugifier;

protected function setUp()
{
$this->slugifier = new Slugifier();
}

/**
* @test
* @dataProvider texts
*/
public function shouldCreateSlug($text, $expectedSlug)
{
$slug = $this->slugifier->slugify($text);

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

public function texts()
{
return array(
array('Word', 'word'),
array('An awesome slug', 'an-awesome-slug'),
array(' should trim this text ', 'should-trim-this-text'),
array('Práctica de acentuación', 'practica-de-acentuacion'),
array('Cumpleaños del muerciélago', 'cumpleanos-del-muercielago')
);
}

/**
* @test
* @dataProvider emptySlugs
*/
public function shouldCreateEmptySlug($text)
{
$this->assertEmpty($this->slugifier->slugify($text));
}

public function emptySlugs()
{
return array(
array(' ..`-. '),
array('테스트'),
array('اختبار'),
array('- --'),
);
}
}

0 comments on commit 1d2a455

Please sign in to comment.