1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Inflector; |
6
|
|
|
|
7
|
|
|
use function lcfirst; |
8
|
|
|
use function mb_strtolower; |
9
|
|
|
use function preg_replace; |
10
|
|
|
use function str_replace; |
11
|
|
|
use function ucwords; |
12
|
|
|
|
13
|
|
|
class Inflector |
14
|
|
|
{ |
15
|
|
|
/** @var WordInflector */ |
16
|
|
|
private $singularInflector; |
17
|
|
|
|
18
|
|
|
/** @var WordInflector */ |
19
|
|
|
private $pluralInflector; |
20
|
|
|
|
21
|
535 |
|
public function __construct(WordInflector $singularInflector, WordInflector $pluralInflector) |
22
|
|
|
{ |
23
|
535 |
|
$this->singularInflector = $singularInflector; |
24
|
535 |
|
$this->pluralInflector = $pluralInflector; |
25
|
535 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'. |
29
|
|
|
*/ |
30
|
4 |
|
public function tableize(string $word) : string |
31
|
|
|
{ |
32
|
4 |
|
return mb_strtolower(preg_replace('~(?<=\\w)([A-Z])~u', '_$1', $word)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Converts a word into the format for a Doctrine class name. Converts 'table_name' to 'TableName'. |
37
|
|
|
*/ |
38
|
13 |
|
public function classify(string $word) : string |
39
|
|
|
{ |
40
|
13 |
|
return str_replace([' ', '_', '-'], '', ucwords($word, ' _-')); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Camelizes a word. This uses the classify() method and turns the first character to lowercase. |
45
|
|
|
*/ |
46
|
6 |
|
public function camelize(string $word) : string |
47
|
|
|
{ |
48
|
6 |
|
return lcfirst($this->classify($word)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Uppercases words with configurable delimiters between words. |
53
|
|
|
* |
54
|
|
|
* Takes a string and capitalizes all of the words, like PHP's built-in |
55
|
|
|
* ucwords function. This extends that behavior, however, by allowing the |
56
|
|
|
* word delimiters to be configured, rather than only separating on |
57
|
|
|
* whitespace. |
58
|
|
|
* |
59
|
|
|
* Here is an example: |
60
|
|
|
* <code> |
61
|
|
|
* <?php |
62
|
|
|
* $string = 'top-o-the-morning to all_of_you!'; |
63
|
|
|
* echo $inflector->ucwords($string); |
64
|
|
|
* // Top-O-The-Morning To All_of_you! |
65
|
|
|
* |
66
|
|
|
* echo $inflector->ucwords($string, '-_ '); |
67
|
|
|
* // Top-O-The-Morning To All_Of_You! |
68
|
|
|
* ?> |
69
|
|
|
* </code> |
70
|
|
|
* |
71
|
|
|
* @param string $string The string to operate on. |
72
|
|
|
* @param string $delimiters A list of word separators. |
73
|
|
|
* |
74
|
|
|
* @return string The string with all delimiter-separated words capitalized. |
75
|
|
|
*/ |
76
|
3 |
|
public function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-") : string |
77
|
|
|
{ |
78
|
3 |
|
return ucwords($string, $delimiters); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns a word in plural form. |
83
|
|
|
* |
84
|
|
|
* @param string $word The word in singular form. |
85
|
|
|
* |
86
|
|
|
* @return string The word in plural form. |
87
|
|
|
*/ |
88
|
257 |
|
public function pluralize(string $word) : string |
89
|
|
|
{ |
90
|
257 |
|
return $this->pluralInflector->inflect($word); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns a word in singular form. |
95
|
|
|
* |
96
|
|
|
* @param string $word The word in plural form. |
97
|
|
|
* |
98
|
|
|
* @return string The word in singular form. |
99
|
|
|
*/ |
100
|
258 |
|
public function singularize(string $word) : string |
101
|
|
|
{ |
102
|
258 |
|
return $this->singularInflector->inflect($word); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|