|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Polyfill\Intl\ListFormatter; |
| 13 | + |
| 14 | +/** |
| 15 | + * A polyfill implementation of the IntlListFormatter class provided by the intl extension. |
| 16 | + * |
| 17 | + * @author Ayesh Karunaratne <[email protected]> |
| 18 | + * |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +class IntlListFormatter |
| 22 | +{ |
| 23 | + public const TYPE_AND = 0; |
| 24 | + public const TYPE_OR = 1; |
| 25 | + public const TYPE_UNITS = 3; |
| 26 | + |
| 27 | + public const WIDTH_WIDE = 0; |
| 28 | + public const WIDTH_SHORT = 1; |
| 29 | + public const WIDTH_NARROW = 2; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + private $locale; |
| 35 | + /** |
| 36 | + * @var int |
| 37 | + */ |
| 38 | + private $type; |
| 39 | + /** |
| 40 | + * @var int |
| 41 | + */ |
| 42 | + private $width; |
| 43 | + |
| 44 | + protected static $listPatterns = [ |
| 45 | + 'en' => [ |
| 46 | + 'listPattern-type-standard' => [ |
| 47 | + 'start' => '{0}, {1}', |
| 48 | + 'middle' => '{0}, {1}', |
| 49 | + 'end' => '{0}, and {1}', |
| 50 | + 2 => '{0} and {1}', |
| 51 | + ], |
| 52 | + 'listPattern-type-or' => [ |
| 53 | + 'start' => '{0}, {1}', |
| 54 | + 'middle' => '{0}, {1}', |
| 55 | + 'end' => '{0}, or {1}', |
| 56 | + 2 => '{0} or {1}', |
| 57 | + ], |
| 58 | + 'listPattern-type-or-narrow' => [ |
| 59 | + 'start' => '{0}, {1}', |
| 60 | + 'middle' => '{0}, {1}', |
| 61 | + 'end' => '{0}, or {1}', |
| 62 | + 2 => '{0} or {1}', |
| 63 | + ], |
| 64 | + 'listPattern-type-or-short' => [ |
| 65 | + 'start' => '{0}, {1}', |
| 66 | + 'middle' => '{0}, {1}', |
| 67 | + 'end' => '{0}, or {1}', |
| 68 | + 2 => '{0} or {1}', |
| 69 | + ], |
| 70 | + 'listPattern-type-standard-narrow' => [ |
| 71 | + 'start' => '{0}, {1}', |
| 72 | + 'middle' => '{0}, {1}', |
| 73 | + 'end' => '{0}, {1}', |
| 74 | + 2 => '{0}, {1}', |
| 75 | + ], |
| 76 | + 'listPattern-type-standard-short' => [ |
| 77 | + 'start' => '{0}, {1}', |
| 78 | + 'middle' => '{0}, {1}', |
| 79 | + 'end' => '{0}, & {1}', |
| 80 | + 2 => '{0} & {1}', |
| 81 | + ], |
| 82 | + 'listPattern-type-unit' => [ |
| 83 | + 'start' => '{0}, {1}', |
| 84 | + 'middle' => '{0}, {1}', |
| 85 | + 'end' => '{0}, {1}', |
| 86 | + 2 => '{0}, {1}', |
| 87 | + ], |
| 88 | + 'listPattern-type-unit-narrow' => [ |
| 89 | + 'start' => '{0} {1}', |
| 90 | + 'middle' => '{0} {1}', |
| 91 | + 'end' => '{0} {1}', |
| 92 | + 2 => '{0} {1}', |
| 93 | + ], |
| 94 | + 'listPattern-type-unit-short' => [ |
| 95 | + 'start' => '{0}, {1}', |
| 96 | + 'middle' => '{0}, {1}', |
| 97 | + 'end' => '{0}, {1}', |
| 98 | + 2 => '{0}, {1}', |
| 99 | + ], |
| 100 | + ], |
| 101 | + ]; |
| 102 | + |
| 103 | + public function __construct( |
| 104 | + string $locale, |
| 105 | + int $type = self::TYPE_AND, |
| 106 | + int $width = self::WIDTH_WIDE |
| 107 | + ) { |
| 108 | + $exceptionClass = PHP_VERSION_ID >= 80000 ? \ValueError::class : \InvalidArgumentException::class; |
| 109 | + if ($locale !== 'en' && strpos($locale, 'en') !== 0) { |
| 110 | + throw new $exceptionClass('Invalid locale, only "en" and "en-*" locales are supported'); |
| 111 | + } |
| 112 | + |
| 113 | + if ($type !== self::TYPE_AND && $type !== self::TYPE_OR && $type !== self::TYPE_UNITS) { |
| 114 | + throw new $exceptionClass('Argument #2 ($type) must be one of IntlListFormatter::TYPE_AND, IntlListFormatter::TYPE_OR, or IntlListFormatter::TYPE_UNITS.'); |
| 115 | + } |
| 116 | + |
| 117 | + if ($width !== self::WIDTH_WIDE && $width !== self::WIDTH_SHORT && $width !== self::WIDTH_NARROW) { |
| 118 | + throw new $exceptionClass('Argument #3 ($width) must be one of IntlListFormatter::WIDTH_WIDE, IntlListFormatter::WIDTH_SHORT, or IntlListFormatter::WIDTH_NARROW.'); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + $this->locale = 'en'; |
| 123 | + $this->type = $type; |
| 124 | + $this->width = $width; |
| 125 | + } |
| 126 | + |
| 127 | + public function format(array $strings): string |
| 128 | + { |
| 129 | + $itemCount = count($strings); |
| 130 | + |
| 131 | + if ($itemCount === 0) { |
| 132 | + return ''; |
| 133 | + } |
| 134 | + |
| 135 | + $strings = array_values($strings); |
| 136 | + |
| 137 | + if ($itemCount === 1) { |
| 138 | + return (string) $strings[0]; |
| 139 | + } |
| 140 | + |
| 141 | + $pattern = $this->getListPattern(); |
| 142 | + |
| 143 | + switch ($this->type) { |
| 144 | + case self::TYPE_AND: |
| 145 | + $lookupKeyType = 'standard'; |
| 146 | + break; |
| 147 | + case self::TYPE_OR: |
| 148 | + $lookupKeyType = 'or'; |
| 149 | + break; |
| 150 | + case self::TYPE_UNITS: |
| 151 | + $lookupKeyType = 'unit'; |
| 152 | + break; |
| 153 | + } |
| 154 | + |
| 155 | + switch ($this->width) { |
| 156 | + case self::WIDTH_WIDE: |
| 157 | + $lookupKeyWidth = ''; |
| 158 | + break; |
| 159 | + case self::WIDTH_SHORT: |
| 160 | + $lookupKeyWidth = '-short'; |
| 161 | + break; |
| 162 | + case self::WIDTH_NARROW: |
| 163 | + $lookupKeyWidth = '-narrow'; |
| 164 | + break; |
| 165 | + } |
| 166 | + |
| 167 | + $pattern = $pattern['listPattern-type-' . $lookupKeyType . $lookupKeyWidth]; |
| 168 | + |
| 169 | + if ($itemCount === 2) { |
| 170 | + return strtr($pattern[2], ['{0}' => (string) $strings[0], '{1}' => (string) $strings[1]]); |
| 171 | + } |
| 172 | + |
| 173 | + if ($itemCount === 3) { |
| 174 | + $start = strtr($pattern['start'], ['{0}' => (string) $strings[0], '{1}' => (string) $strings[1]]); |
| 175 | + return strtr($pattern['end'], ['{0}' => $start, '{1}' => (string) $strings[2]]); |
| 176 | + } |
| 177 | + |
| 178 | + $result = strtr($pattern['start'], ['{0}' => (string) $strings[0], '{1}' => (string) $strings[1]]); |
| 179 | + |
| 180 | + for ($i = 2; $i < $itemCount - 1; $i++) { |
| 181 | + $result = strtr($pattern["middle"], [ |
| 182 | + "{0}" => $result, |
| 183 | + "{1}" => $strings[$i], |
| 184 | + ]); |
| 185 | + } |
| 186 | + |
| 187 | + return strtr($pattern["end"], [ |
| 188 | + "{0}" => $result, |
| 189 | + "{1}" => $strings[$itemCount - 1], |
| 190 | + ]); |
| 191 | + } |
| 192 | + |
| 193 | + protected function getListPattern(): array { |
| 194 | + return self::$listPatterns[$this->locale]; |
| 195 | + } |
| 196 | + |
| 197 | + public function getErrorCode() |
| 198 | + { |
| 199 | + return 0; |
| 200 | + } |
| 201 | + |
| 202 | + public function getErrorMessage() |
| 203 | + { |
| 204 | + return ''; |
| 205 | + } |
| 206 | +} |
0 commit comments