This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Selami\Stdlib; | ||
|
||
use Selami\Stdlib\Exception\InvalidArgumentException; | ||
use Transliterator; | ||
|
||
class Slugifier | ||
{ | ||
/** | ||
* @var string|array $subject | ||
*/ | ||
private $subject; | ||
|
||
private Transliterator $transliterator; | ||
/** | ||
* @param string|iterable $subject | ||
*/ | ||
private function __construct($subject) | ||
{ | ||
$this->subject = $subject; | ||
$this->transliterator = Transliterator::create('Any-Latin; Latin-ASCII'); | ||
} | ||
/** | ||
* @param mixed<string|iterable> $subject | ||
* @return string|iterable<string> | ||
*/ | ||
public static function slugify($subject) | ||
{ | ||
return (new self($subject)) | ||
->getSlugifiedResult(); | ||
} | ||
/** | ||
* @return string|iterable<string> | ||
*/ | ||
private function getSlugifiedResult() | ||
{ | ||
if (is_iterable($this->subject)) { | ||
return $this->getSlugifiedIterable($this->subject); | ||
} | ||
return $this->getSlugifiedString($this->subject); | ||
} | ||
|
||
private function getSlugifiedString($subject) : string | ||
{ | ||
|
||
if (!is_string($subject)) { | ||
throw new InvalidArgumentException( | ||
sprintf('Only string or array of strings accepted but %s given', gettype($subject)) | ||
); | ||
} | ||
$stringWithRemovedDiacritics = $this->transliterator->transliterate($subject); | ||
$stringWithRemovedWhite = preg_replace(['/\-/','/[\W]+/'], [' ','-'], $stringWithRemovedDiacritics); | ||
return strtolower($stringWithRemovedWhite); | ||
} | ||
|
||
/** | ||
* @param iterable $subject | ||
* @return iterable<string> | ||
*/ | ||
private function getSlugifiedIterable(iterable $subject) : iterable | ||
{ | ||
foreach ($subject as $item) { | ||
yield $this->getSlugifiedString($item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace UnitTest; | ||
|
||
use Codeception\Test\Unit; | ||
use Selami\Stdlib\Exception\InvalidArgumentException; | ||
use Selami\Stdlib\Slugifier; | ||
|
||
class SlugifierTest extends Unit | ||
{ | ||
protected $tester; | ||
|
||
protected function _before(): void | ||
{ | ||
} | ||
|
||
protected function _after(): void | ||
{ | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider dataProvider | ||
*/ | ||
public function shouldReturnSlugifySuccessfully($subjects, $expected): void | ||
{ | ||
$actual = Slugifier::slugify($subjects); | ||
$index = 0; | ||
foreach ($actual as $item) { | ||
$this->assertEquals($expected[$index], $item, 'Returning expected slugs failed'); | ||
$index++; | ||
} | ||
} | ||
/** | ||
* @test | ||
*/ | ||
public function shouldThrowExceptionForInvalidInput(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
Slugifier::slugify(1234); | ||
} | ||
|
||
public function dataProvider() : array | ||
{ | ||
return [ | ||
[ | ||
[ | ||
'Meinung: Impfstoff-Mangel für die Ärmsten - eine moralische Bankrotterklärung', | ||
'Türkiye\'nin İstanbul Sözleşmesi serüveni', | ||
'Во Франции прошли протесты против ужесточения мер', | ||
'1234' | ||
], | ||
[ | ||
'meinung-impfstoff-mangel-fur-die-armsten-eine-moralische-bankrotterklarung', | ||
'turkiye-nin-istanbul-sozlesmesi-seruveni', | ||
'vo-francii-prosli-protesty-protiv-uzestocenia-mer', | ||
'1234' | ||
], | ||
] | ||
]; | ||
} | ||
|
||
} |