Completed
Pull Request — master (#90)
by Jonathan
02:28
created

Inflector::pluralize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
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 RulesetInflector */
16
    private $singularInflector;
17
18
    /** @var RulesetInflector */
19
    private $pluralInflector;
20
21 526
    public function __construct(RulesetInflector $singularInflector, RulesetInflector $pluralInflector)
22
    {
23 526
        $this->singularInflector = $singularInflector;
24 526
        $this->pluralInflector   = $pluralInflector;
25 526
    }
26
27
    /**
28
     * Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'.
29
     */
30 3
    public function tableize(string $word) : string
31
    {
32 3
        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 11
    public function classify(string $word) : string
39
    {
40 11
        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 5
    public function camelize(string $word) : string
47
    {
48 5
        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 2
    public function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-") : string
77
    {
78 2
        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 256
    public function pluralize(string $word) : string
89
    {
90 256
        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 256
    public function singularize(string $word) : string
101
    {
102 256
        return $this->singularInflector->inflect($word);
103
    }
104
105
    /**
106
     * Add custom singular inflection rules.
107
     *
108
     * $inflector->addSingularRules(['/^(inflect)or$/i' => '\1ables']);
109
     *
110
     * @param mixed[] $rules
111
     */
112 3
    public function addSingularRules(array $rules) : void
113
    {
114 3
        $this->singularInflector->addRules($rules);
115 3
    }
116
117
    /**
118
     * Add custom plural inflection rules.
119
     *
120
     * $inflector->addPluralRules([
121
     *     'rules' => ['/^(inflect)ors$/i' => '\1ables'],
122
     *     'uninflected' => ['dontinflectme'],
123
     *     'irregular' => ['red' => 'redlings']
124
     * ]);
125
     *
126
     * @param mixed[] $rules
127
     */
128 3
    public function addPluralRules(array $rules) : void
129
    {
130 3
        $this->pluralInflector->addRules($rules);
131 3
    }
132
}
133