|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Inflector\Rules; |
|
6
|
|
|
|
|
7
|
|
|
use function array_keys; |
|
8
|
|
|
use function implode; |
|
9
|
|
|
use function preg_match; |
|
10
|
|
|
use function preg_replace; |
|
11
|
|
|
use function strtolower; |
|
12
|
|
|
use function substr; |
|
13
|
|
|
|
|
14
|
|
|
class Ruleset |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string[] */ |
|
17
|
|
|
private $rules = []; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string[] */ |
|
20
|
|
|
private $uninflected = []; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string[] */ |
|
23
|
|
|
private $irregular = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string[] $rules |
|
27
|
|
|
* @param string[] $uninflected |
|
28
|
|
|
* @param string[] $irregular |
|
29
|
|
|
*/ |
|
30
|
526 |
|
public function __construct(array $rules, array $uninflected, array $irregular) |
|
31
|
|
|
{ |
|
32
|
526 |
|
$this->rules = $rules; |
|
|
|
|
|
|
33
|
526 |
|
$this->uninflected = $uninflected; |
|
34
|
526 |
|
$this->irregular = $irregular; |
|
|
|
|
|
|
35
|
526 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string[] |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getRules() : array |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->rules; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return string[] |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getUninflected() : array |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->uninflected; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return string[] |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getIrregular() : array |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->irregular; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Adds custom inflection rules. |
|
63
|
|
|
* |
|
64
|
|
|
* $singularRuleset->addRules(['/^(inflect)or$/i' => '\1ables']); |
|
65
|
|
|
* $pluralRuleset->addRules([ |
|
66
|
|
|
* 'rules' => ['/^(inflect)ors$/i' => '\1ables'], |
|
67
|
|
|
* 'uninflected' => ['dontinflectme'], |
|
68
|
|
|
* 'irregular' => ['red' => 'redlings'] |
|
69
|
|
|
* ]); |
|
70
|
|
|
* |
|
71
|
|
|
* @param mixed[] $rules An array of rules to be added. new rules that are being defined in $rules. |
|
72
|
|
|
* @param bool $reset If true, will unset default inflections for all new rules that are being defined in $rules. |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function addRules(array $rules, bool $reset = false) : void |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
4 |
|
if ($this->isFullSyntax($rules)) { |
|
77
|
4 |
|
$this->rules = $rules['rules'] ?? $this->rules; |
|
|
|
|
|
|
78
|
4 |
|
$this->uninflected = $rules['uninflected'] ?? $this->uninflected; |
|
79
|
4 |
|
$this->irregular = $rules['irregular'] ?? $this->irregular; |
|
|
|
|
|
|
80
|
|
|
} else { |
|
81
|
2 |
|
$this->rules = $rules; |
|
82
|
|
|
} |
|
83
|
4 |
|
} |
|
84
|
|
|
|
|
85
|
510 |
|
public function inflect(string $word) : string |
|
86
|
|
|
{ |
|
87
|
510 |
|
$irregularRegex = '(?:' . implode('|', array_keys($this->irregular)) . ')'; |
|
88
|
|
|
|
|
89
|
510 |
|
if (preg_match('/(.*)\\b(' . $irregularRegex . ')$/i', $word, $regs)) { |
|
90
|
93 |
|
return $regs[1] . $word[0] . substr($this->irregular[strtolower($regs[2])], 1); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
420 |
|
$uninflectedRegex = '(?:' . implode('|', $this->uninflected) . ')'; |
|
94
|
|
|
|
|
95
|
420 |
|
if (preg_match('/^(' . $uninflectedRegex . ')$/i', $word, $regs)) { |
|
96
|
244 |
|
return $word; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
179 |
|
foreach ($this->rules as $rule => $replacement) { |
|
100
|
179 |
|
if (preg_match($rule, $word)) { |
|
101
|
179 |
|
return preg_replace($rule, $replacement, $word); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
2 |
|
return $word; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param mixed[] $rules |
|
110
|
|
|
*/ |
|
111
|
4 |
|
private function isFullSyntax(array $rules) : bool |
|
112
|
|
|
{ |
|
113
|
4 |
|
return isset($rules['rules']) || isset($rules['irregular']) || isset($rules['uninflected']); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.