Skip to content

Commit d0f1ba6

Browse files
committed
Refactor UniqueTranslationValidator
1 parent 1adf0d7 commit d0f1ba6

File tree

2 files changed

+43
-62
lines changed

2 files changed

+43
-62
lines changed

src/UniqueTranslationRule.php

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,32 @@
22

33
namespace CodeZero\UniqueTranslation;
44

5-
use DB;
6-
use Illuminate\Contracts\Validation\Rule;
7-
8-
class UniqueTranslationRule implements Rule
5+
class UniqueTranslationRule
96
{
107
/**
118
* @var string
129
*/
13-
protected $table;
10+
protected $rule = 'unique_translation';
1411

1512
/**
1613
* @var string
1714
*/
18-
protected $column;
15+
protected $table;
1916

2017
/**
21-
* @var string
18+
* @var string|null
2219
*/
23-
protected $attribute;
20+
protected $column = null;
2421

2522
/**
2623
* @var mixed
2724
*/
28-
protected $ignoreValue;
25+
protected $ignoreValue = null;
2926

3027
/**
31-
* @var string
28+
* @var string|null
3229
*/
33-
protected $ignoreColumn;
30+
protected $ignoreColumn = null;
3431

3532
/**
3633
* Create a new rule instance.
@@ -61,50 +58,19 @@ public function ignore($value, $column = 'id')
6158
}
6259

6360
/**
64-
* Determine if the validation rule passes.
65-
*
66-
* @param string $attribute
67-
* @param string $value
68-
*
69-
* @return bool
70-
*/
71-
public function passes($attribute, $value)
72-
{
73-
$attributes = explode('.', $attribute);
74-
$this->attribute = $attributes[0];
75-
$this->column = $this->column ?: $attributes[0];
76-
$locale = $attributes[1] ?? app()->getLocale();
77-
78-
return $this->isUnique($this->column, $locale, $value);
79-
}
80-
81-
/**
82-
* Get the validation error message.
61+
* Generate a string representation of the validation rule.
8362
*
8463
* @return string
8564
*/
86-
public function message()
65+
public function __toString()
8766
{
88-
return trans('validation.unique');
89-
}
90-
91-
/**
92-
* Check if the given translated value does not exist in the database.
93-
*
94-
* @param string $column
95-
* @param string $locale
96-
* @param string $value
97-
*
98-
* @return bool
99-
*/
100-
protected function isUnique($column, $locale, $value)
101-
{
102-
$query = DB::table($this->table)->where("{$column}->{$locale}", '=', $value);
103-
104-
if ($this->ignoreColumn !== null) {
105-
$query = $query->where($this->ignoreColumn, '!=', $this->ignoreValue);
106-
}
107-
108-
return $query->count() === 0;
67+
return sprintf(
68+
'%s:%s,%s,%s,%s',
69+
$this->rule,
70+
$this->table,
71+
$this->column ?: 'NULL',
72+
$this->ignoreValue ?: 'NULL',
73+
$this->ignoreColumn ?: 'NULL'
74+
);
10975
}
11076
}

src/UniqueTranslationValidator.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public function validate($attribute, $value, $parameters, $validator) {
2121
$attribute = $attributeParts[0];
2222
$locale = $attributeParts[1] ?? app()->getLocale();
2323
$table = $parameters[0] ?? null;
24-
$column = $parameters[1] ?? null;
25-
$ignoreValue = $parameters[2] ?? null;
26-
$ignoreColumn = $parameters[3] ?? null;
24+
$column = $this->filterNullValues($parameters[1] ?? null) ?: $attribute;
25+
$ignoreValue = $this->filterNullValues($parameters[2] ?? null);
26+
$ignoreColumn = $this->filterNullValues($parameters[3] ?? null);
2727

28-
$isUnique = $this->isUnique($attribute, $locale, $value, $table, $column, $ignoreValue, $ignoreColumn);
28+
$isUnique = $this->isUnique($value, $locale, $table, $column, $ignoreValue, $ignoreColumn);
2929

3030
$validator->setCustomMessages([
3131
'unique_translation' => trans('validation.unique'),
@@ -34,23 +34,38 @@ public function validate($attribute, $value, $parameters, $validator) {
3434
return $isUnique;
3535
}
3636

37+
/**
38+
* Filter NULL values.
39+
*
40+
* @param string|null $value
41+
*
42+
* @return string|null
43+
*/
44+
protected function filterNullValues($value)
45+
{
46+
$nullValues = ['null', 'NULL'];
47+
48+
if (in_array($value, $nullValues)) {
49+
return null;
50+
}
51+
52+
return $value;
53+
}
54+
3755
/**
3856
* Check if a translation is unique.
3957
*
40-
* @param string $attribute
41-
* @param string $locale
4258
* @param mixed $value
59+
* @param string $locale
4360
* @param string $table
44-
* @param string|null $column
61+
* @param string $column
4562
* @param mixed $ignoreValue
4663
* @param string|null $ignoreColumn
4764
*
4865
* @return bool
4966
*/
50-
protected function isUnique($attribute, $locale, $value, $table, $column = null, $ignoreValue = null, $ignoreColumn = null)
67+
protected function isUnique($value, $locale, $table, $column, $ignoreValue = null, $ignoreColumn = null)
5168
{
52-
$column = $column ?: $attribute;
53-
5469
$query = $this->findTranslation($table, $column, $locale, $value);
5570
$query = $this->ignore($query, $ignoreColumn, $ignoreValue);
5671

0 commit comments

Comments
 (0)