@@ -68,7 +68,7 @@ |
||
| 68 | 68 | $allowMissing = ($options['allowMissing'] ?? false) === true |
| 69 | 69 | ? static function ($key, $options, $locale, $tokenRegex, Polyglot $polyglot) { |
| 70 | 70 | return $polyglot->transformPhrase($key, $options, $locale, $tokenRegex); |
| 71 | - } |
|
| 71 | + } |
|
| 72 | 72 | : null; |
| 73 | 73 | $this->onMissingKey = is_callable($options['onMissingKey'] ?? false) ? $options['onMissingKey'] : $allowMissing; |
| 74 | 74 | } |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | /** |
| 20 | 20 | * @var array |
| 21 | 21 | */ |
| 22 | - private $phrases = []; |
|
| 22 | + private $phrases=[]; |
|
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * @var string |
@@ -56,21 +56,21 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @param array $options |
| 58 | 58 | */ |
| 59 | - public function __construct(array $options = []) |
|
| 59 | + public function __construct(array $options=[]) |
|
| 60 | 60 | { |
| 61 | 61 | $this->extend($options['phrases'] ?? []); |
| 62 | 62 | $this->locale($options['locale'] ?? 'en'); |
| 63 | 63 | |
| 64 | - $this->delimiter = $options['delimiter'] ?? '||||'; |
|
| 65 | - $this->tokenRegex = $this->constructTokenRegex($options['interpolation'] ?? []); |
|
| 66 | - $this->warn = $options['warn'] ?? static function() {}; |
|
| 64 | + $this->delimiter=$options['delimiter'] ?? '||||'; |
|
| 65 | + $this->tokenRegex=$this->constructTokenRegex($options['interpolation'] ?? []); |
|
| 66 | + $this->warn=$options['warn'] ?? static function() {}; |
|
| 67 | 67 | |
| 68 | - $allowMissing = ($options['allowMissing'] ?? false) === true |
|
| 69 | - ? static function ($key, $options, $locale, $tokenRegex, Polyglot $polyglot) { |
|
| 68 | + $allowMissing=($options['allowMissing'] ?? false) === true |
|
| 69 | + ? static function($key, $options, $locale, $tokenRegex, Polyglot $polyglot) { |
|
| 70 | 70 | return $polyglot->transformPhrase($key, $options, $locale, $tokenRegex); |
| 71 | 71 | } |
| 72 | 72 | : null; |
| 73 | - $this->onMissingKey = is_callable($options['onMissingKey'] ?? false) ? $options['onMissingKey'] : $allowMissing; |
|
| 73 | + $this->onMissingKey=is_callable($options['onMissingKey'] ?? false) ? $options['onMissingKey'] : $allowMissing; |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | /** |
@@ -127,15 +127,15 @@ discard block |
||
| 127 | 127 | * @param array $morePhrases |
| 128 | 128 | * @param string|null $prefix |
| 129 | 129 | */ |
| 130 | - public function extend(array $morePhrases = [], ?string $prefix = null): void |
|
| 130 | + public function extend(array $morePhrases=[], ?string $prefix=null): void |
|
| 131 | 131 | { |
| 132 | 132 | foreach ($morePhrases as $key => $phrase) { |
| 133 | - $prefixedKey = $prefix !== null ? $prefix . '.' . $key : $key; |
|
| 133 | + $prefixedKey=$prefix !== null ? $prefix.'.'.$key : $key; |
|
| 134 | 134 | |
| 135 | 135 | if (is_array($phrase)) { |
| 136 | 136 | $this->extend($phrase, $prefixedKey); |
| 137 | 137 | } else { |
| 138 | - $this->phrases[$prefixedKey] = $phrase; |
|
| 138 | + $this->phrases[$prefixedKey]=$phrase; |
|
| 139 | 139 | } |
| 140 | 140 | } |
| 141 | 141 | } |
@@ -157,13 +157,13 @@ discard block |
||
| 157 | 157 | * @param string|array $morePhrases |
| 158 | 158 | * @param string|null $prefix |
| 159 | 159 | */ |
| 160 | - public function unset($morePhrases, ?string $prefix = null): void |
|
| 160 | + public function unset($morePhrases, ?string $prefix=null) : void |
|
| 161 | 161 | { |
| 162 | 162 | if (is_string($morePhrases)) { |
| 163 | 163 | unset($this->phrases[$morePhrases]); |
| 164 | 164 | } else { |
| 165 | 165 | foreach ($morePhrases as $key => $phrase) { |
| 166 | - $prefixedKey = $prefix !== null ? $prefix . '.' . $key : $key; |
|
| 166 | + $prefixedKey=$prefix !== null ? $prefix.'.'.$key : $key; |
|
| 167 | 167 | |
| 168 | 168 | if (is_array($phrase)) { |
| 169 | 169 | $this->unset($phrase, $prefixedKey); |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | */ |
| 185 | 185 | public function clear(): void |
| 186 | 186 | { |
| 187 | - $this->phrases = []; |
|
| 187 | + $this->phrases=[]; |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | /** |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | * @param array $newPhrases |
| 198 | 198 | * @param string|null $prefix |
| 199 | 199 | */ |
| 200 | - public function replace(array $newPhrases, ?string $prefix = null): void |
|
| 200 | + public function replace(array $newPhrases, ?string $prefix=null): void |
|
| 201 | 201 | { |
| 202 | 202 | $this->clear(); |
| 203 | 203 | $this->extend($newPhrases, $prefix); |
@@ -212,11 +212,11 @@ discard block |
||
| 212 | 212 | * |
| 213 | 213 | * @return string |
| 214 | 214 | */ |
| 215 | - public function locale(?string $locale = null): string |
|
| 215 | + public function locale(?string $locale=null): string |
|
| 216 | 216 | { |
| 217 | 217 | if ($locale !== null) { |
| 218 | - $this->currentLocale = $locale; |
|
| 219 | - $this->pluralRule = RuleFactory::make($locale); |
|
| 218 | + $this->currentLocale=$locale; |
|
| 219 | + $this->pluralRule=RuleFactory::make($locale); |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | return $this->currentLocale; |
@@ -270,27 +270,27 @@ discard block |
||
| 270 | 270 | * |
| 271 | 271 | * @return string |
| 272 | 272 | */ |
| 273 | - public function t(string $key, $options = []): string |
|
| 273 | + public function t(string $key, $options=[]): string |
|
| 274 | 274 | { |
| 275 | - $phrase = ''; |
|
| 276 | - $result = ''; |
|
| 277 | - $options = is_int($options) ? ['smart_count' => $options] : $options; |
|
| 275 | + $phrase=''; |
|
| 276 | + $result=''; |
|
| 277 | + $options=is_int($options) ? ['smart_count' => $options] : $options; |
|
| 278 | 278 | |
| 279 | 279 | if ($this->has($key) && is_string($this->phrases[$key])) { |
| 280 | - $phrase = $this->phrases[$key]; |
|
| 281 | - } elseif(isset($options['_'])) { |
|
| 282 | - $phrase = $options['_']; |
|
| 280 | + $phrase=$this->phrases[$key]; |
|
| 281 | + } elseif (isset($options['_'])) { |
|
| 282 | + $phrase=$options['_']; |
|
| 283 | 283 | } elseif ($this->onMissingKey !== null) { |
| 284 | - $result = call_user_func($this->onMissingKey, $key, $options, $this->currentLocale, $this->tokenRegex, $this); |
|
| 284 | + $result=call_user_func($this->onMissingKey, $key, $options, $this->currentLocale, $this->tokenRegex, $this); |
|
| 285 | 285 | } else { |
| 286 | 286 | // warn missing translations |
| 287 | - call_user_func($this->warn, 'Missing translation for key: "' . $key . '"'); |
|
| 287 | + call_user_func($this->warn, 'Missing translation for key: "'.$key.'"'); |
|
| 288 | 288 | |
| 289 | - $result = $key; |
|
| 289 | + $result=$key; |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | if (!empty($phrase)) { |
| 293 | - $result = $this->transformPhrase($phrase, $options, $this->currentLocale, $this->tokenRegex); |
|
| 293 | + $result=$this->transformPhrase($phrase, $options, $this->currentLocale, $this->tokenRegex); |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | return $result; |
@@ -326,35 +326,35 @@ discard block |
||
| 326 | 326 | * |
| 327 | 327 | * @return string |
| 328 | 328 | */ |
| 329 | - public function transformPhrase(string $phrase, $substitutions = null, string $locale = 'en', ?string $tokenRegex = null): string |
|
| 329 | + public function transformPhrase(string $phrase, $substitutions=null, string $locale='en', ?string $tokenRegex=null): string |
|
| 330 | 330 | { |
| 331 | 331 | if ($substitutions === null) { |
| 332 | 332 | return $phrase; |
| 333 | 333 | } |
| 334 | 334 | |
| 335 | - $result = $phrase; |
|
| 335 | + $result=$phrase; |
|
| 336 | 336 | |
| 337 | 337 | // allow number as a pluralization shortcut |
| 338 | - $options = is_int($substitutions) ? ['smart_count' => $substitutions] : $substitutions; |
|
| 338 | + $options=is_int($substitutions) ? ['smart_count' => $substitutions] : $substitutions; |
|
| 339 | 339 | |
| 340 | 340 | // Select plural form: based on a phrase text that contains `n` |
| 341 | 341 | // plural forms separated by `delimiter`, a `locale`, and a `substitutions.smart_count`, |
| 342 | 342 | // choose the correct plural form. This is only done if `count` is set. |
| 343 | 343 | if (($options['smart_count'] ?? null) !== null && $result) { |
| 344 | - $texts = explode($this->delimiter, $result); |
|
| 345 | - $pluralRule = $locale !== $this->currentLocale ? RuleFactory::make($locale) : $this->pluralRule; |
|
| 346 | - $pluralTypeIndex = $pluralRule->decide($options['smart_count']); |
|
| 347 | - $result = trim($texts[$pluralTypeIndex] ?? $texts[0]); |
|
| 344 | + $texts=explode($this->delimiter, $result); |
|
| 345 | + $pluralRule=$locale !== $this->currentLocale ? RuleFactory::make($locale) : $this->pluralRule; |
|
| 346 | + $pluralTypeIndex=$pluralRule->decide($options['smart_count']); |
|
| 347 | + $result=trim($texts[$pluralTypeIndex] ?? $texts[0]); |
|
| 348 | 348 | } |
| 349 | 349 | |
| 350 | - $interpolationRegex = $tokenRegex ?? $this->tokenRegex; |
|
| 350 | + $interpolationRegex=$tokenRegex ?? $this->tokenRegex; |
|
| 351 | 351 | |
| 352 | 352 | return preg_replace_callback($interpolationRegex, static function($matches) use ($options) { |
| 353 | 353 | if (array_key_exists($matches[1], $options) && $options[$matches[1]] !== null) { |
| 354 | 354 | return $options[$matches[1]]; |
| 355 | 355 | } |
| 356 | 356 | |
| 357 | - return ($options['interpolation']['prefix'] ?? '%{') . $matches[1] . ($options['interpolation']['suffix'] ?? '}'); |
|
| 357 | + return ($options['interpolation']['prefix'] ?? '%{').$matches[1].($options['interpolation']['suffix'] ?? '}'); |
|
| 358 | 358 | }, $result); |
| 359 | 359 | } |
| 360 | 360 | |
@@ -365,15 +365,15 @@ discard block |
||
| 365 | 365 | * |
| 366 | 366 | * @return string |
| 367 | 367 | */ |
| 368 | - private function constructTokenRegex(array $options = []): string |
|
| 368 | + private function constructTokenRegex(array $options=[]): string |
|
| 369 | 369 | { |
| 370 | - $prefix = $options['prefix'] ?? '%{'; |
|
| 371 | - $suffix = $options['suffix'] ?? '}'; |
|
| 370 | + $prefix=$options['prefix'] ?? '%{'; |
|
| 371 | + $suffix=$options['suffix'] ?? '}'; |
|
| 372 | 372 | |
| 373 | 373 | if ($prefix === $this->delimiter || $suffix === $this->delimiter) { |
| 374 | - throw new RuntimeException('"' . $this->delimiter . '" token is reserved for pluralization'); |
|
| 374 | + throw new RuntimeException('"'.$this->delimiter.'" token is reserved for pluralization'); |
|
| 375 | 375 | } |
| 376 | 376 | |
| 377 | - return '~' . preg_quote($prefix) . '(.*?)' . preg_quote($suffix) . '~'; |
|
| 377 | + return '~'.preg_quote($prefix).'(.*?)'.preg_quote($suffix).'~'; |
|
| 378 | 378 | } |
| 379 | 379 | } |
| 380 | 380 | \ No newline at end of file |