Skip to content

Commit 10c91eb

Browse files
committed
Add named constructor to UniqueTranslationRule
1 parent b618bf7 commit 10c91eb

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/UniqueTranslationRule.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ class UniqueTranslationRule
2929
*/
3030
protected $ignoreColumn = null;
3131

32+
/**
33+
* Create a new rule instance.
34+
*
35+
* @param string $table
36+
* @param string|null $column
37+
*
38+
* @return static
39+
*/
40+
public static function for($table, $column = null)
41+
{
42+
return new static($table, $column);
43+
}
44+
3245
/**
3346
* Create a new rule instance.
3447
*

tests/UniqueTranslationTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function it_checks_if_the_translation_for_the_current_locale_is_unique()
4444
{
4545
$rules = [
4646
'slug' => "{$this->rule}:{$this->table}",
47-
'name' => new UniqueTranslationRule($this->table),
47+
'name' => UniqueTranslationRule::for($this->table),
4848
];
4949

5050
$this->createRoute('test', $rules);
@@ -65,7 +65,7 @@ public function it_checks_if_the_translation_for_a_specific_locale_is_unique()
6565
{
6666
$rules = [
6767
'slug.*' => "{$this->rule}:{$this->table}",
68-
'name.*' => new UniqueTranslationRule($this->table),
68+
'name.*' => UniqueTranslationRule::for($this->table),
6969
];
7070

7171
$this->createRoute('test', $rules);
@@ -96,7 +96,7 @@ public function the_models_attribute_name_can_be_specified()
9696
{
9797
$rules = [
9898
'form_slug' => "{$this->rule}:{$this->table},slug",
99-
'form_name' => new UniqueTranslationRule($this->table, 'name'),
99+
'form_name' => UniqueTranslationRule::for($this->table, 'name'),
100100
];
101101

102102
$this->createRoute('test-single', $rules);
@@ -108,7 +108,7 @@ public function the_models_attribute_name_can_be_specified()
108108

109109
$rules = [
110110
'form_slug.*' => "{$this->rule}:{$this->table},slug",
111-
'form_name.*' => new UniqueTranslationRule($this->table, 'name'),
111+
'form_name.*' => UniqueTranslationRule::for($this->table, 'name'),
112112
];
113113

114114
$this->createRoute('test-array', $rules);
@@ -124,7 +124,7 @@ public function it_ignores_the_given_id()
124124
{
125125
$rules = [
126126
'slug' => "{$this->rule}:{$this->table},slug,{$this->model->id}",
127-
'name' => (new UniqueTranslationRule($this->table))->ignore($this->model->id),
127+
'name' => UniqueTranslationRule::for($this->table)->ignore($this->model->id),
128128
];
129129

130130
$this->createRoute('test-single', $rules);
@@ -136,7 +136,7 @@ public function it_ignores_the_given_id()
136136

137137
$rules = [
138138
'slug.*' => "{$this->rule}:{$this->table},slug,{$this->model->id}",
139-
'name.*' => (new UniqueTranslationRule($this->table))->ignore($this->model->id),
139+
'name.*' => UniqueTranslationRule::for($this->table)->ignore($this->model->id),
140140
];
141141

142142
$this->createRoute('test-array', $rules);
@@ -152,7 +152,7 @@ public function it_ignores_a_specific_attribute_with_the_given_value()
152152
{
153153
$rules = [
154154
'slug' => "{$this->rule}:{$this->table},slug,{$this->model->other_field},other_field",
155-
'name' => (new UniqueTranslationRule($this->table))->ignore($this->model->other_field, 'other_field'),
155+
'name' => UniqueTranslationRule::for($this->table)->ignore($this->model->other_field, 'other_field'),
156156
];
157157

158158
$this->createRoute('test-single', $rules);
@@ -164,7 +164,7 @@ public function it_ignores_a_specific_attribute_with_the_given_value()
164164

165165
$rules = [
166166
'slug.*' => "{$this->rule}:{$this->table},slug,{$this->model->other_field},other_field",
167-
'name.*' => (new UniqueTranslationRule($this->table))->ignore($this->model->other_field, 'other_field'),
167+
'name.*' => UniqueTranslationRule::for($this->table)->ignore($this->model->other_field, 'other_field'),
168168
];
169169

170170
$this->createRoute('test-array', $rules);
@@ -180,7 +180,7 @@ public function it_returns_a_default_error_message_when_validating_a_single_tran
180180
{
181181
$rules = [
182182
'form_slug' => "{$this->rule}:{$this->table},slug",
183-
'form_name' => new UniqueTranslationRule($this->table, 'name'),
183+
'form_name' => UniqueTranslationRule::for($this->table, 'name'),
184184
];
185185

186186
$this->createRoute('test', $rules);
@@ -219,7 +219,7 @@ public function it_returns_a_default_error_message_when_validating_an_array()
219219
{
220220
$rules = [
221221
'form_slug.*' => "{$this->rule}:{$this->table},slug",
222-
'form_name.*' => new UniqueTranslationRule($this->table, 'name'),
222+
'form_name.*' => UniqueTranslationRule::for($this->table, 'name'),
223223
];
224224

225225
$this->createRoute('test', $rules);
@@ -258,7 +258,7 @@ public function it_returns_a_custom_error_message_when_validating_a_single_trans
258258
{
259259
$rules = [
260260
'form_slug' => "{$this->rule}:{$this->table},slug",
261-
'form_name' => new UniqueTranslationRule($this->table, 'name'),
261+
'form_name' => UniqueTranslationRule::for($this->table, 'name'),
262262
];
263263

264264
$messages = [
@@ -302,7 +302,7 @@ public function it_returns_a_custom_error_message_when_validating_an_array()
302302
{
303303
$rules = [
304304
'form_slug.*' => "{$this->rule}:{$this->table},slug",
305-
'form_name.*' => new UniqueTranslationRule($this->table, 'name'),
305+
'form_name.*' => UniqueTranslationRule::for($this->table, 'name'),
306306
];
307307

308308
$messages = [

0 commit comments

Comments
 (0)