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

Inflector   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 131
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A tableize() 0 3 1
A camelize() 0 3 1
A pluralize() 0 3 1
A rules() 0 3 1
A reset() 0 3 1
A setInflect() 0 3 1
A singularize() 0 3 1
A ucwords() 0 3 1
A initialize() 0 9 1
A classify() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Inflector;
6
7
use Doctrine\Inflector\Rules\Uninflected;
8
9
/**
10
 * Doctrine inflector has static methods for inflecting text.
11
 *
12
 * The methods in these classes are from several different sources collected
13
 * across several different php projects and several different authors. The
14
 * original author names and emails are not known.
15
 *
16
 * Pluralize & Singularize implementation are borrowed from CakePHP with some modifications.
17
 *
18
 * @deprecated
19
 */
20
class Inflector
21
{
22
    /** @var Inflect */
23
    private static $inflect;
24
25
    public static function setInflect(Inflect $inflect) : void
26
    {
27
        self::$inflect = $inflect;
28
    }
29
30
    public static function initialize() : void
31
    {
32
        $uninflected  = new Uninflected();
33
        $pluralizer   = new Pluralizer($uninflected);
34
        $singularizer = new Singularizer($uninflected, $pluralizer);
35
36
        $inflect = new Inflect($singularizer, $pluralizer);
37
38
        self::setInflect($inflect);
39
    }
40
41
    /**
42
     * Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'.
43
     */
44
    public static function tableize(string $word) : string
45
    {
46
        return self::$inflect->tableize($word);
47
    }
48
49
    /**
50
     * Converts a word into the format for a Doctrine class name. Converts 'table_name' to 'TableName'.
51
     */
52
    public static function classify(string $word) : string
53
    {
54
        return self::$inflect->classify($word);
55
    }
56
57
    /**
58
     * Camelizes a word. This uses the classify() method and turns the first character to lowercase.
59
     */
60
    public static function camelize(string $word) : string
61
    {
62
        return self::$inflect->camelize($word);
63
    }
64
65
    /**
66
     * Uppercases words with configurable delimiters between words.
67
     *
68
     * Takes a string and capitalizes all of the words, like PHP's built-in
69
     * ucwords function. This extends that behavior, however, by allowing the
70
     * word delimiters to be configured, rather than only separating on
71
     * whitespace.
72
     *
73
     * Here is an example:
74
     * <code>
75
     * <?php
76
     * $string = 'top-o-the-morning to all_of_you!';
77
     * echo \Doctrine\Common\Inflector\Inflector::ucwords($string);
78
     * // Top-O-The-Morning To All_of_you!
79
     *
80
     * echo \Doctrine\Common\Inflector\Inflector::ucwords($string, '-_ ');
81
     * // Top-O-The-Morning To All_Of_You!
82
     * ?>
83
     * </code>
84
     *
85
     * @param string $string     The string to operate on.
86
     * @param string $delimiters A list of word separators.
87
     *
88
     * @return string The string with all delimiter-separated words capitalized.
89
     */
90
    public static function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-") : string
91
    {
92
        return self::$inflect->ucwords($string, $delimiters);
93
    }
94
95
    /**
96
     * Clears Inflectors inflected value caches, and resets the inflection
97
     * rules to the initial values.
98
     */
99
    public static function reset() : void
100
    {
101
        self::$inflect->reset();
102
    }
103
104
    /**
105
     * Adds custom inflection $rules, of either 'plural' or 'singular' $type.
106
     *
107
     * ### Usage:
108
     *
109
     * {{{
110
     * Inflector::rules('plural', ['/^(inflect)or$/i' => '\1ables']);
111
     * Inflector::rules('plural', [
112
     *     'rules' => ['/^(inflect)ors$/i' => '\1ables'],
113
     *     'uninflected' => ['dontinflectme'],
114
     *     'irregular' => ['red' => 'redlings']
115
     * ]);
116
     * }}}
117
     *
118
     * @param string        $type  The type of inflection, either 'plural' or 'singular'
119
     * @param array|mixed[] $rules An array of rules to be added.
120
     *                             new rules that are being defined in $rules.
121
     * @param bool          $reset If true, will unset default inflections for all
122
     *                             new rules that are being defined in $rules.
123
     */
124
    public static function rules(string $type, array $rules, bool $reset = false) : void
125
    {
126
        self::$inflect->addRules($type, $rules, $reset);
127
    }
128
129
    /**
130
     * Returns a word in plural form.
131
     *
132
     * @param string $word The word in singular form.
133
     *
134
     * @return string The word in plural form.
135
     */
136
    public static function pluralize(string $word) : string
137
    {
138
        return self::$inflect->pluralize($word);
139
    }
140
141
    /**
142
     * Returns a word in singular form.
143
     *
144
     * @param string $word The word in plural form.
145
     *
146
     * @return string The word in singular form.
147
     */
148
    public static function singularize(string $word) : string
149
    {
150
        return self::$inflect->singularize($word);
151
    }
152
}
153
154
Inflector::initialize();
155