1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Utility; |
4: | |
5: | use Salient\Contract\Core\Arrayable; |
6: | use Closure; |
7: | use Countable; |
8: | use InvalidArgumentException; |
9: | use Traversable; |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | final class Inflect extends AbstractUtility |
17: | { |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | public static function formatRange($from, $to, string $format, ...$values): string |
45: | { |
46: | if (is_float($from) xor is_float($to)) { |
47: | throw new InvalidArgumentException('$from and $to must be of the same type'); |
48: | } |
49: | |
50: | $singular = $from === $to; |
51: | $zero = $singular && $from === 0; |
52: | $one = $singular && $from === 1; |
53: | $count = $singular ? ($zero ? 0 : 1) : 2; |
54: | |
55: | $callback = $singular |
56: | ? fn(): string => |
57: | (string) $from |
58: | : fn(?string $pluralWord): string => |
59: | sprintf('%s %s %s', $from, $pluralWord ?? 'to', $to); |
60: | |
61: | if ($zero) { |
62: | $no = 'no'; |
63: | } |
64: | |
65: | $replace = [ |
66: | '#' => $callback, |
67: | '' => $callback, |
68: | ]; |
69: | |
70: | if ($zero || $one) { |
71: | $replace += [ |
72: | 'no' => $no ?? $callback, |
73: | 'a' => $no ?? 'a', |
74: | 'an' => $no ?? 'an', |
75: | ]; |
76: | } else { |
77: | $replace += [ |
78: | 'no' => $callback, |
79: | 'a' => $callback, |
80: | 'an' => $callback, |
81: | ]; |
82: | } |
83: | |
84: | return self::doFormat($count, $format, $replace, false, ...$values); |
85: | } |
86: | |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | public static function format($count, string $format, ...$values): string |
124: | { |
125: | return self::doFormat(Get::count($count), $format, [], false, ...$values); |
126: | } |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | public static function formatWithZeroAsOne($count, string $format, ...$values): string |
137: | { |
138: | return self::doFormat(Get::count($count), $format, [], true, ...$values); |
139: | } |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | private static function doFormat(int $count, string $format, array $replace, bool $zeroIsSingular, ...$values): string |
146: | { |
147: | $zero = $count === 0; |
148: | $singular = $count === 1 || ($zero && $zeroIsSingular); |
149: | |
150: | if ($zero) { |
151: | $no = 'no'; |
152: | } |
153: | |
154: | $replace = array_replace($singular |
155: | ? [ |
156: | '#' => (string) $count, |
157: | '' => $no ?? (string) $count, |
158: | 'no' => $no ?? (string) $count, |
159: | 'a' => $no ?? 'a', |
160: | 'an' => $no ?? 'an', |
161: | 'are' => 'is', |
162: | 'is' => 'is', |
163: | 'has' => 'has', |
164: | 'have' => 'has', |
165: | 'was' => 'was', |
166: | 'were' => 'was', |
167: | ] |
168: | : [ |
169: | '#' => (string) $count, |
170: | '' => (string) $count, |
171: | 'no' => $no ?? (string) $count, |
172: | 'a' => $no ?? (string) $count, |
173: | 'an' => $no ?? (string) $count, |
174: | 'are' => 'are', |
175: | 'is' => 'are', |
176: | 'has' => 'have', |
177: | 'have' => 'have', |
178: | 'was' => 'were', |
179: | 'were' => 'were', |
180: | ], $replace); |
181: | |
182: | $format = Regex::replaceCallback( |
183: | '/\{\{#(?::(?<word>[-a-z0-9_\h]*+|#)(?::(?<plural_word>[-a-z0-9_\h]*+))?)?\}\}/i', |
184: | function ($matches) use ($singular, $replace): string { |
185: | $word = $matches['word']; |
186: | $plural = $matches['plural_word']; |
187: | if ($word === '') { |
188: | return $singular ? '' : (string) $plural; |
189: | } |
190: | $word ??= ''; |
191: | $word = Get::value($replace[Str::lower($word)] |
192: | ?? ($singular |
193: | ? $word |
194: | : ($plural ?? self::plural($word))), $plural); |
195: | return $word === $matches['word'] || $matches['word'] === null |
196: | ? $word |
197: | : Str::matchCase($word, $matches['word']); |
198: | }, |
199: | $format, |
200: | -1, |
201: | $count, |
202: | \PREG_UNMATCHED_AS_NULL, |
203: | ); |
204: | |
205: | if ($values) { |
206: | return sprintf($format, ...$values); |
207: | } |
208: | |
209: | return $format; |
210: | } |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | public static function plural(string $word): string |
218: | { |
219: | foreach ([ |
220: | '/(sh?|ch|x|z| (?<! \A phot | \A pian | \A hal ) o) \Z/ix' => ['es', 0], |
221: | '/[^aeiou] y \Z/ix' => ['ies', -1], |
222: | '/is \Z/ix' => ['es', -2], |
223: | '/on \Z/ix' => ['a', -2], |
224: | ] as $pattern => [$replace, $offset]) { |
225: | if (Regex::match($pattern, $word)) { |
226: | if ($offset) { |
227: | return substr_replace($word, $replace, $offset); |
228: | } |
229: | return $word . $replace; |
230: | } |
231: | } |
232: | |
233: | return $word . 's'; |
234: | } |
235: | |
236: | |
237: | |
238: | |
239: | |
240: | |
241: | |
242: | |
243: | public static function indefinite(string $word): string |
244: | { |
245: | $ordinalAn = '/\A [aefhilmnorsx] -?th \Z/ix'; |
246: | $ordinalA = '/\A [bcdgjkpqtuvwyz] -?th \Z/ix'; |
247: | $explicitAn = '/\A (?: euler | hour(?!i) | heir | honest | hono )/ix'; |
248: | $singleAn = '/\A [aefhilmnorsx] \Z/ix'; |
249: | $singleA = '/\A [bcdgjkpqtuvwyz] \Z/ix'; |
250: | |
251: | |
252: | |
253: | |
254: | $abbrevAn = <<<'REGEX' |
255: | / \A (?! |
256: | FJO | [HLMNS]Y. | RY[EO] | SQU | |
257: | ( F[LR]? | [HL] | MN? | N | RH? | S[CHKLMNPTVW]? | X(YL)? ) [AEIOU] |
258: | ) |
259: | [FHLMNRSX][A-Z] /xms |
260: | REGEX; |
261: | |
262: | |
263: | $initialYAn = '/\A y (?: b[lor] | cl[ea] | fere | gg | p[ios] | rou | tt)/xi'; |
264: | |
265: | |
266: | if (Regex::match($ordinalA, $word)) { return 'a'; } |
267: | if (Regex::match($ordinalAn, $word)) { return 'an'; } |
268: | |
269: | |
270: | if (Regex::match($explicitAn, $word)) { return 'an'; } |
271: | if (Regex::match($singleAn, $word)) { return 'an'; } |
272: | if (Regex::match($singleA, $word)) { return 'a'; } |
273: | |
274: | |
275: | if (Regex::match($abbrevAn, $word)) { return 'an'; } |
276: | if (Regex::match('/\A [aefhilmnorsx][.-]/xi', $word)) { return 'an'; } |
277: | if (Regex::match('/\A [a-z][.-]/xi', $word)) { return 'a'; } |
278: | |
279: | |
280: | if (Regex::match('/\A [^aeiouy] /xi', $word)) { return 'a'; } |
281: | |
282: | |
283: | if (Regex::match('/\A e [uw] /xi', $word)) { return 'a'; } |
284: | if (Regex::match('/\A onc?e \b /xi', $word)) { return 'a'; } |
285: | if (Regex::match('/\A uni (?: [^nmd] | mo) /xi', $word)) { return 'a'; } |
286: | if (Regex::match('/\A ut[th] /xi', $word)) { return 'an'; } |
287: | if (Regex::match('/\A u [bcfhjkqrst] [aeiou] /xi', $word)) { return 'a'; } |
288: | |
289: | |
290: | if (Regex::match('/\A U [NK] [AIEO]? /x', $word)) { return 'a'; } |
291: | |
292: | |
293: | if (Regex::match('/\A [aeiou]/xi', $word)) { return 'an'; } |
294: | |
295: | |
296: | if (Regex::match($initialYAn, $word)) { return 'an'; } |
297: | |
298: | |
299: | return 'a'; |
300: | } |
301: | } |
302: | |