Skip to content

Commit cdfb60d

Browse files
committed
withIrregulars method for InflectorFactory
1 parent bd3ebc2 commit cdfb60d

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

lib/Doctrine/Inflector/GenericLanguageInflectorFactory.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
namespace Doctrine\Inflector;
66

77
use Doctrine\Inflector\Rules\Ruleset;
8+
use Doctrine\Inflector\Rules\Word;
9+
use Doctrine\Inflector\Rules\Patterns;
10+
use Doctrine\Inflector\Rules\Substitution;
11+
use Doctrine\Inflector\Rules\Substitutions;
12+
use Doctrine\Inflector\Rules\Transformations;
813

914
use function array_unshift;
1015

@@ -60,6 +65,31 @@ final public function withPluralRules(?Ruleset $pluralRules, bool $reset = false
6065
return $this;
6166
}
6267

68+
final public function withIrregulars(array $irregulars, bool $reset = false): LanguageInflectorFactory
69+
{
70+
if ($reset) {
71+
$this->pluralRulesets = [];
72+
$this->singularRulesets = [];
73+
}
74+
75+
$newIrregulars = array();
76+
foreach ($irregulars as $irregular) {
77+
$newIrregulars[] = new Substitution(new Word($irregular[0]), new Word($irregular[1]));
78+
}
79+
80+
$transf = new Transformations();
81+
$patterns = new Patterns();
82+
$substs = new Substitutions(...$newIrregulars);
83+
84+
$plural = new Ruleset($transf, $patterns, $substs);
85+
$singular = new Ruleset($transf, $patterns, $substs->getFlippedSubstitutions());
86+
87+
array_unshift($this->pluralRulesets, $plural);
88+
array_unshift($this->singularRulesets, $singular);
89+
90+
return $this;
91+
}
92+
6393
abstract protected function getSingularRuleset(): Ruleset;
6494

6595
abstract protected function getPluralRuleset(): Ruleset;

lib/Doctrine/Inflector/LanguageInflectorFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ public function withSingularRules(?Ruleset $singularRules, bool $reset = false):
2626
*/
2727
public function withPluralRules(?Ruleset $pluralRules, bool $reset = false): self;
2828

29+
/**
30+
* Applies custom rules for irregular words
31+
*
32+
* @param bool $reset If true, will unset default inflections for all new rules
33+
*
34+
* @return $this
35+
*/
36+
public function withIrregulars(array $irregulars, bool $reset = false): self;
37+
2938
/**
3039
* Builds the inflector instance with all applicable rules
3140
*/
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Inflector;
6+
7+
use Doctrine\Inflector\Inflector;
8+
use Doctrine\Inflector\InflectorFactory;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class InflectorWithIrregularsTest extends TestCase
12+
{
13+
/** @var Inflector */
14+
private $inflector;
15+
16+
/**
17+
* @dataProvider dataIrregulars
18+
*/
19+
public function testIrregulars(string $word, string $expected): void
20+
{
21+
self::assertSame($expected, $this->inflector->pluralize($word));
22+
self::assertSame($word, $this->inflector->singularize($expected));
23+
}
24+
/**
25+
* @dataProvider dataRegulars
26+
*/
27+
public function testRegulars(string $word, string $expected): void
28+
{
29+
self::assertSame($expected, $this->inflector->pluralize($word));
30+
self::assertSame($word, $this->inflector->singularize($expected));
31+
}
32+
33+
/**
34+
* Strings which are used for testTableize.
35+
*
36+
* @return string[][]
37+
*/
38+
public function dataRegulars(): array
39+
{
40+
// In the format array('word', 'expected')
41+
return [
42+
['address', 'addresses'],
43+
['advice', 'advice'],
44+
['agency', 'agencies'],
45+
['aircraft', 'aircraft'],
46+
['alias', 'aliases'],
47+
];
48+
}
49+
50+
/**
51+
* Strings which are used for testIrregulars.
52+
*
53+
* @return string[][]
54+
*/
55+
public function dataIrregulars(): array
56+
{
57+
// In the format array('word', 'expected')
58+
return [
59+
['foobar', 'barfoo'],
60+
['test', 'testz']
61+
];
62+
}
63+
64+
protected function setUp(): void
65+
{
66+
$this->inflector = InflectorFactory::create()->withIrregulars($this->dataIrregulars())->build();
67+
}
68+
}

0 commit comments

Comments
 (0)