zend-blog-3-backend
130 строк · 3.6 Кб
1<?php
2/**
3* Created by PhpStorm.
4* User: morontt
5* Date: 28.02.16
6* Time: 18:42
7*/
8
9namespace App\Utils;
10
11/**
12* PHP implementation Ember.Inflector.pluralize
13*
14* @see https://github.com/stefanpenner/ember-inflector/blob/1.9.4/addon/lib/system/inflections.js
15* @see https://github.com/stefanpenner/ember-inflector/blob/1.9.4/addon/lib/system/inflector.js
16*
17* Class Inflector
18*/
19class Inflector
20{
21private const BLANK_REGEX = '/^\s*$/';
22private const LAST_WORD_DASHED_REGEX = '/([\w\/-]+[_\/\s-])([a-z\d]+$)/';
23private const LAST_WORD_CAMELIZED_REGEX = '/([\w\/\s-]+)([A-Z][a-z\d]*$)/';
24private const CAMELIZED_REGEX = '/[A-Z][a-z\d]*$/';
25
26protected static array $plurals = [
27['/$/', 's'],
28['/s$/i', 's'],
29['/^(ax|test)is$/i', '$1es'],
30['/(octop|vir)us$/i', '$1i'],
31['/(octop|vir)i$/i', '$1i'],
32['/(alias|status)$/i', '$1es'],
33['/(bu)s$/i', '$1ses'],
34['/(buffal|tomat)o$/i', '$1oes'],
35['/([ti])um$/i', '$1a'],
36['/([ti])a$/i', '$1a'],
37['/sis$/i', 'ses'],
38['/(?:([^f])fe|([lr])f)$/i', '$1$2ves'],
39['/(hive)$/i', '$1s'],
40['/([^aeiouy]|qu)y$/i', '$1ies'],
41['/(x|ch|ss|sh)$/i', '$1es'],
42['/(matr|vert|ind)(?:ix|ex)$/i', '$1ices'],
43['/^(m|l)ouse$/i', '$1ice'],
44['/^(m|l)ice$/i', '$1ice'],
45['/^(ox)$/i', '$1en'],
46['/^(oxen)$/i', '$1'],
47['/(quiz)$/i', '$1zes'],
48];
49
50protected static array $irregularPairs = [
51['person', 'people'],
52['man', 'men'],
53['child', 'children'],
54['sex', 'sexes'],
55['move', 'moves'],
56['cow', 'kine'],
57['zombie', 'zombies'],
58];
59
60protected static array $uncountable = [
61'equipment',
62'information',
63'rice',
64'money',
65'species',
66'series',
67'fish',
68'sheep',
69'jeans',
70'police',
71];
72
73/**
74* @param string $word
75*
76* @return string
77*/
78public static function pluralize(string $word): string
79{
80if (!$word || preg_match(self::BLANK_REGEX, $word)) {
81return $word;
82}
83
84$isCamelized = preg_match(self::CAMELIZED_REGEX, $word);
85$firstPhrase = '';
86$lastWord = $word;
87
88$lowercase = strtolower($word);
89$wordSplit = [];
90if (preg_match(self::LAST_WORD_DASHED_REGEX, $word, $wordSplit)) {
91$firstPhrase = $wordSplit[1];
92$lastWord = strtolower($wordSplit[2]);
93} else {
94$wordSplit = [];
95if (preg_match(self::LAST_WORD_CAMELIZED_REGEX, $word, $wordSplit)) {
96$firstPhrase = $wordSplit[1];
97$lastWord = strtolower($wordSplit[2]);
98}
99}
100
101if (in_array($lowercase, self::$uncountable) || in_array($lastWord, self::$uncountable)) {
102return $word;
103}
104
105foreach (self::$irregularPairs as $rule) {
106if ($rule[0] === $lastWord) {
107$substitution = $rule[1];
108if ($isCamelized) {
109$substitution = ucfirst($substitution);
110}
111
112return $firstPhrase . $substitution;
113}
114}
115
116for ($i = count(self::$plurals); $i > 0; $i--) {
117$inflection = self::$plurals[$i - 1];
118if (preg_match($inflection[0], $lastWord)) {
119$res = preg_replace($inflection[0], $inflection[1], $lastWord);
120if ($isCamelized) {
121$res = ucfirst($res);
122}
123
124return $firstPhrase . $res;
125}
126}
127
128return 'n/a';
129}
130}
131