|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Inflector; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Inflector\Rules\Plural; |
|
8
|
|
|
use Doctrine\Inflector\Rules\Uninflected; |
|
9
|
|
|
use function array_keys; |
|
10
|
|
|
use function array_merge; |
|
11
|
|
|
use function implode; |
|
12
|
|
|
use function preg_match; |
|
13
|
|
|
use function preg_replace; |
|
14
|
|
|
use function strtolower; |
|
15
|
|
|
use function substr; |
|
16
|
|
|
|
|
17
|
|
|
class Pluralizer extends InflectorService |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var Uninflected */ |
|
20
|
|
|
private $uninflected; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(Uninflected $uninflected) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->uninflected = $uninflected; |
|
25
|
|
|
|
|
26
|
|
|
parent::__construct(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getRules() : array |
|
33
|
|
|
{ |
|
34
|
|
|
return Plural::RULES; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Returns a word in plural form. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $word The word in singular form. |
|
41
|
|
|
* |
|
42
|
|
|
* @return string The word in plural form. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function inflect(string $word) : string |
|
45
|
|
|
{ |
|
46
|
|
|
if (isset($this->cache['pluralize'][$word])) { |
|
47
|
|
|
return $this->cache['pluralize'][$word]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (! isset($this->rules['merged']['irregular'])) { |
|
51
|
|
|
$this->rules['merged']['irregular'] = $this->rules['irregular']; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (! isset($this->rules['merged']['uninflected'])) { |
|
55
|
|
|
$this->rules['merged']['uninflected'] = array_merge( |
|
56
|
|
|
$this->rules['uninflected'], |
|
57
|
|
|
$this->uninflected->getUninflected() |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (! isset($this->rules['cacheUninflected']) || ! isset($this->rules['cacheIrregular'])) { |
|
62
|
|
|
$this->rules['cacheUninflected'] = '(?:' . implode('|', $this->rules['merged']['uninflected']) . ')'; |
|
63
|
|
|
$this->rules['cacheIrregular'] = '(?:' . implode('|', array_keys($this->rules['merged']['irregular'])) . ')'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (preg_match('/(.*)\\b(' . $this->rules['cacheIrregular'] . ')$/i', $word, $regs)) { |
|
67
|
|
|
$this->cache['pluralize'][$word] = $regs[1] . $word[0] . substr($this->rules['merged']['irregular'][strtolower($regs[2])], 1); |
|
68
|
|
|
|
|
69
|
|
|
return $this->cache['pluralize'][$word]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (preg_match('/^(' . $this->rules['cacheUninflected'] . ')$/i', $word, $regs)) { |
|
73
|
|
|
$this->cache['pluralize'][$word] = $word; |
|
74
|
|
|
|
|
75
|
|
|
return $word; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
foreach ($this->rules['rules'] as $rule => $replacement) { |
|
79
|
|
|
if (preg_match($rule, $word)) { |
|
80
|
|
|
$this->cache['pluralize'][$word] = preg_replace($rule, $replacement, $word); |
|
81
|
|
|
|
|
82
|
|
|
return $this->cache['pluralize'][$word]; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: