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