|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Inflector; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use function lcfirst; |
|
9
|
|
|
use function preg_replace; |
|
10
|
|
|
use function sprintf; |
|
11
|
|
|
use function str_replace; |
|
12
|
|
|
use function strtolower; |
|
13
|
|
|
use function ucwords; |
|
14
|
|
|
|
|
15
|
|
|
class Inflect implements InflectInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Singularizer */ |
|
18
|
|
|
private $singularizer; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Pluralizer */ |
|
21
|
|
|
private $pluralizer; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(Singularizer $singularizer, Pluralizer $pluralizer) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->singularizer = $singularizer; |
|
26
|
|
|
$this->pluralizer = $pluralizer; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function tableize(string $word) : string |
|
33
|
|
|
{ |
|
34
|
|
|
return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $word)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function classify(string $word) : string |
|
41
|
|
|
{ |
|
42
|
|
|
return str_replace([' ', '_', '-'], '', ucwords($word, ' _-')); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function camelize(string $word) : string |
|
49
|
|
|
{ |
|
50
|
|
|
return lcfirst($this->classify($word)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-") : string |
|
57
|
|
|
{ |
|
58
|
|
|
return ucwords($string, $delimiters); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function reset() : void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->singularizer->reset(); |
|
67
|
|
|
$this->pluralizer->reset(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function addRules(string $type, array $rules, bool $reset = false) : void |
|
74
|
|
|
{ |
|
75
|
|
|
switch ($type) { |
|
76
|
|
|
case 'plural': |
|
77
|
|
|
$this->pluralizer->addRules($rules, $reset); |
|
78
|
|
|
|
|
79
|
|
|
return; |
|
80
|
|
|
|
|
81
|
|
|
case 'singular': |
|
82
|
|
|
$this->singularizer->addRules($rules, $reset); |
|
83
|
|
|
|
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
throw new InvalidArgumentException(sprintf('Invalid rule type specified "%s"', $type)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function pluralize(string $word) : string |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->pluralizer->inflect($word); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function singularize(string $word) : string |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->singularizer->inflect($word); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|