Skip to content

Commit 6d7444b

Browse files
committed
Refactor
1 parent f6bd785 commit 6d7444b

File tree

1 file changed

+74
-32
lines changed

1 file changed

+74
-32
lines changed

src/UniqueTranslationValidator.php

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

33
namespace CodeZero\UniqueTranslation;
44

5+
use App;
6+
use Config;
57
use DB;
68

79
class UniqueTranslationValidator
@@ -16,60 +18,99 @@ class UniqueTranslationValidator
1618
*
1719
* @return bool
1820
*/
19-
public function validate($attribute, $value, $parameters, $validator) {
20-
$attributeParts = explode('.', $attribute);
21-
$name = $attributeParts[0];
22-
$locale = $attributeParts[1] ?? app()->getLocale();
23-
$column = $this->filterNullValues($parameters[1] ?? null) ?: $name;
24-
$ignoreValue = $this->filterNullValues($parameters[2] ?? null);
25-
$ignoreColumn = $this->filterNullValues($parameters[3] ?? null);
26-
27-
$table = $parameters[0] ?? null;
28-
$tableParts = explode('.', $table);
29-
$connection = isset($tableParts[1]) ? $tableParts[0] : config('database.default');
30-
$table = $tableParts[1] ?? $tableParts[0];
31-
32-
$isUnique = $this->isUnique($value, $locale, $connection, $table, $column, $ignoreValue, $ignoreColumn);
33-
34-
if ( ! $isUnique) {
35-
$this->addErrorsToValidator($validator, $parameters, $name, $locale);
21+
public function validate($attribute, $value, $parameters, $validator)
22+
{
23+
list ($name, $locale) = $this->getAttributeNameAndLocale($attribute);
24+
25+
if ($this->isUnique($value, $name, $locale, $parameters)) {
26+
return true;
3627
}
3728

38-
return $isUnique;
29+
$this->addErrorsToValidator($validator, $parameters, $name, $locale);
30+
31+
return false;
3932
}
4033

4134
/**
42-
* Filter NULL values.
35+
* Get the attribute name and locale.
4336
*
44-
* @param string|null $value
37+
* @param string $attribute
4538
*
46-
* @return string|null
39+
* @return array
4740
*/
48-
protected function filterNullValues($value)
41+
protected function getAttributeNameAndLocale($attribute)
4942
{
50-
$nullValues = ['null', 'NULL'];
43+
$parts = explode('.', $attribute);
5144

52-
if (in_array($value, $nullValues)) {
53-
return null;
54-
}
45+
$name = $parts[0];
46+
$locale = $parts[1] ?? App::getLocale();
47+
48+
return [$name, $locale];
49+
}
50+
51+
/**
52+
* Get the database connection and table name.
53+
*
54+
* @param array $parameters
55+
*
56+
* @return array
57+
*/
58+
protected function getConnectionAndTable($parameters)
59+
{
60+
$parts = explode('.', $this->getParameter($parameters, 0));
61+
62+
$connection = isset($parts[1])
63+
? $parts[0]
64+
: Config::get('database.default');
65+
66+
$table = $parts[1] ?? $parts[0];
67+
68+
return [$connection, $table];
69+
}
70+
71+
/**
72+
* Get the parameter value at the given index.
73+
*
74+
* @param array $parameters
75+
* @param int $index
76+
*
77+
* @return string|null
78+
*/
79+
protected function getParameter($parameters, $index)
80+
{
81+
return $this->convertNullValue($parameters[$index] ?? null);
82+
}
5583

56-
return $value;
84+
/**
85+
* Convert any 'NULL' string value to null.
86+
*
87+
* @param string $value
88+
*
89+
* @return string|null
90+
*/
91+
protected function convertNullValue($value)
92+
{
93+
return strtoupper($value) === 'NULL' ? null : $value;
5794
}
5895

5996
/**
6097
* Check if a translation is unique.
6198
*
6299
* @param mixed $value
100+
* @param string $name
63101
* @param string $locale
64-
* @param string $table
65-
* @param string $column
66-
* @param mixed $ignoreValue
67-
* @param string|null $ignoreColumn
102+
* @param array $parameters
68103
*
69104
* @return bool
70105
*/
71-
protected function isUnique($value, $locale, $connection, $table, $column, $ignoreValue = null, $ignoreColumn = null)
106+
protected function isUnique($value, $name, $locale, $parameters)
72107
{
108+
list ($connection, $table) = $this->getConnectionAndTable($parameters);
109+
110+
$column = $this->getParameter($parameters, 1) ?? $name;
111+
$ignoreValue = $this->getParameter($parameters, 2);
112+
$ignoreColumn = $this->getParameter($parameters, 3);
113+
73114
$query = $this->findTranslation($connection, $table, $column, $locale, $value);
74115
$query = $this->ignore($query, $ignoreColumn, $ignoreValue);
75116

@@ -81,6 +122,7 @@ protected function isUnique($value, $locale, $connection, $table, $column, $igno
81122
/**
82123
* Find the given translated value in the database.
83124
*
125+
* @param string $connection
84126
* @param string $table
85127
* @param string $column
86128
* @param string $locale

0 commit comments

Comments
 (0)