Failed Conditions
Pull Request — master (#90)
by Jonathan
02:40
created

Inflector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Inflector;
6
7
use Doctrine\Inflector\Rules\Ruleset;
8
use function lcfirst;
9
use function mb_strtolower;
10
use function preg_replace;
11
use function str_replace;
12
use function ucwords;
13
14
class Inflector
15
{
16
    /** @var Ruleset */
17
    private $singularRuleset;
18
19
    /** @var Ruleset */
20
    private $pluralRuleset;
21
22 526
    public function __construct(Ruleset $singularRuleset, Ruleset $pluralRuleset)
23
    {
24 526
        $this->singularRuleset = $singularRuleset;
25 526
        $this->pluralRuleset   = $pluralRuleset;
26 526
    }
27
28
    /**
29
     * Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'.
30
     */
31 3
    public function tableize(string $word) : string
32
    {
33 3
        return mb_strtolower(preg_replace('~(?<=\\w)([A-Z])~u', '_$1', $word));
34
    }
35
36
    /**
37
     * Converts a word into the format for a Doctrine class name. Converts 'table_name' to 'TableName'.
38
     */
39 11
    public function classify(string $word) : string
40
    {
41 11
        return str_replace([' ', '_', '-'], '', ucwords($word, ' _-'));
42
    }
43
44
    /**
45
     * Camelizes a word. This uses the classify() method and turns the first character to lowercase.
46
     */
47 5
    public function camelize(string $word) : string
48
    {
49 5
        return lcfirst($this->classify($word));
50
    }
51
52
    /**
53
     * Uppercases words with configurable delimiters between words.
54
     *
55
     * Takes a string and capitalizes all of the words, like PHP's built-in
56
     * ucwords function. This extends that behavior, however, by allowing the
57
     * word delimiters to be configured, rather than only separating on
58
     * whitespace.
59
     *
60
     * Here is an example:
61
     * <code>
62
     * <?php
63
     * $string = 'top-o-the-morning to all_of_you!';
64
     * echo $inflector->ucwords($string);
65
     * // Top-O-The-Morning To All_of_you!
66
     *
67
     * echo $inflector->ucwords($string, '-_ ');
68
     * // Top-O-The-Morning To All_Of_You!
69
     * ?>
70
     * </code>
71
     *
72
     * @param string $string     The string to operate on.
73
     * @param string $delimiters A list of word separators.
74
     *
75
     * @return string The string with all delimiter-separated words capitalized.
76
     */
77 2
    public function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-") : string
78
    {
79 2
        return ucwords($string, $delimiters);
80
    }
81
82
    /**
83
     * Returns a word in plural form.
84
     *
85
     * @param string $word The word in singular form.
86
     *
87
     * @return string The word in plural form.
88
     */
89 256
    public function pluralize(string $word) : string
90
    {
91 256
        return $this->pluralRuleset->inflect($word);
92
    }
93
94
    /**
95
     * Returns a word in singular form.
96
     *
97
     * @param string $word The word in plural form.
98
     *
99
     * @return string The word in singular form.
100
     */
101 256
    public function singularize(string $word) : string
102
    {
103 256
        return $this->singularRuleset->inflect($word);
104
    }
105
106
    /**
107
     * Adds custom singular inflection rules.
108
     *
109
     * $inflector->addSingularRules(['/^(inflect)or$/i' => '\1ables']);
110
     *
111
     * @param mixed[] $rules An array of rules to be added.
112
     *                                new rules that are being defined in $rules.
113
     * @param bool    $reset If true, will unset default inflections for all
114
     *                       new rules that are being defined in $rules.
115
     */
116 3
    public function addSingularRules(array $rules, bool $reset = false) : void
117
    {
118 3
        $this->singularRuleset->addRules($rules, $reset);
119 3
    }
120
121
    /**
122
     * Adds custom plural inflection rules.
123
     *
124
     * $inflector->addPluralRules([
125
     *     'rules' => ['/^(inflect)ors$/i' => '\1ables'],
126
     *     'uninflected' => ['dontinflectme'],
127
     *     'irregular' => ['red' => 'redlings']
128
     * ]);
129
     *
130
     * @param mixed[] $rules An array of rules to be added.
131
     *                                new rules that are being defined in $rules.
132
     * @param bool    $reset If true, will unset default inflections for all
133
     *                       new rules that are being defined in $rules.
134
     */
135 3
    public function addPluralRules(array $rules, bool $reset = false) : void
136
    {
137 3
        $this->pluralRuleset->addRules($rules, $reset);
138 3
    }
139
}
140