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.
2224 * Import functions
2325 */
2426use function preg_replace ;
25- use function str_replace ;
27+ use function strtr ;
2628use 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}
0 commit comments