-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(support): adds string pluralizer
- Loading branch information
Showing
10 changed files
with
195 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
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
50 changes: 50 additions & 0 deletions
50
src/Tempest/Support/src/Pluralizer/InflectorPluralizer.php
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Support\Pluralizer; | ||
|
||
use Countable; | ||
use Doctrine\Inflector\Inflector; | ||
use Doctrine\Inflector\InflectorFactory; | ||
|
||
final class InflectorPluralizer implements Pluralizer | ||
{ | ||
private Inflector $inflector; | ||
|
||
public function __construct(string $language = 'english') | ||
{ | ||
$this->inflector = InflectorFactory::createForLanguage($language)->build(); | ||
} | ||
|
||
public function pluralize(string $value, int|array|Countable $count = 2): string | ||
{ | ||
if (is_countable($count)) { | ||
$count = count($count); | ||
} | ||
|
||
if ((int) abs($count) === 1 || preg_match('/^(.*)[A-Za-z0-9\x{0080}-\x{FFFF}]$/u', $value) == 0) { | ||
return $value; | ||
} | ||
|
||
return $this->matchCase($this->inflector->pluralize($value), $value); | ||
} | ||
|
||
public function singularize(string $value): string | ||
{ | ||
return $this->matchCase($this->inflector->singularize($value), $value); | ||
} | ||
|
||
private function matchCase(string $value, string $comparison): string | ||
{ | ||
$functions = ['mb_strtolower', 'mb_strtoupper', 'ucfirst', 'ucwords']; | ||
|
||
foreach ($functions as $function) { | ||
if ($function($comparison) === $comparison) { | ||
return $function($value); | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Support\Pluralizer; | ||
|
||
use Countable; | ||
|
||
interface Pluralizer | ||
{ | ||
public function pluralize(string $value, int|array|Countable $count = 2): string; | ||
|
||
public function singularize(string $value): string; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Tempest/Support/src/Pluralizer/PluralizerInitializer.php
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Support\Pluralizer; | ||
|
||
use Tempest\Container\Container; | ||
use Tempest\Container\Initializer; | ||
|
||
final readonly class PluralizerInitializer implements Initializer | ||
{ | ||
public function initialize(Container $container): Pluralizer | ||
{ | ||
return new InflectorPluralizer(); | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Support\Tests; | ||
|
||
use PHPUnit\Framework\Attributes\TestWith; | ||
use PHPUnit\Framework\TestCase; | ||
use Tempest\Support\LanguageHelper; | ||
|
||
/** | ||
* @internal | ||
* @small | ||
*/ | ||
final class LanguageHelperTest extends TestCase | ||
{ | ||
#[TestWith([['Jon', 'Jane'], 'Jon and Jane'])] | ||
#[TestWith([['Jon', 'Jane', 'Jill'], 'Jon, Jane and Jill'])] | ||
public function test_join(array $parts, string $expected): void | ||
{ | ||
$this->assertEquals($expected, LanguageHelper::join($parts)); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Tempest/Support/tests/Pluralizer/InflectorPluralizerTest.php
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Support\Tests\Pluralizer; | ||
|
||
use Countable; | ||
use PHPUnit\Framework\Attributes\TestWith; | ||
use PHPUnit\Framework\TestCase; | ||
use Tempest\Support\Pluralizer\InflectorPluralizer; | ||
|
||
/** | ||
* @internal | ||
* @small | ||
*/ | ||
final class InflectorPluralizerTest extends TestCase | ||
{ | ||
#[TestWith(['migration', 'migrations', 0])] | ||
#[TestWith(['migration', 'migration', 1])] | ||
#[TestWith(['Migration', 'Migrations', 2])] | ||
#[TestWith(['migration', 'migrations', 2])] | ||
#[TestWith(['migration', 'migrations', [1, 2]])] | ||
public function test_that_pluralizer_pluralizes(string $value, string $expected, int|array|Countable $count): void | ||
{ | ||
$pluralizer = new InflectorPluralizer(); | ||
|
||
$this->assertEquals($expected, $pluralizer->pluralize($value, $count)); | ||
} | ||
|
||
#[TestWith(['Migrations', 'Migration'])] | ||
#[TestWith(['migrations', 'migration'])] | ||
public function test_that_pluralizer_singularizes(string $value, string $expected): void | ||
{ | ||
$pluralizer = new InflectorPluralizer(); | ||
|
||
$this->assertEquals($expected, $pluralizer->singularize($value)); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Integration\Support; | ||
|
||
use Countable; | ||
use PHPUnit\Framework\Attributes\TestWith; | ||
use Tempest\Support\LanguageHelper; | ||
use Tests\Tempest\Integration\FrameworkIntegrationTestCase; | ||
|
||
/** | ||
* @internal | ||
* @small | ||
*/ | ||
final class LanguageHelperTest extends FrameworkIntegrationTestCase | ||
{ | ||
#[TestWith(['migration', 'migrations', 0])] | ||
#[TestWith(['migration', 'migration', 1])] | ||
#[TestWith(['Migration', 'Migrations', 2])] | ||
#[TestWith(['migration', 'migrations', 2])] | ||
#[TestWith(['migration', 'migrations', [1, 2]])] | ||
public function test_that_pluralize_pluralizes(string $value, string $expected, int|array|Countable $count): void | ||
{ | ||
$this->assertEquals($expected, LanguageHelper::pluralize($value, $count)); | ||
} | ||
|
||
#[TestWith(['Migrations', 'Migration'])] | ||
#[TestWith(['migrations', 'migration'])] | ||
public function test_that_pluralizer_singularizes(string $value, string $expected): void | ||
{ | ||
$this->assertEquals($expected, LanguageHelper::singularize($value)); | ||
} | ||
} |