|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Astrotomic\Translatable\Validation; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use Astrotomic\Translatable\Locales; |
| 7 | +use Illuminate\Contracts\Config\Repository; |
| 8 | + |
| 9 | +class RuleFactory |
| 10 | +{ |
| 11 | + const FORMAT_ARRAY = 1; |
| 12 | + const FORMAT_KEY = 2; |
| 13 | + |
| 14 | + /** |
| 15 | + * @var int |
| 16 | + */ |
| 17 | + protected $format; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $prefix; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $suffix; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var null|array |
| 31 | + */ |
| 32 | + protected $locales = null; |
| 33 | + |
| 34 | + public function __construct(Repository $config, ?int $format = null, ?string $prefix = null, ?string $suffix = null) |
| 35 | + { |
| 36 | + $this->format = $format ?? $config->get('translatable.rule_factory.format'); |
| 37 | + $this->prefix = $prefix ?? $config->get('translatable.rule_factory.prefix'); |
| 38 | + $this->suffix = $suffix ?? $config->get('translatable.rule_factory.suffix'); |
| 39 | + } |
| 40 | + |
| 41 | + public static function make(array $rules, ?int $format = null, ?string $prefix = null, ?string $suffix = null, ?array $locales = null): array |
| 42 | + { |
| 43 | + $factory = app()->make(static::class, compact('format', 'prefix', 'suffix')); |
| 44 | + |
| 45 | + $factory->setLocales($locales); |
| 46 | + |
| 47 | + return $factory->parse($rules); |
| 48 | + } |
| 49 | + |
| 50 | + public function setLocales(?array $locales = null): self |
| 51 | + { |
| 52 | + /** @var Locales */ |
| 53 | + $helper = app(Locales::class); |
| 54 | + |
| 55 | + if (is_null($locales)) { |
| 56 | + $this->locales = $helper->all(); |
| 57 | + |
| 58 | + return $this; |
| 59 | + } |
| 60 | + |
| 61 | + foreach ($locales as $locale) { |
| 62 | + if (! $helper->has($locale)) { |
| 63 | + throw new InvalidArgumentException(sprintf('The locale [%s] is not defined in available locales.', $locale)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + $this->locales = $locales; |
| 68 | + |
| 69 | + return $this; |
| 70 | + } |
| 71 | + |
| 72 | + public function parse(array $input): array |
| 73 | + { |
| 74 | + $rules = []; |
| 75 | + |
| 76 | + foreach ($input as $key => $value) { |
| 77 | + if (! $this->isTranslatable($key)) { |
| 78 | + $rules[$key] = $value; |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + foreach ($this->locales as $locale) { |
| 83 | + $rules[$this->formatKey($locale, $key)] = $value; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return $rules; |
| 88 | + } |
| 89 | + |
| 90 | + protected function formatKey(string $locale, string $key): string |
| 91 | + { |
| 92 | + switch ($this->format) { |
| 93 | + case self::FORMAT_ARRAY: |
| 94 | + return preg_replace($this->getPattern(), $locale.'.$1', $key); |
| 95 | + case self::FORMAT_KEY: |
| 96 | + return preg_replace($this->getPattern(), '$1:'.$locale, $key); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + protected function getPattern(): string |
| 101 | + { |
| 102 | + $prefix = preg_quote($this->prefix); |
| 103 | + $suffix = preg_quote($this->suffix); |
| 104 | + |
| 105 | + return '/'.$prefix.'([^\.'.$prefix.$suffix.']+)'.$suffix.'/'; |
| 106 | + } |
| 107 | + |
| 108 | + protected function isTranslatable(string $key): bool |
| 109 | + { |
| 110 | + return strpos($key, $this->prefix) !== false && strpos($key, $this->suffix) !== false; |
| 111 | + } |
| 112 | +} |
0 commit comments