Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @apfelbox
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI

on: [ push ]

jobs:
build-test:
runs-on: ubuntu-latest

strategy:
matrix:
php: ['8.4']

steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
coverage: "none"
ini-values: "memory_limit=-1"
php-version: "${{ matrix.php }}"
tools: "composer"

- name: Display versions
run: |
php --version
php -r 'foreach (get_loaded_extensions() as $extension) echo $extension . " " . phpversion($extension) . PHP_EOL;'
php -i

- name: Install Composer
run: composer install --optimize-autoloader --classmap-authoritative --no-interaction

- name: Run Linters
run: composer run-script lint
env:
PHP_CS_FIXER_IGNORE_ENV: 1

- name: Run Tests
run: composer run-script test
6 changes: 3 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ includes:

# If you use simple-phpunit, you need to uncomment the following line.
# Always make sure to first run simple-phpunit and then PHPStan.
# parameters:
# bootstrapFiles:
# - vendor/bin/.phpunit/phpunit/vendor/autoload.php
parameters:
bootstrapFiles:
- vendor/bin/.phpunit/phpunit/vendor/autoload.php
15 changes: 9 additions & 6 deletions src/Snail/SnailHelper.php → src/Snail/Snailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace Torr\Snail\Snail;

use Symfony\Component\String\Slugger\AsciiSlugger;
use Symfony\Component\String\UnicodeString;
use Torr\Snail\Exception\SnailGenerationFailedException;

use function Symfony\Component\String\u;

/**
* Main class that helps generating and validation snails
*
* @final
*/
class SnailHelper
class Snailer
{
private const LOCALE_TO_TRANSLITERATOR_ID = [
'am' => 'Amharic-Latin',
Expand Down Expand Up @@ -45,7 +47,7 @@ class SnailHelper
/**
* Cache of transliterators per locale.
*
* @var \Transliterator[]
* @var array<array-key, \Transliterator|null>
*/
private array $transliterators = [];

Expand All @@ -69,6 +71,7 @@ public function generateSnail (string $text, ?string $locale = null) : string
: $string->lower();

$transliterator = [];

if ($locale && ('de' === $locale || str_starts_with($locale, 'de_')))
{
// Use the shortcut for German in UnicodeString::ascii() if possible (faster and no requirement on intl)
Expand All @@ -89,7 +92,7 @@ public function generateSnail (string $text, ?string $locale = null) : string
if ("" === $transformed)
{
throw new SnailGenerationFailedException(
sprintf(
\sprintf(
"Could not generate snail from text '%s', as the result would be empty.",
$text,
),
Expand All @@ -114,8 +117,8 @@ private function createTransliterator (string $locale) : ?\Transliterator
// Exact locale supported, cache and return
if ($id = self::LOCALE_TO_TRANSLITERATOR_ID[$locale] ?? null)
{
return $this->transliterators[$locale] =
\Transliterator::create($id . '/BGN') ?? \Transliterator::create($id);
return $this->transliterators[$locale]
= \Transliterator::create($id . '/BGN') ?? \Transliterator::create($id);
}

// Locale is not supported and there is no parent, fallback to any-latin
Expand Down
27 changes: 13 additions & 14 deletions tests/Snail/SnailHelperTest.php → tests/Snail/SnailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

namespace Tests\Torr\Snail\Snail;

use Torr\Snail\Exception\SnailGenerationFailedException;
use Torr\Snail\Snail\SnailHelper;
use PHPUnit\Framework\TestCase;
use Torr\Snail\Exception\SnailGenerationFailedException;
use Torr\Snail\Snail\Snailer;

class SnailHelperTest extends TestCase
/**
* @internal
*/
final class SnailerTest extends TestCase
{
/**
*
*/
public static function provideIsValid () : iterable
{
{
yield "plain" => ["test"];
yield "with dash" => ["a-b"];
yield "with underscore" => ["a-b_c"];
Expand All @@ -26,14 +29,14 @@ public static function provideIsValid () : iterable
*/
public function testIsValid (string $input) : void
{
self::assertTrue(SnailHelper::isValidSnail($input));
self::assertTrue(Snailer::isValidSnail($input));
}

/**
*
*/
public static function provideIsInvalid () : iterable
{
{
yield "empty" => [""];
yield "dash at the end" => ["test-"];
yield "dot at the end" => ["test."];
Expand All @@ -51,11 +54,9 @@ public static function provideIsInvalid () : iterable
*/
public function testIsInvalid (string $input) : void
{
self::assertFalse(SnailHelper::isValidSnail($input));
self::assertFalse(Snailer::isValidSnail($input));
}



/**
*
*/
Expand All @@ -65,13 +66,12 @@ public static function provideGenerateValid () : iterable
yield "umlauts" => ["äöü", "aou"];
}


/**
* @dataProvider provideGenerateValid
*/
public function testGenerateValid (string $input, string $expected) : void
{
$helper = new SnailHelper();
$helper = new Snailer();

self::assertSame(
$expected,
Expand All @@ -88,19 +88,18 @@ public static function provideGenerateInvalid () : iterable
yield "invalid-chars" => ["@"];
}


/**
* @dataProvider provideGenerateInvalid
*/
public function testGenerateInvalid (string $input) : void
{
$this->expectException(SnailGenerationFailedException::class);
$this->expectExceptionMessage(sprintf(
$this->expectExceptionMessage(\sprintf(
"Could not generate snail from text '%s', as the result would be empty.",
$input,
));

$helper = new SnailHelper();
$helper = new Snailer();
$helper->generateSnail($input);
}
}