Skip to content

Commit 610f89c

Browse files
authored
Merge pull request #16 from sunrise-php/release/v2.1.0
v2.1.0
2 parents 7c3d581 + 1ee7a80 commit 610f89c

11 files changed

+135
-43
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.phpunit.result.cache
33
composer.lock
44
coverage.xml
5+
phpbench.json
56
phpcs.xml
67
phpunit.xml
78
vendor/

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Simple slugger for PHP 7.1+ (incl. PHP8) based on ICU
1+
# Simple slugger for PHP 7.1+ (incl. PHP 8) based on ICU
22

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

45+
#### Custom replacements
46+
47+
```php
48+
$slugger = new Slugger(null, [
49+
'.' => ' dot ',
50+
'@' => ' at ',
51+
]);
52+
53+
// admin@acme.com
54+
$slugger->slugify('admin-at-acme-dot-com');
55+
```
56+
4557
## Useful links
4658

4759
* http://site.icu-project.org/

composer.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
22
"name": "sunrise/slugger",
33
"homepage": "https://github.com/sunrise-php/slugger",
4-
"description": "Simple slugger for PHP 7.1+ based on ICU",
4+
"description": "Simple slugger for PHP 7.1+ (incl. PHP 8) based on ICU",
55
"license": "MIT",
66
"keywords": [
77
"fenric",
88
"sunrise",
99
"slugger",
10-
"translit",
1110
"icu",
1211
"php7",
1312
"php8"
@@ -20,12 +19,12 @@
2019
}
2120
],
2221
"require": {
23-
"php": "^7.1|^8.0",
24-
"ext-intl": "*"
22+
"ext-intl": "*",
23+
"php": "^7.1|^8.0"
2524
},
2625
"require-dev": {
27-
"phpunit/phpunit": "7.5.20|9.5.0",
28-
"sunrise/coding-standard": "1.0.0"
26+
"sunrise/coding-standard": "1.0.0",
27+
"phpunit/phpunit": "7.5.20|9.5.0"
2928
},
3029
"autoload": {
3130
"psr-4": {
@@ -34,8 +33,8 @@
3433
},
3534
"scripts": {
3635
"test": [
37-
"phpunit --colors=always --coverage-text",
38-
"phpcs"
36+
"phpcs",
37+
"XDEBUG_MODE=coverage phpunit --colors=always --coverage-text"
3938
]
4039
}
4140
}

phpunit.xml.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?xml version="1.0"?>
2-
<phpunit colors="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
2+
<phpunit
3+
colors="true"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
36
<coverage>
47
<include>
58
<directory>./src</directory>

src/Exception/Exception.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
/**
46
* It's free open-source software released under the MIT License.
@@ -14,11 +16,11 @@
1416
/**
1517
* Import classes
1618
*/
17-
use RuntimeException;
19+
use Exception as BaseException;
1820

1921
/**
2022
* Exception
2123
*/
22-
class Exception extends RuntimeException
24+
class Exception extends BaseException implements ExceptionInterface
2325
{
2426
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* It's free open-source software released under the MIT License.
7+
*
8+
* @author Anatoly Fenric <anatoly@fenric.ru>
9+
* @copyright Copyright (c) 2018, Anatoly Fenric
10+
* @license https://github.com/sunrise-php/slugger/blob/master/LICENSE
11+
* @link https://github.com/sunrise-php/slugger
12+
*/
13+
14+
namespace Sunrise\Slugger\Exception;
15+
16+
/**
17+
* Import classes
18+
*/
19+
use Throwable as BaseExceptionInterface;
20+
21+
/**
22+
* ExceptionInterface
23+
*/
24+
interface ExceptionInterface extends BaseExceptionInterface
25+
{
26+
}

src/Exception/UnableToCreateTransliteratorException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
/**
46
* It's free open-source software released under the MIT License.

src/Exception/UnableToTransliterateException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
/**
46
* It's free open-source software released under the MIT License.

src/Slugger.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
/**
46
* It's free open-source software released under the MIT License.
@@ -22,7 +24,7 @@
2224
* Import functions
2325
*/
2426
use function preg_replace;
25-
use function str_replace;
27+
use function strtr;
2628
use function trim;
2729

2830
/**
@@ -32,54 +34,61 @@ class Slugger implements SluggerInterface
3234
{
3335

3436
/**
35-
* Transliterator instance
37+
* Transliterator
3638
*
3739
* @var Transliterator
3840
*/
3941
private $transliterator;
4042

43+
/**
44+
* Replacements
45+
*
46+
* @var array<string,string>
47+
*/
48+
private $replacements = [
49+
"'" => '', // <= <Ь>
50+
'"' => '', // <= <Ъ>
51+
];
52+
4153
/**
4254
* Constructor of the class
4355
*
4456
* @param string $basicId
57+
* @param array<string,string> $replacements
4558
*
4659
* @throws UnableToCreateTransliteratorException
4760
*/
48-
public function __construct(string $basicId = 'Russian-Latin/BGN')
61+
public function __construct(?string $basicId = null, array $replacements = [])
4962
{
5063
// http://userguide.icu-project.org/transforms/general#TOC-Basic-IDs
5164
// http://userguide.icu-project.org/transforms/general#TOC-Compound-IDs
52-
$compoundIds = $basicId . '; Any-Latin; Latin-ASCII; Lower(); [^\x20\x30-\x39\x41-\x5A\x61-\x7A] Remove';
65+
$compoundIds = ($basicId ?? 'Russian-Latin/BGN') . '; Any-Latin; Latin-ASCII; Lower()';
5366

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

5972
$this->transliterator = $transliterator;
73+
$this->replacements += $replacements;
6074
}
6175

6276
/**
63-
* Converts the given string to slug
64-
*
65-
* @param string $string
66-
* @param string $delimiter
67-
*
68-
* @return string
77+
* {@inheritdoc}
6978
*
7079
* @throws UnableToTransliterateException
7180
*/
72-
public function slugify(string $string, string $delimiter = '-') : string
81+
public function slugify(string $string, string $separator = '-') : string
7382
{
74-
$transliteratedString = $this->transliterator->transliterate($string);
75-
if (false === $transliteratedString) {
83+
$result = $this->transliterator->transliterate($string);
84+
if (false === $result) {
7685
throw new UnableToTransliterateException('Unable to transliterate');
7786
}
7887

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

83-
return $slug;
92+
return $result;
8493
}
8594
}

src/SluggerInterface.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

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

2022
/**
21-
* Converts the given string to slug
23+
* Slugifies the given string
2224
*
2325
* @param string $string
24-
* @param string $delimiter
26+
* @param string $separator
2527
*
2628
* @return string
2729
*
28-
* @throws \RuntimeException
30+
* @throws Exception\ExceptionInterface
2931
*/
30-
public function slugify(string $string, string $delimiter) : string;
32+
public function slugify(string $string, string $separator) : string;
3133
}

0 commit comments

Comments
 (0)