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

Inflector::addSingularRules()   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
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
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
     * @param mixed[] $rules
29
     */
30 3
    public function addSingularRules(array $rules, bool $reset = false) : void
31
    {
32 3
        $this->singularizer->addRules($rules, $reset);
33 3
    }
34
35
    /**
36
     * @param mixed[] $rules
37
     */
38 3
    public function addPluralRules(array $rules, bool $reset = false) : void
39
    {
40 3
        $this->pluralizer->addRules($rules, $reset);
41 3
    }
42
43
    /**
44
     * Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'.
45
     */
46 3
    public function tableize(string $word) : string
47
    {
48 3
        return mb_strtolower(preg_replace('~(?<=\\w)([A-Z])~u', '_$1', $word));
49
    }
50
51
    /**
52
     * Converts a word into the format for a Doctrine class name. Converts 'table_name' to 'TableName'.
53
     */
54 11
    public function classify(string $word) : string
55
    {
56 11
        return str_replace([' ', '_', '-'], '', ucwords($word, ' _-'));
57
    }
58
59
    /**
60
     * Camelizes a word. This uses the classify() method and turns the first character to lowercase.
61
     */
62 5
    public function camelize(string $word) : string
63
    {
64 5
        return lcfirst($this->classify($word));
65
    }
66
67
    /**
68
     * Uppercases words with configurable delimiters between words.
69
     *
70
     * Takes a string and capitalizes all of the words, like PHP's built-in
71
     * ucwords function. This extends that behavior, however, by allowing the
72
     * word delimiters to be configured, rather than only separating on
73
     * whitespace.
74
     *
75
     * Here is an example:
76
     * <code>
77
     * <?php
78
     * $string = 'top-o-the-morning to all_of_you!';
79
     * echo $inflect->ucwords($string);
80
     * // Top-O-The-Morning To All_of_you!
81
     *
82
     * echo $inflect->ucwords($string, '-_ ');
83
     * // Top-O-The-Morning To All_Of_You!
84
     * ?>
85
     * </code>
86
     *
87
     * @param string $string     The string to operate on.
88
     * @param string $delimiters A list of word separators.
89
     *
90
     * @return string The string with all delimiter-separated words capitalized.
91
     */
92 2
    public function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-") : string
93
    {
94 2
        return ucwords($string, $delimiters);
95
    }
96
97
    /**
98
     * Returns a word in plural form.
99
     *
100
     * @param string $word The word in singular form.
101
     *
102
     * @return string The word in plural form.
103
     */
104 256
    public function pluralize(string $word) : string
105
    {
106 256
        return $this->pluralizer->inflect($word);
107
    }
108
109
    /**
110
     * Returns a word in singular form.
111
     *
112
     * @param string $word The word in plural form.
113
     *
114
     * @return string The word in singular form.
115
     */
116 256
    public function singularize(string $word) : string
117
    {
118 256
        return $this->singularizer->inflect($word);
119
    }
120
}
121