| Conditions | 6 |
| Paths | 6 |
| Total Lines | 39 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 25 |
| CRAP Score | 6 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | 8 | public function format(string $message, array $parameters, string $locale): string |
|
| 10 | { |
||
| 11 | 8 | preg_match_all('/{((?>[^{}]+)|(?R))*}/', $message, $matches); |
|
| 12 | 8 | $replacements = []; |
|
| 13 | |||
| 14 | 8 | foreach ($matches[0] as $match) { |
|
| 15 | 8 | $parts = explode(',', $match); |
|
| 16 | 8 | $parameter = trim($parts[0], '{}'); |
|
| 17 | 8 | $value = $parameters[$parameter]; |
|
| 18 | |||
| 19 | 8 | if (!is_scalar($value)) { |
|
| 20 | 1 | continue; |
|
| 21 | } |
||
| 22 | |||
| 23 | 7 | if (count($parts) === 1) { |
|
| 24 | 3 | $replacements[$match] = $value; |
|
| 25 | |||
| 26 | 3 | continue; |
|
| 27 | } |
||
| 28 | |||
| 29 | 5 | $format = ltrim($parts[1]); |
|
| 30 | 5 | $format = rtrim($format, '}'); |
|
| 31 | |||
| 32 | 5 | switch ($format) { |
|
| 33 | 5 | case 'plural': |
|
| 34 | 4 | $options = $parts[2]; |
|
| 35 | 4 | preg_match_all('/([^{}\s]+)({(.*?)})/', $options, $pluralMatches); |
|
| 36 | 4 | $map = array_combine($pluralMatches[1], $pluralMatches[3]); |
|
| 37 | 4 | $formattedValue = $value . ' '; |
|
| 38 | 4 | $formattedValue .= $value === 1 ? $map['one'] : $map['other']; |
|
| 39 | 4 | $replacements[$match] = $formattedValue; |
|
| 40 | |||
| 41 | 4 | break; |
|
| 42 | default: |
||
| 43 | 2 | $replacements[$match] = $value; |
|
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | 8 | return strtr($message, $replacements); |
|
| 48 | } |
||
| 50 |