Skip to content

Commit a11f8a0

Browse files
maniabapaulbalandan
authored andcommitted
refactor: code for is_unique and is_not_unique methods; extracted common code into prepareUniqueQuery method
1 parent 25a1adc commit a11f8a0

File tree

2 files changed

+40
-103
lines changed

2 files changed

+40
-103
lines changed

system/Validation/Rules.php

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Validation;
1515

16+
use CodeIgniter\Database\BaseBuilder;
1617
use CodeIgniter\Exceptions\InvalidArgumentException;
1718
use CodeIgniter\Helpers\Array\ArrayHelper;
1819
use Config\Database;
@@ -118,40 +119,17 @@ public function greater_than_equal_to($str, string $min): bool
118119
*/
119120
public function is_not_unique($str, string $field, array $data): bool
120121
{
121-
if (! is_string($str) && $str !== null) {
122-
$str = (string) $str;
123-
}
124-
125-
// Grab any data for exclusion of a single row.
126-
[$field, $whereField, $whereValue] = array_pad(explode(',', $field), 3, null);
127-
128-
// Break the dbGroup, table and field apart from the field string
129-
$parts = explode('.', $field, 3);
130-
$numParts = count($parts);
131-
132-
if ($numParts === 3) {
133-
[$dbGroup, $table, $field] = $parts;
134-
} elseif ($numParts === 2) {
135-
[$table, $field] = $parts;
136-
} else {
137-
throw new InvalidArgumentException('The field must be in the format "table.field" or "dbGroup.table.field".');
138-
}
139-
140-
$row = Database::connect($dbGroup ?? $data['DBGroup'] ?? null)
141-
->table($table)
142-
->select('1')
143-
->where($field, $str)
144-
->limit(1);
122+
[$builder, $whereField, $whereValue] = $this->prepareUniqueQuery($str, $field, $data);
145123

146124
if (
147125
$whereField !== null && $whereField !== ''
148126
&& $whereValue !== null && $whereValue !== ''
149127
&& ! preg_match('/^\{(\w+)\}$/', $whereValue)
150128
) {
151-
$row = $row->where($whereField, $whereValue);
129+
$builder = $builder->where($whereField, $whereValue);
152130
}
153131

154-
return $row->get()->getRow() !== null;
132+
return $builder->get()->getRow() !== null;
155133
}
156134

157135
/**
@@ -184,14 +162,38 @@ public function in_list($value, string $list): bool
184162
*/
185163
public function is_unique($str, string $field, array $data): bool
186164
{
187-
if (! is_string($str) && $str !== null) {
188-
$str = (string) $str;
165+
[$builder, $ignoreField, $ignoreValue] = $this->prepareUniqueQuery($str, $field, $data);
166+
167+
if (
168+
$ignoreField !== null && $ignoreField !== ''
169+
&& $ignoreValue !== null && $ignoreValue !== ''
170+
&& ! preg_match('/^\{(\w+)\}$/', $ignoreValue)
171+
) {
172+
$builder = $builder->where("{$ignoreField} !=", $ignoreValue);
173+
}
174+
175+
return $builder->get()->getRow() === null;
176+
}
177+
178+
/**
179+
* Prepares the database query for uniqueness checks.
180+
*
181+
* @param mixed $value The value to check.
182+
* @param string $field The field parameters.
183+
* @param array $data Additional data.
184+
*
185+
* @return array{0: BaseBuilder, 1: string|null, 2: string|null}
186+
*/
187+
private function prepareUniqueQuery($value, string $field, array $data): array
188+
{
189+
if (! is_string($value) && $value !== null) {
190+
$value = (string) $value;
189191
}
190192

191-
// Grab any data for exclusion of a single row.
192-
[$field, $ignoreField, $ignoreValue] = array_pad(explode(',', $field), 3, null);
193+
// Split the field parameters and pad the array to ensure three elements.
194+
[$field, $extraField, $extraValue] = array_pad(explode(',', $field), 3, null);
193195

194-
// Break the dbGroup, table and field apart from the field string
196+
// Parse the field string to extract dbGroup, table, and field.
195197
$parts = explode('.', $field, 3);
196198
$numParts = count($parts);
197199

@@ -203,21 +205,15 @@ public function is_unique($str, string $field, array $data): bool
203205
throw new InvalidArgumentException('The field must be in the format "table.field" or "dbGroup.table.field".');
204206
}
205207

206-
$row = Database::connect($dbGroup ?? $data['DBGroup'] ?? null)
208+
// Connect to the database.
209+
$dbGroup ??= $data['DBGroup'] ?? null;
210+
$builder = Database::connect($dbGroup)
207211
->table($table)
208212
->select('1')
209-
->where($field, $str)
213+
->where($field, $value)
210214
->limit(1);
211215

212-
if (
213-
$ignoreField !== null && $ignoreField !== ''
214-
&& $ignoreValue !== null && $ignoreValue !== ''
215-
&& ! preg_match('/^\{(\w+)\}$/', $ignoreValue)
216-
) {
217-
$row = $row->where("{$ignoreField} !=", $ignoreValue);
218-
}
219-
220-
return $row->get()->getRow() === null;
216+
return [$builder, $extraField, $extraValue];
221217
}
222218

223219
/**

system/Validation/StrictRules/Rules.php

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use CodeIgniter\Helpers\Array\ArrayHelper;
1717
use CodeIgniter\Validation\Rules as NonStrictRules;
1818
use Config\Database;
19-
use InvalidArgumentException;
2019

2120
/**
2221
* Validation Rules.
@@ -147,36 +146,7 @@ public function is_not_unique($str, string $field, array $data): bool
147146
return false;
148147
}
149148

150-
// Grab any data for exclusion of a single row.
151-
[$field, $whereField, $whereValue] = array_pad(explode(',', $field), 3, null);
152-
153-
// Break the dbGroup, table and field apart from the field string
154-
$parts = explode('.', $field, 3);
155-
$numParts = count($parts);
156-
157-
if ($numParts === 3) {
158-
[$dbGroup, $table, $field] = $parts;
159-
} elseif ($numParts === 2) {
160-
[$table, $field] = $parts;
161-
} else {
162-
throw new InvalidArgumentException('The field must be in the format "table.field" or "dbGroup.table.field".');
163-
}
164-
165-
$row = Database::connect($dbGroup ?? $data['DBGroup'] ?? null)
166-
->table($table)
167-
->select('1')
168-
->where($field, $str)
169-
->limit(1);
170-
171-
if (
172-
$whereField !== null && $whereField !== ''
173-
&& $whereValue !== null && $whereValue !== ''
174-
&& ! preg_match('/^\{(\w+)\}$/', $whereValue)
175-
) {
176-
$row = $row->where($whereField, $whereValue);
177-
}
178-
179-
return $row->get()->getRow() !== null;
149+
return $this->nonStrictRules->is_not_unique($str, $field, $data);
180150
}
181151

182152
/**
@@ -215,36 +185,7 @@ public function is_unique($str, string $field, array $data): bool
215185
return false;
216186
}
217187

218-
// Grab any data for exclusion of a single row.
219-
[$field, $ignoreField, $ignoreValue] = array_pad(explode(',', $field), 3, null);
220-
221-
// Break the dbGroup, table and field apart from the field string
222-
$parts = explode('.', $field, 3);
223-
$numParts = count($parts);
224-
225-
if ($numParts === 3) {
226-
[$dbGroup, $table, $field] = $parts;
227-
} elseif ($numParts === 2) {
228-
[$table, $field] = $parts;
229-
} else {
230-
throw new InvalidArgumentException('The field must be in the format "table.field" or "dbGroup.table.field".');
231-
}
232-
233-
$row = Database::connect($dbGroup ?? $data['DBGroup'] ?? null)
234-
->table($table)
235-
->select('1')
236-
->where($field, $str)
237-
->limit(1);
238-
239-
if (
240-
$ignoreField !== null && $ignoreField !== ''
241-
&& $ignoreValue !== null && $ignoreValue !== ''
242-
&& ! preg_match('/^\{(\w+)\}$/', $ignoreValue)
243-
) {
244-
$row = $row->where("{$ignoreField} !=", $ignoreValue);
245-
}
246-
247-
return $row->get()->getRow() === null;
188+
return $this->nonStrictRules->is_unique($str, $field, $data);
248189
}
249190

250191
/**

0 commit comments

Comments
 (0)