1313
1414namespace CodeIgniter \Validation ;
1515
16+ use CodeIgniter \Database \BaseBuilder ;
1617use CodeIgniter \Exceptions \InvalidArgumentException ;
1718use CodeIgniter \Helpers \Array \ArrayHelper ;
1819use 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 /**
0 commit comments