Skip to content

Commit

Permalink
Merge pull request #16 from sunrise-php/release/v2.1.0
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
fenric authored Aug 8, 2021
2 parents 7c3d581 + 1ee7a80 commit 610f89c
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.phpunit.result.cache
composer.lock
coverage.xml
phpbench.json
phpcs.xml
phpunit.xml
vendor/
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Simple slugger for PHP 7.1+ (incl. PHP8) based on ICU
# Simple slugger for PHP 7.1+ (incl. PHP 8) based on ICU

[![Gitter](https://badges.gitter.im/sunrise-php/support.png)](https://gitter.im/sunrise-php/support)
[![Build Status](https://circleci.com/gh/sunrise-php/slugger.svg?style=shield)](https://circleci.com/gh/sunrise-php/slugger)
Expand Down Expand Up @@ -42,6 +42,18 @@ $slugger = new Slugger('de-ASCII');
$slugger->slugify('Falsches Üben von Xylophonmusik quält jeden größeren Zwerg');
```

#### Custom replacements

```php
$slugger = new Slugger(null, [
'.' => ' dot ',
'@' => ' at ',
]);

// [email protected]
$slugger->slugify('admin-at-acme-dot-com');
```

## Useful links

* http://site.icu-project.org/
Expand Down
15 changes: 7 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"name": "sunrise/slugger",
"homepage": "https://github.com/sunrise-php/slugger",
"description": "Simple slugger for PHP 7.1+ based on ICU",
"description": "Simple slugger for PHP 7.1+ (incl. PHP 8) based on ICU",
"license": "MIT",
"keywords": [
"fenric",
"sunrise",
"slugger",
"translit",
"icu",
"php7",
"php8"
Expand All @@ -20,12 +19,12 @@
}
],
"require": {
"php": "^7.1|^8.0",
"ext-intl": "*"
"ext-intl": "*",
"php": "^7.1|^8.0"
},
"require-dev": {
"phpunit/phpunit": "7.5.20|9.5.0",
"sunrise/coding-standard": "1.0.0"
"sunrise/coding-standard": "1.0.0",
"phpunit/phpunit": "7.5.20|9.5.0"
},
"autoload": {
"psr-4": {
Expand All @@ -34,8 +33,8 @@
},
"scripts": {
"test": [
"phpunit --colors=always --coverage-text",
"phpcs"
"phpcs",
"XDEBUG_MODE=coverage phpunit --colors=always --coverage-text"
]
}
}
5 changes: 4 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0"?>
<phpunit colors="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit
colors="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./src</directory>
Expand Down
8 changes: 5 additions & 3 deletions src/Exception/Exception.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/**
* It's free open-source software released under the MIT License.
Expand All @@ -14,11 +16,11 @@
/**
* Import classes
*/
use RuntimeException;
use Exception as BaseException;

/**
* Exception
*/
class Exception extends RuntimeException
class Exception extends BaseException implements ExceptionInterface
{
}
26 changes: 26 additions & 0 deletions src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/**
* It's free open-source software released under the MIT License.
*
* @author Anatoly Fenric <[email protected]>
* @copyright Copyright (c) 2018, Anatoly Fenric
* @license https://github.com/sunrise-php/slugger/blob/master/LICENSE
* @link https://github.com/sunrise-php/slugger
*/

namespace Sunrise\Slugger\Exception;

/**
* Import classes
*/
use Throwable as BaseExceptionInterface;

/**
* ExceptionInterface
*/
interface ExceptionInterface extends BaseExceptionInterface
{
}
4 changes: 3 additions & 1 deletion src/Exception/UnableToCreateTransliteratorException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/**
* It's free open-source software released under the MIT License.
Expand Down
4 changes: 3 additions & 1 deletion src/Exception/UnableToTransliterateException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/**
* It's free open-source software released under the MIT License.
Expand Down
45 changes: 27 additions & 18 deletions src/Slugger.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/**
* It's free open-source software released under the MIT License.
Expand All @@ -22,7 +24,7 @@
* Import functions
*/
use function preg_replace;
use function str_replace;
use function strtr;
use function trim;

/**
Expand All @@ -32,54 +34,61 @@ class Slugger implements SluggerInterface
{

/**
* Transliterator instance
* Transliterator
*
* @var Transliterator
*/
private $transliterator;

/**
* Replacements
*
* @var array<string,string>
*/
private $replacements = [
"'" => '', // <= <Ь>
'"' => '', // <= <Ъ>
];

/**
* Constructor of the class
*
* @param string $basicId
* @param array<string,string> $replacements
*
* @throws UnableToCreateTransliteratorException
*/
public function __construct(string $basicId = 'Russian-Latin/BGN')
public function __construct(?string $basicId = null, array $replacements = [])
{
// http://userguide.icu-project.org/transforms/general#TOC-Basic-IDs
// http://userguide.icu-project.org/transforms/general#TOC-Compound-IDs
$compoundIds = $basicId . '; Any-Latin; Latin-ASCII; Lower(); [^\x20\x30-\x39\x41-\x5A\x61-\x7A] Remove';
$compoundIds = ($basicId ?? 'Russian-Latin/BGN') . '; Any-Latin; Latin-ASCII; Lower()';

$transliterator = Transliterator::create($compoundIds, Transliterator::FORWARD);
if (null === $transliterator) {
throw new UnableToCreateTransliteratorException('Unable to create transliterator');
}

$this->transliterator = $transliterator;
$this->replacements += $replacements;
}

/**
* Converts the given string to slug
*
* @param string $string
* @param string $delimiter
*
* @return string
* {@inheritdoc}
*
* @throws UnableToTransliterateException
*/
public function slugify(string $string, string $delimiter = '-') : string
public function slugify(string $string, string $separator = '-') : string
{
$transliteratedString = $this->transliterator->transliterate($string);
if (false === $transliteratedString) {
$result = $this->transliterator->transliterate($string);
if (false === $result) {
throw new UnableToTransliterateException('Unable to transliterate');
}

$slug = preg_replace('/[\x20]{2,}/', ' ', $transliteratedString);
$slug = trim($slug);
$slug = str_replace(' ', $delimiter, $slug);
$result = strtr($result, $this->replacements);
$result = preg_replace('/[^0-9A-Za-z]++/', $separator, $result);
$result = trim($result, $separator);

return $slug;
return $result;
}
}
12 changes: 7 additions & 5 deletions src/SluggerInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

/**
* It's free open-source software released under the MIT License.
Expand All @@ -18,14 +20,14 @@ interface SluggerInterface
{

/**
* Converts the given string to slug
* Slugifies the given string
*
* @param string $string
* @param string $delimiter
* @param string $separator
*
* @return string
*
* @throws \RuntimeException
* @throws Exception\ExceptionInterface
*/
public function slugify(string $string, string $delimiter) : string;
public function slugify(string $string, string $separator) : string;
}
44 changes: 39 additions & 5 deletions tests/SluggerTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Sunrise\Slugger\Tests;

Expand All @@ -7,11 +9,11 @@
*/
use PHPUnit\Framework\TestCase;
use Sunrise\Slugger\Exception\Exception;
use Sunrise\Slugger\Exception\ExceptionInterface;
use Sunrise\Slugger\Exception\UnableToCreateTransliteratorException;
use Sunrise\Slugger\Exception\UnableToTransliterateException;
use Sunrise\Slugger\Slugger;
use Sunrise\Slugger\SluggerInterface;
use RuntimeException;

/**
* SluggerTest
Expand All @@ -32,7 +34,7 @@ class SluggerTest extends TestCase
/**
* @return void
*/
public function testConstructor() : void
public function testContracts() : void
{
$slugger = new Slugger();

Expand Down Expand Up @@ -77,7 +79,7 @@ public function testSlugifyWithNumbers() : void
/**
* @return void
*/
public function testSlugifyWithDelimiter() : void
public function testSlugifyWithSeparator() : void
{
$input = ' А Б В ';
$output = 'a_b_v';
Expand Down Expand Up @@ -110,13 +112,45 @@ public function testSlugifyWithCyrillicLatinTransliteratorBasicId() : void
$this->assertEquals($output, $slugger->slugify($input));
}

/**
* @return void
*/
public function testReplacements() : void
{
$input = 'У меня есть 1$ и 2€';
$output = 'u-menya-yest-1-dollar-i-2-euro';
$slugger = new Slugger(self::RUSSIAN_LATIN_TRANSLITERATOR_BASIC_ID, [
'$' => ' dollar ',
'' => ' euro ',
]);

$this->assertEquals($output, $slugger->slugify($input));
}

/**
* @return void
*/
public function testPunctuations() : void
{
$input = 'С.Т.А.Л.К.Е.Р.';
$output = 's-t-a-l-k-ye-r';
$slugger = new Slugger(self::RUSSIAN_LATIN_TRANSLITERATOR_BASIC_ID);

$this->assertEquals($output, $slugger->slugify($input));
}

/**
* @return void
*/
public function testExceptions() : void
{
$this->assertInstanceOf(\RuntimeException::class, new Exception());
$this->assertInstanceOf(\Throwable::class, new Exception());
$this->assertInstanceOf(ExceptionInterface::class, new Exception());

$this->assertInstanceOf(ExceptionInterface::class, new UnableToCreateTransliteratorException());
$this->assertInstanceOf(Exception::class, new UnableToCreateTransliteratorException());

$this->assertInstanceOf(ExceptionInterface::class, new UnableToTransliterateException());
$this->assertInstanceOf(Exception::class, new UnableToTransliterateException());
}
}

0 comments on commit 610f89c

Please sign in to comment.