Skip to content

Commit 26c28c6

Browse files
authored
Merge pull request #34 from Astrotomic/ft-rule-factory
rule factory
2 parents 34e7377 + 4de0511 commit 26c28c6

File tree

5 files changed

+409
-0
lines changed

5 files changed

+409
-0
lines changed

.codeclimate.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ checks:
1010
enabled: false
1111
method-complexity:
1212
enabled: true
13+
config:
14+
threshold: 6
1315
method-count:
1416
enabled: true
1517
method-lines:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor/
22
composer.lock
33
coverage/
4+
.phpunit.result.cache
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}

src/config/translatable.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,19 @@
131131
|
132132
*/
133133
'to_array_always_loads_translations' => true,
134+
135+
/*
136+
|--------------------------------------------------------------------------
137+
| Configure the default behavior of the rule factory
138+
|--------------------------------------------------------------------------
139+
| The default values used to control the behavior of the RuleFactory.
140+
| Here you can set your own default format and delimiters for
141+
| your whole app.
142+
*
143+
*/
144+
'rule_factory' => [
145+
'format' => \Astrotomic\Translatable\Validation\RuleFactory::FORMAT_ARRAY,
146+
'prefix' => '%',
147+
'suffix' => '%',
148+
],
134149
];

0 commit comments

Comments
 (0)