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