Completed
Pull Request — master (#139)
by Andreas
04:45
created

Inflector::getInstance()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 4
nop 0
crap 3
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\Common\Inflector;
21
22
use Doctrine\Inflector\Inflector as InflectorObject;
23
use Doctrine\Inflector\InflectorFactory;
24
use Doctrine\Inflector\LanguageInflectorFactory;
25
use Doctrine\Inflector\Rules\Pattern;
26
use Doctrine\Inflector\Rules\Patterns;
27
use Doctrine\Inflector\Rules\Ruleset;
28
use Doctrine\Inflector\Rules\Substitution;
29
use Doctrine\Inflector\Rules\Substitutions;
30
use Doctrine\Inflector\Rules\Transformation;
31
use Doctrine\Inflector\Rules\Transformations;
32
use Doctrine\Inflector\Rules\Word;
33
use InvalidArgumentException;
34
use function array_keys;
35
use function array_map;
36
use function array_values;
37
use function sprintf;
38
use function trigger_error;
39
use const E_USER_DEPRECATED;
40
41
/**
42
 * @deprecated
43
 */
44
final class Inflector
45
{
46
    /**
47
     * @var LanguageInflectorFactory|null
48
     */
49
    private static $factory;
50
51
    /** @var InflectorObject|null */
52
    private static $instance;
53
54 520
    private static function getInstance() : InflectorObject
55
    {
56 520
        if (self::$factory === null) {
57 2
            self::$factory = self::createFactory();
58
        }
59
60 520
        if (self::$instance === null) {
61 5
            self::$instance = self::$factory->build();
62
        }
63
64 520
        return self::$instance;
65
    }
66
67 5
    private static function createFactory() : LanguageInflectorFactory
68
    {
69 5
        return InflectorFactory::create();
70
    }
71
72
    /**
73
     * Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'.
74
     *
75
     * @deprecated
76
     */
77 3
    public static function tableize(string $word) : string
78
    {
79 3
        @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED);
80
81 3
        return self::getInstance()->tableize($word);
82
    }
83
84
    /**
85
     * Converts a word into the format for a Doctrine class name. Converts 'table_name' to 'TableName'.
86
     */
87 6
    public static function classify(string $word) : string
88
    {
89 6
        @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED);
90
91 6
        return self::getInstance()->classify($word);
92
    }
93
94
    /**
95
     * Camelizes a word. This uses the classify() method and turns the first character to lowercase.
96
     *
97
     * @deprecated
98
     */
99 5
    public static function camelize(string $word) : string
100
    {
101 5
        @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED);
102
103 5
        return self::getInstance()->camelize($word);
104
    }
105
106
    /**
107
     * Uppercases words with configurable delimeters between words.
108
     *
109
     * Takes a string and capitalizes all of the words, like PHP's built-in
110
     * ucwords function. This extends that behavior, however, by allowing the
111
     * word delimeters to be configured, rather than only separating on
112
     * whitespace.
113
     *
114
     * Here is an example:
115
     * <code>
116
     * <?php
117
     * $string = 'top-o-the-morning to all_of_you!';
118
     * echo \Doctrine\Common\Inflector\Inflector::ucwords($string);
119
     * // Top-O-The-Morning To All_of_you!
120
     *
121
     * echo \Doctrine\Common\Inflector\Inflector::ucwords($string, '-_ ');
122
     * // Top-O-The-Morning To All_Of_You!
123
     * ?>
124
     * </code>
125
     *
126
     * @param string $string The string to operate on.
127
     * @param string $delimiters A list of word separators.
128
     *
129
     * @return string The string with all delimeter-separated words capitalized.
130
     *
131
     * @deprecated
132
     */
133 2
    public static function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-") : string
134
    {
135 2
        @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please use the "ucwords" function instead.', __METHOD__), E_USER_DEPRECATED);
136
137 2
        return ucwords($string, $delimiters);
138
    }
139
140
    /**
141
     * Clears Inflectors inflected value caches, and resets the inflection
142
     * rules to the initial values.
143
     *
144
     * @deprecated
145
     */
146 4
    public static function reset() : void
147
    {
148 4
        @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED);
149
150 4
        self::$factory = null;
151 4
        self::$instance = null;
152 4
    }
153
154
    /**
155
     * Adds custom inflection $rules, of either 'plural' or 'singular' $type.
156
     *
157
     * ### Usage:
158
     *
159
     * {{{
160
     * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
161
     * Inflector::rules('plural', array(
162
     *     'rules' => array('/^(inflect)ors$/i' => '\1ables'),
163
     *     'uninflected' => array('dontinflectme'),
164
     *     'irregular' => array('red' => 'redlings')
165
     * ));
166
     * }}}
167
     *
168
     * @param string  $type         The type of inflection, either 'plural' or 'singular'
169
     * @param array|iterable $rules An array of rules to be added.
170
     * @param boolean $reset        If true, will unset default inflections for all
171
     *                              new rules that are being defined in $rules.
172
     *
173
     * @return void
174
     *
175
     * @deprecated
176
     */
177 4
    public static function rules(string $type, iterable $rules, bool $reset = false) : void
178
    {
179 4
        @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED);
180
181 4
        if (self::$factory === null) {
182 3
            self::$factory = self::createFactory();
183
        }
184
185 4
        self::$instance = null;
186
187 4
        switch ($type) {
188 4
            case 'singular':
189 3
                self::$factory->withSingularRules(self::buildRuleset($rules), $reset);
190 3
                break;
191 3
            case 'plural':
192 3
                self::$factory->withPluralRules(self::buildRuleset($rules), $reset);
193 3
                break;
194
            default:
195
                throw new InvalidArgumentException(sprintf('Cannot define custom inflection rules for type "%s".', $type));
196
        }
197 4
    }
198
199 4
    private static function buildRuleset(iterable $rules) : Ruleset
200
    {
201 4
        $regular = [];
202 4
        $irregular = [];
203 4
        $uninflected = [];
204
205 4
        foreach ($rules as $rule => $pattern) {
206 4
            if ( ! is_array($pattern)) {
207 2
                continue;
208
            }
209
210 4
            switch ($rule) {
211 4
                case 'uninflected':
212 3
                    $uninflected = $pattern;
213 3
                    break;
214 4
                case 'irregular':
215 4
                    $irregular = $pattern;
216 4
                    break;
217 4
                case 'rules':
218 4
                    $regular = $pattern;
219 4
                    break;
220
            }
221
222 4
            unset($rules[$rule]);
223
        }
224
225 4
        $regular = (array) $rules + $regular;
226
227 4
        return new Ruleset(
228 4
            new Transformations(...array_map(
229
                static function (string $pattern, string $replacement) : Transformation {
230 4
                    return new Transformation(new Pattern($pattern), $replacement);
231 4
                },
232 4
                array_keys($regular),
233 4
                array_values($regular)
234
            )),
235 4
            new Patterns(...array_map(
236
                static function (string $pattern) : Pattern {
237 3
                    return new Pattern($pattern);
238 4
                },
239 4
                $uninflected
240
            )),
241 4
            new Substitutions(...array_map(
242
                static function (string $word, string $to) : Substitution {
243 4
                    return new Substitution(new Word($word), new Word($to));
244 4
                },
245 4
                array_keys($irregular),
246 4
                array_values($irregular)
247
            ))
248
        );
249
    }
250
251
    /**
252
     * Returns a word in plural form.
253
     *
254
     * @param string $word The word in singular form.
255
     *
256
     * @return string The word in plural form.
257
     *
258
     * @deprecated
259
     */
260 254
    public static function pluralize(string $word) : string
261
    {
262 254
        @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED);
263
264 254
        return self::getInstance()->pluralize($word);
265
    }
266
267
    /**
268
     * Returns a word in singular form.
269
     *
270
     * @param string $word The word in plural form.
271
     *
272
     * @return string The word in singular form.
273
     *
274
     * @deprecated
275
     */
276 254
    public static function singularize(string $word) : string
277
    {
278 254
        @trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED);
279
280 254
        return self::getInstance()->singularize($word);
281
    }
282
}
283