13
13
14
14
namespace CodeIgniter \Validation ;
15
15
16
+ use CodeIgniter \Database \BaseBuilder ;
16
17
use CodeIgniter \Exceptions \InvalidArgumentException ;
17
18
use CodeIgniter \Helpers \Array \ArrayHelper ;
18
19
use Config \Database ;
@@ -118,40 +119,17 @@ public function greater_than_equal_to($str, string $min): bool
118
119
*/
119
120
public function is_not_unique ($ str , string $ field , array $ data ): bool
120
121
{
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 );
145
123
146
124
if (
147
125
$ whereField !== null && $ whereField !== ''
148
126
&& $ whereValue !== null && $ whereValue !== ''
149
127
&& ! preg_match ('/^\{(\w+)\}$/ ' , $ whereValue )
150
128
) {
151
- $ row = $ row ->where ($ whereField , $ whereValue );
129
+ $ builder = $ builder ->where ($ whereField , $ whereValue );
152
130
}
153
131
154
- return $ row ->get ()->getRow () !== null ;
132
+ return $ builder ->get ()->getRow () !== null ;
155
133
}
156
134
157
135
/**
@@ -184,14 +162,38 @@ public function in_list($value, string $list): bool
184
162
*/
185
163
public function is_unique ($ str , string $ field , array $ data ): bool
186
164
{
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 ;
189
191
}
190
192
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 );
193
195
194
- // Break the dbGroup, table and field apart from the field string
196
+ // Parse the field string to extract dbGroup, table, and field.
195
197
$ parts = explode ('. ' , $ field , 3 );
196
198
$ numParts = count ($ parts );
197
199
@@ -203,21 +205,15 @@ public function is_unique($str, string $field, array $data): bool
203
205
throw new InvalidArgumentException ('The field must be in the format "table.field" or "dbGroup.table.field". ' );
204
206
}
205
207
206
- $ row = Database::connect ($ dbGroup ?? $ data ['DBGroup ' ] ?? null )
208
+ // Connect to the database.
209
+ $ dbGroup ??= $ data ['DBGroup ' ] ?? null ;
210
+ $ builder = Database::connect ($ dbGroup )
207
211
->table ($ table )
208
212
->select ('1 ' )
209
- ->where ($ field , $ str )
213
+ ->where ($ field , $ value )
210
214
->limit (1 );
211
215
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 ];
221
217
}
222
218
223
219
/**
0 commit comments