Skip to content

Commit 0b09a2a

Browse files
committed
v2
1 parent effd00f commit 0b09a2a

File tree

4 files changed

+93
-180
lines changed

4 files changed

+93
-180
lines changed

src/Exception/UnableToCreateTransliteratorException.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@
1111

1212
namespace Sunrise\Slugger\Exception;
1313

14-
/**
15-
* Import classes
16-
*/
17-
use RuntimeException;
18-
1914
/**
2015
* UnableToCreateTransliteratorException
2116
*/
22-
class UnableToCreateTransliteratorException extends RuntimeException
17+
class UnableToCreateTransliteratorException extends Exception
2318
{
2419
}

src/Exception/UnableToTransliterateException.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@
1111

1212
namespace Sunrise\Slugger\Exception;
1313

14-
/**
15-
* Import classes
16-
*/
17-
use RuntimeException;
18-
1914
/**
2015
* UnableToTransliterateException
2116
*/
22-
class UnableToTransliterateException extends RuntimeException
17+
class UnableToTransliterateException extends Exception
2318
{
2419
}

src/Slugger.php

Lines changed: 26 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,105 +11,52 @@
1111

1212
namespace Sunrise\Slugger;
1313

14+
/**
15+
* Import classes
16+
*/
17+
use Transliterator;
18+
use Sunrise\Slugger\Exception\UnableToCreateTransliteratorException;
19+
use Sunrise\Slugger\Exception\UnableToTransliterateException;
20+
1421
/**
1522
* Import functions
1623
*/
17-
use function in_array;
1824
use function preg_replace;
19-
use function sprintf;
2025
use function str_replace;
21-
use function transliterator_create;
22-
use function transliterator_list_ids;
23-
use function transliterator_transliterate;
2426
use function trim;
2527

2628
/**
2729
* Slugger
28-
*
29-
* @link http://userguide.icu-project.org/transforms/general
3030
*/
3131
class Slugger implements SluggerInterface
3232
{
3333

3434
/**
35-
* The transliterator ID
35+
* Transliterator instance
3636
*
37-
* @var string
37+
* @var Transliterator
3838
*/
39-
protected $transliteratorId = 'Russian-Latin/BGN';
39+
private $transliterator;
4040

4141
/**
42-
* Sets the transliterator ID
43-
*
44-
* @param string $transliteratorId
42+
* Constructor of the class
4543
*
46-
* @return void
44+
* @param string $basicId
4745
*
48-
* @throws Exception\UnsupportedTransliteratorIdentifierException
49-
*
50-
* @link http://userguide.icu-project.org/transforms/general#TOC-Basic-IDs
46+
* @throws UnableToCreateTransliteratorException
5147
*/
52-
public function setTransliteratorId(string $transliteratorId) : void
48+
public function __construct(string $basicId = 'Russian-Latin/BGN')
5349
{
54-
$supportedTransliteratorIds = $this->getSupportedTransliteratorIds();
55-
if (!in_array($transliteratorId, $supportedTransliteratorIds)) {
56-
throw new Exception\UnsupportedTransliteratorIdentifierException(
57-
sprintf('The transliterator ID "%s" is not supported', $transliteratorId)
58-
);
59-
}
60-
61-
$this->transliteratorId = $transliteratorId;
62-
}
50+
// http://userguide.icu-project.org/transforms/general#TOC-Basic-IDs
51+
// 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';
6353

64-
/**
65-
* Gets the transliterator ID
66-
*
67-
* @return string
68-
*/
69-
public function getTransliteratorId() : string
70-
{
71-
return $this->transliteratorId;
72-
}
73-
74-
/**
75-
* Gets supported transliterator IDs
76-
*
77-
* @return string[]
78-
*/
79-
public function getSupportedTransliteratorIds() : array
80-
{
81-
return transliterator_list_ids();
82-
}
83-
84-
/**
85-
* Transliterates the given string using the given compound
86-
*
87-
* @param string $string
88-
* @param string $compound
89-
*
90-
* @return string
91-
*
92-
* @throws Exception\UnableToCreateTransliteratorException
93-
* @throws Exception\UnableToTransliterateException
94-
*/
95-
public function transliterate(string $string, string $compound) : string
96-
{
97-
$compound = $this->getTransliteratorId() . '; ' . $compound;
98-
$transliterator = transliterator_create($compound);
54+
$transliterator = Transliterator::create($compoundIds, Transliterator::FORWARD);
9955
if (null === $transliterator) {
100-
throw new Exception\UnableToCreateTransliteratorException(
101-
sprintf('Unable to create transliterator with compound "%s"', $compound)
102-
);
103-
}
104-
105-
$transliterated = transliterator_transliterate($transliterator, $string);
106-
if (false === $transliterated) {
107-
throw new Exception\UnableToTransliterateException(
108-
sprintf('Unable to transliterate string with compound "%s"', $compound)
109-
);
56+
throw new UnableToCreateTransliteratorException('Unable to create transliterator');
11057
}
11158

112-
return $transliterated;
59+
$this->transliterator = $transliterator;
11360
}
11461

11562
/**
@@ -120,15 +67,16 @@ public function transliterate(string $string, string $compound) : string
12067
*
12168
* @return string
12269
*
123-
* @throws Exception\UnableToCreateTransliteratorException
124-
* @throws Exception\UnableToTransliterateException
70+
* @throws UnableToTransliterateException
12571
*/
12672
public function slugify(string $string, string $delimiter = '-') : string
12773
{
128-
$compound = 'Any-Latin; Latin-ASCII; Lower(); [^\x20\x30-\x39\x41-\x5A\x61-\x7A] Remove';
74+
$transliteratedString = $this->transliterator->transliterate($string);
75+
if (false === $transliteratedString) {
76+
throw new UnableToTransliterateException('Unable to transliterate');
77+
}
12978

130-
$slug = $this->transliterate($string, $compound);
131-
$slug = preg_replace('/[\x20]{2,}/', ' ', $slug);
79+
$slug = preg_replace('/[\x20]{2,}/', ' ', $transliteratedString);
13280
$slug = trim($slug);
13381
$slug = str_replace(' ', $delimiter, $slug);
13482

tests/SluggerTest.php

Lines changed: 65 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,122 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Sunrise\Slugger\Tests;
44

5+
/**
6+
* Import classes
7+
*/
58
use PHPUnit\Framework\TestCase;
9+
use Sunrise\Slugger\Exception\Exception;
610
use Sunrise\Slugger\Exception\UnableToCreateTransliteratorException;
711
use Sunrise\Slugger\Exception\UnableToTransliterateException;
8-
use Sunrise\Slugger\Exception\UnsupportedTransliteratorIdentifierException;
912
use Sunrise\Slugger\Slugger;
1013
use Sunrise\Slugger\SluggerInterface;
14+
use RuntimeException;
1115

16+
/**
17+
* SluggerTest
18+
*/
1219
class SluggerTest extends TestCase
1320
{
14-
private const RUSSIAN_LATIN_TRANSLITERATOR_ID = 'Russian-Latin/BGN';
15-
private const CYRILLIC_LATIN_TRANSLITERATOR_ID = 'Cyrillic-Latin';
1621

17-
public function testConstructor()
18-
{
19-
$slugger = new Slugger();
20-
21-
$this->assertInstanceOf(SluggerInterface::class, $slugger);
22-
}
23-
24-
public function testTransliteratorId()
25-
{
26-
$slugger = new Slugger();
27-
28-
$slugger->setTransliteratorId(self::RUSSIAN_LATIN_TRANSLITERATOR_ID);
29-
$this->assertEquals(self::RUSSIAN_LATIN_TRANSLITERATOR_ID, $slugger->getTransliteratorId());
30-
31-
$slugger->setTransliteratorId(self::CYRILLIC_LATIN_TRANSLITERATOR_ID);
32-
$this->assertEquals(self::CYRILLIC_LATIN_TRANSLITERATOR_ID, $slugger->getTransliteratorId());
33-
}
34-
35-
public function testDefaultTransliteratorId()
36-
{
37-
$slugger = new Slugger();
22+
/**
23+
* @var string
24+
*/
25+
private const RUSSIAN_LATIN_TRANSLITERATOR_BASIC_ID = 'Russian-Latin/BGN';
3826

39-
$this->assertEquals(self::RUSSIAN_LATIN_TRANSLITERATOR_ID, $slugger->getTransliteratorId());
40-
}
41-
42-
public function testUnsupportedTransliterationId()
43-
{
44-
$this->expectException(UnsupportedTransliteratorIdentifierException::class);
45-
$this->expectExceptionMessage('The transliterator ID "Morrowind-Oblivion/KFC" is not supported');
46-
47-
$slugger = new Slugger();
48-
49-
$slugger->setTransliteratorId('Morrowind-Oblivion/KFC');
50-
}
27+
/**
28+
* @var string
29+
*/
30+
private const CYRILLIC_LATIN_TRANSLITERATOR_BASIC_ID = 'Cyrillic-Latin';
5131

52-
public function testSupportedTransliteratorIds()
32+
/**
33+
* @return void
34+
*/
35+
public function testConstructor() : void
5336
{
5437
$slugger = new Slugger();
5538

56-
$this->assertEquals(\transliterator_list_ids(), $slugger->getSupportedTransliteratorIds());
57-
}
58-
59-
public function testTransliterateRussianLatin()
60-
{
61-
$input = 'съешь ещё этих мягких французских булок, да выпей чаю';
62-
$output = 'syesh yeshche etikh myagkikh frantsuzskikh bulok da vypey chayu';
63-
$compound = 'Any-Latin; Latin-ASCII; [^\x20\x41-\x5A\x61-\x7A] Remove';
64-
65-
$slugger = new Slugger();
66-
$slugger->setTransliteratorId(self::RUSSIAN_LATIN_TRANSLITERATOR_ID);
67-
68-
$this->assertEquals($output, $slugger->transliterate($input, $compound));
39+
$this->assertInstanceOf(SluggerInterface::class, $slugger);
6940
}
7041

71-
public function testTransliterateCyrillicLatin()
42+
/**
43+
* @return void
44+
*/
45+
public function testConstructorWithUnsupportedTransliteratorBasicId() : void
7246
{
73-
$input = 'съешь ещё этих мягких французских булок, да выпей чаю';
74-
$output = 'ses ese etih magkih francuzskih bulok da vypej cau';
75-
$compound = 'Any-Latin; Latin-ASCII; [^\x20\x41-\x5A\x61-\x7A] Remove';
76-
77-
$slugger = new Slugger();
78-
$slugger->setTransliteratorId(self::CYRILLIC_LATIN_TRANSLITERATOR_ID);
47+
$this->expectException(UnableToCreateTransliteratorException::class);
48+
$this->expectExceptionMessage('Unable to create transliterator');
7949

80-
$this->assertEquals($output, $slugger->transliterate($input, $compound));
50+
new Slugger('Morrowind-Oblivion/KFC');
8151
}
8252

83-
public function testSlugifyRussianLatin()
53+
/**
54+
* @return void
55+
*/
56+
public function testSlugify() : void
8457
{
8558
$input = 'съешь ещё этих мягких французских булок, да выпей чаю';
8659
$output = 'syesh-yeshche-etikh-myagkikh-frantsuzskikh-bulok-da-vypey-chayu';
87-
8860
$slugger = new Slugger();
8961

90-
$slugger->setTransliteratorId(self::RUSSIAN_LATIN_TRANSLITERATOR_ID);
91-
9262
$this->assertEquals($output, $slugger->slugify($input));
9363
}
9464

95-
public function testSlugifyCyrillicLatin()
65+
/**
66+
* @return void
67+
*/
68+
public function testSlugifyWithNumbers() : void
9669
{
97-
$input = 'съешь ещё этих мягких французских булок, да выпей чаю';
98-
$output = 'ses-ese-etih-magkih-francuzskih-bulok-da-vypej-cau';
99-
70+
$input = '0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz';
71+
$output = '0123456789-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz';
10072
$slugger = new Slugger();
10173

102-
$slugger->setTransliteratorId(self::CYRILLIC_LATIN_TRANSLITERATOR_ID);
103-
10474
$this->assertEquals($output, $slugger->slugify($input));
10575
}
10676

107-
public function testSlugifyWithDelimiter()
77+
/**
78+
* @return void
79+
*/
80+
public function testSlugifyWithDelimiter() : void
10881
{
10982
$input = ' А Б В ';
11083
$output = 'a_b_v';
111-
11284
$slugger = new Slugger();
11385

114-
$slugger->setTransliteratorId(self::RUSSIAN_LATIN_TRANSLITERATOR_ID);
115-
11686
$this->assertEquals($output, $slugger->slugify($input, '_'));
11787
}
11888

119-
public function testSlugifyWithNumbers()
89+
/**
90+
* @return void
91+
*/
92+
public function testSlugifyWithRussianLatinTransliteratorBasicId() : void
12093
{
121-
$input = '0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz';
122-
$output = '0123456789-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz';
123-
124-
$slugger = new Slugger();
94+
$input = 'съешь ещё этих мягких французских булок, да выпей чаю';
95+
$output = 'syesh-yeshche-etikh-myagkikh-frantsuzskikh-bulok-da-vypey-chayu';
96+
$slugger = new Slugger(self::RUSSIAN_LATIN_TRANSLITERATOR_BASIC_ID);
12597

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

129-
public function testTransliterateWithInvalidCompound()
101+
/**
102+
* @return void
103+
*/
104+
public function testSlugifyWithCyrillicLatinTransliteratorBasicId() : void
130105
{
131-
$expectedMessage = 'Unable to create transliterator with compound "Russian-Latin/BGN; UndefinedCommand()"';
132-
133-
$this->expectException(UnableToCreateTransliteratorException::class);
134-
$this->expectExceptionMessage($expectedMessage);
135-
136-
$slugger = new Slugger();
106+
$input = 'съешь ещё этих мягких французских булок, да выпей чаю';
107+
$output = 'ses-ese-etih-magkih-francuzskih-bulok-da-vypej-cau';
108+
$slugger = new Slugger(self::CYRILLIC_LATIN_TRANSLITERATOR_BASIC_ID);
137109

138-
$slugger->transliterate('', 'UndefinedCommand()');
110+
$this->assertEquals($output, $slugger->slugify($input));
139111
}
140112

141-
public function testExceptions()
113+
/**
114+
* @return void
115+
*/
116+
public function testExceptions() : void
142117
{
143-
$this->assertInstanceOf(\RuntimeException::class, new UnableToCreateTransliteratorException(''));
144-
$this->assertInstanceOf(\RuntimeException::class, new UnableToTransliterateException(''));
145-
$this->assertInstanceOf(\RuntimeException::class, new UnsupportedTransliteratorIdentifierException(''));
118+
$this->assertInstanceOf(\RuntimeException::class, new Exception());
119+
$this->assertInstanceOf(Exception::class, new UnableToCreateTransliteratorException());
120+
$this->assertInstanceOf(Exception::class, new UnableToTransliterateException());
146121
}
147122
}

0 commit comments

Comments
 (0)