1 | <?php |
||
22 | class Inflector |
||
23 | { |
||
24 | /** |
||
25 | * Default inflector locale. |
||
26 | * |
||
27 | * Alias to {@link INFLECTOR_DEFAULT_LOCALE}. |
||
28 | */ |
||
29 | public const DEFAULT_LOCALE = INFLECTOR_DEFAULT_LOCALE; |
||
30 | |||
31 | /** |
||
32 | * {@link camelize()} option to downcase the first letter. |
||
33 | */ |
||
34 | public const DOWNCASE_FIRST_LETTER = true; |
||
35 | |||
36 | /** |
||
37 | * {@link camelize()} option to keep the first letter as is. |
||
38 | */ |
||
39 | public const UPCASE_FIRST_LETTER = false; |
||
40 | |||
41 | /** |
||
42 | * @var array<string, Inflector> |
||
43 | */ |
||
44 | static private $inflectors = []; |
||
45 | |||
46 | /** |
||
47 | * Returns an inflector for the specified locale. |
||
48 | * |
||
49 | * Note: Inflectors are shared for the same locale. If you need to alter an inflector you |
||
50 | * MUST clone it first. |
||
51 | */ |
||
52 | static public function get(string $locale = self::DEFAULT_LOCALE): self |
||
61 | |||
62 | /** |
||
63 | * Inflections used by the inflector. |
||
64 | * |
||
65 | * @var Inflections |
||
66 | */ |
||
67 | protected $inflections; |
||
68 | |||
69 | public function __construct(Inflections $inflections = null) |
||
73 | |||
74 | /** |
||
75 | * Returns the {@link $inflections} property. |
||
76 | * |
||
77 | * @throws PropertyNotDefined in attempt to read an inaccessible property. If the {@link PropertyNotDefined} |
||
78 | * class is not available a {@link \InvalidArgumentException} is thrown instead. |
||
79 | */ |
||
80 | public function __get(string $property) |
||
96 | |||
97 | /** |
||
98 | * Clone inflections. |
||
99 | */ |
||
100 | public function __clone() |
||
104 | |||
105 | /** |
||
106 | * Applies inflection rules for {@link singularize} and {@link pluralize}. |
||
107 | * |
||
108 | * <pre> |
||
109 | * $this->apply_inflections('post', $this->plurals); // "posts" |
||
110 | * $this->apply_inflections('posts', $this->singulars); // "post" |
||
111 | * </pre> |
||
112 | * |
||
113 | * @param array<string, string> $rules |
||
114 | */ |
||
115 | private function apply_inflections(string $word, array $rules): string |
||
141 | |||
142 | /** |
||
143 | * Returns the plural form of the word in the string. |
||
144 | * |
||
145 | * <pre> |
||
146 | * $this->pluralize('post'); // "posts" |
||
147 | * $this->pluralize('children'); // "child" |
||
148 | * $this->pluralize('sheep'); // "sheep" |
||
149 | * $this->pluralize('words'); // "words" |
||
150 | * $this->pluralize('CamelChild'); // "CamelChildren" |
||
151 | * </pre> |
||
152 | */ |
||
153 | public function pluralize(string $word): string |
||
157 | |||
158 | /** |
||
159 | * The reverse of {@link pluralize}, returns the singular form of a word in a string. |
||
160 | * |
||
161 | * <pre> |
||
162 | * $this->singularize('posts'); // "post" |
||
163 | * $this->singularize('children'); // "child" |
||
164 | * $this->singularize('sheep'); // "sheep" |
||
165 | * $this->singularize('word'); // "word" |
||
166 | * $this->singularize('CamelChildren'); // "CamelChild" |
||
167 | * </pre> |
||
168 | */ |
||
169 | public function singularize(string $word): string |
||
173 | |||
174 | /** |
||
175 | * By default, {@link camelize} converts strings to UpperCamelCase. |
||
176 | * |
||
177 | * {@link camelize} will also convert "/" to "\" which is useful for converting paths to |
||
178 | * namespaces. |
||
179 | * |
||
180 | * <pre> |
||
181 | * $this->camelize('active_model'); // 'ActiveModel' |
||
182 | * $this->camelize('active_model', true); // 'activeModel' |
||
183 | * $this->camelize('active_model/errors'); // 'ActiveModel\Errors' |
||
184 | * $this->camelize('active_model/errors', true); // 'activeModel\Errors' |
||
185 | * </pre> |
||
186 | * |
||
187 | * As a rule of thumb you can think of {@link camelize} as the inverse of {@link underscore}, |
||
188 | * though there are cases where that does not hold: |
||
189 | * |
||
190 | * <pre> |
||
191 | * $this->camelize($this->underscore('SSLError')); // "SslError" |
||
192 | * </pre> |
||
193 | * |
||
194 | * @param bool $downcase_first_letter One of {@link UPCASE_FIRST_LETTER}, |
||
195 | * {@link DOWNCASE_FIRST_LETTER}. |
||
196 | */ |
||
197 | public function camelize(string $term, bool $downcase_first_letter = self::UPCASE_FIRST_LETTER): string |
||
233 | |||
234 | /** |
||
235 | * Makes an underscored, lowercase form from the expression in the string. |
||
236 | * |
||
237 | * Changes "\" to "/" to convert namespaces to paths. |
||
238 | * |
||
239 | * <pre> |
||
240 | * $this->underscore('ActiveModel'); // 'active_model' |
||
241 | * $this->underscore('ActiveModel\Errors'); // 'active_model/errors' |
||
242 | * </pre> |
||
243 | * |
||
244 | * As a rule of thumb you can think of {@link underscore} as the inverse of {@link camelize()}, |
||
245 | * though there are cases where that does not hold: |
||
246 | * |
||
247 | * <pre> |
||
248 | * $this->camelize($this->underscore('SSLError')); // "SslError" |
||
249 | * </pre> |
||
250 | */ |
||
251 | public function underscore(string $camel_cased_word): string |
||
270 | |||
271 | /** |
||
272 | * Capitalizes the first word and turns underscores into spaces and strips a trailing "_id", |
||
273 | * if any. Like {@link titleize()}, this is meant for creating pretty output. |
||
274 | * |
||
275 | * <pre> |
||
276 | * $this->humanize('employee_salary'); // "Employee salary" |
||
277 | * $this->humanize('author_id'); // "Author" |
||
278 | * </pre> |
||
279 | */ |
||
280 | public function humanize(string $lower_case_and_underscored_word): string |
||
311 | |||
312 | /** |
||
313 | * Capitalizes all the words and replaces some characters in the string to create a nicer |
||
314 | * looking title. {@link titleize()} is meant for creating pretty output. It is not used in |
||
315 | * the Rails internals. |
||
316 | * |
||
317 | * <pre> |
||
318 | * $this->titleize('man from the boondocks'); // "Man From The Boondocks" |
||
319 | * $this->titleize('x-men: the last stand'); // "X Men: The Last Stand" |
||
320 | * $this->titleize('TheManWithoutAPast'); // "The Man Without A Past" |
||
321 | * $this->titleize('raiders_of_the_lost_ark'); // "Raiders Of The Lost Ark" |
||
322 | * </pre> |
||
323 | */ |
||
324 | public function titleize(string $str): string |
||
337 | |||
338 | /** |
||
339 | * Replaces underscores with dashes in the string. |
||
340 | * |
||
341 | * <pre> |
||
342 | * $this->dasherize('puni_puni'); // "puni-puni" |
||
343 | * </pre> |
||
344 | */ |
||
345 | public function dasherize(string $underscored_word): string |
||
349 | |||
350 | /** |
||
351 | * Makes an hyphenated, lowercase form from the expression in the string. |
||
352 | * |
||
353 | * This is a combination of {@link underscore} and {@link dasherize}. |
||
354 | */ |
||
355 | public function hyphenate(string $str): string |
||
359 | |||
360 | /** |
||
361 | * Returns the suffix that should be added to a number to denote the position in an ordered |
||
362 | * sequence such as 1st, 2nd, 3rd, 4th. |
||
363 | * |
||
364 | * <pre> |
||
365 | * $this->ordinal(1); // "st" |
||
366 | * $this->ordinal(2); // "nd" |
||
367 | * $this->ordinal(1002); // "nd" |
||
368 | * $this->ordinal(1003); // "rd" |
||
369 | * $this->ordinal(-11); // "th" |
||
370 | * $this->ordinal(-1021); // "st" |
||
371 | * </pre> |
||
372 | */ |
||
373 | public function ordinal(int $number): string |
||
390 | |||
391 | /** |
||
392 | * Turns a number into an ordinal string used to denote the position in an ordered sequence |
||
393 | * such as 1st, 2nd, 3rd, 4th. |
||
394 | * |
||
395 | * <pre> |
||
396 | * $this->ordinalize(1); // "1st" |
||
397 | * $this->ordinalize(2); // "2nd" |
||
398 | * $this->ordinalize(1002); // "1002nd" |
||
399 | * $this->ordinalize(1003); // "1003rd" |
||
400 | * $this->ordinalize(-11); // "-11th" |
||
401 | * $this->ordinalize(-1021); // "-1021st" |
||
402 | * </pre> |
||
403 | */ |
||
404 | public function ordinalize(int $number): string |
||
408 | |||
409 | /** |
||
410 | * Returns true if the word is uncountable, false otherwise. |
||
411 | * |
||
412 | * <pre> |
||
413 | * $this->is_uncountable('advice'); // true |
||
414 | * $this->is_uncountable('weather'); // true |
||
415 | * $this->is_uncountable('cat'); // false |
||
416 | * </pre> |
||
417 | */ |
||
418 | public function is_uncountable(string $word): bool |
||
426 | } |
||
427 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: