Skip to content

Commit 5e14c12

Browse files
committed
add rule factory configuration
1 parent 13a46b8 commit 5e14c12

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

src/Translatable/Validation/RuleFactory.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Astrotomic\Translatable\Validation;
44

5+
use Illuminate\Contracts\Config\Repository;
56
use InvalidArgumentException;
67
use Astrotomic\Translatable\Locales;
78

@@ -30,16 +31,16 @@ class RuleFactory
3031
*/
3132
protected $locales = null;
3233

33-
public function __construct(int $format = self::FORMAT_ARRAY, string $prefix = '%', string $suffix = '%')
34+
public function __construct(Repository $config, ?int $format = null, ?string $prefix = null, ?string $suffix = null)
3435
{
35-
$this->format = $format;
36-
$this->prefix = $prefix;
37-
$this->suffix = $suffix;
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');
3839
}
3940

40-
public static function make(array $rules, int $format = self::FORMAT_ARRAY, string $prefix = '%', string $suffix = '%', ?array $locales = null): array
41+
public static function make(array $rules, ?int $format = null, ?string $prefix = null, ?string $suffix = null, ?array $locales = null): array
4142
{
42-
$factory = new static($format, $prefix, $suffix);
43+
$factory = app()->make(static::class, compact('format', 'prefix', 'suffix'));
4344

4445
$factory->setLocales($locales);
4546

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
];

tests/ValidationTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,30 @@ public function test_format_array_it_replaces_middle_key_with_custom_regex_delim
130130
], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY, '$', '$'));
131131
}
132132

133+
public function test_format_array_it_uses_config_as_default()
134+
{
135+
app('config')->set('translatable.rule_factory', [
136+
'format' => RuleFactory::FORMAT_ARRAY,
137+
'prefix' => '{',
138+
'suffix' => '}',
139+
]);
140+
141+
$rules = [
142+
'title' => 'required',
143+
'{content}' => 'required',
144+
'%content%' => 'required',
145+
];
146+
147+
$this->assertEquals([
148+
'title' => 'required',
149+
'%content%' => 'required',
150+
'en.content' => 'required',
151+
'de.content' => 'required',
152+
'de-DE.content' => 'required',
153+
'de-AT.content' => 'required',
154+
], RuleFactory::make($rules));
155+
}
156+
133157
public function test_format_key_it_replaces_single_key()
134158
{
135159
$rules = [
@@ -178,6 +202,30 @@ public function test_format_key_it_replaces_middle_key()
178202
], RuleFactory::make($rules, RuleFactory::FORMAT_KEY));
179203
}
180204

205+
public function test_format_key_it_uses_config_as_default()
206+
{
207+
app('config')->set('translatable.rule_factory', [
208+
'format' => RuleFactory::FORMAT_KEY,
209+
'prefix' => '{',
210+
'suffix' => '}',
211+
]);
212+
213+
$rules = [
214+
'title' => 'required',
215+
'{content}' => 'required',
216+
'%content%' => 'required',
217+
];
218+
219+
$this->assertEquals([
220+
'title' => 'required',
221+
'%content%' => 'required',
222+
'content:en' => 'required',
223+
'content:de' => 'required',
224+
'content:de-DE' => 'required',
225+
'content:de-AT' => 'required',
226+
], RuleFactory::make($rules));
227+
}
228+
181229
public function test_it_replaces_key_with_custom_locales()
182230
{
183231
$rules = [

0 commit comments

Comments
 (0)