1111
1212namespace CodeIgniter \Database ;
1313
14+ use CodeIgniter \Database \DataConverter \DataConverter ;
1415use CodeIgniter \Entity \Entity ;
1516use stdClass ;
1617
@@ -101,17 +102,17 @@ public function __construct(&$connID, &$resultID)
101102 *
102103 * @param string $type The row type. Either 'array', 'object', or a class name to use
103104 */
104- public function getResult (string $ type = 'object ' ): array
105+ public function getResult (string $ type = 'object ' , ? DataConverter $ converter = null ): array
105106 {
106107 if ($ type === 'array ' ) {
107- return $ this ->getResultArray ();
108+ return $ this ->getResultArray ($ converter );
108109 }
109110
110111 if ($ type === 'object ' ) {
111- return $ this ->getResultObject ();
112+ return $ this ->getResultObject ($ converter );
112113 }
113114
114- return $ this ->getCustomResultObject ($ type );
115+ return $ this ->getCustomResultObject ($ type, $ converter );
115116 }
116117
117118 /**
@@ -121,7 +122,7 @@ public function getResult(string $type = 'object'): array
121122 *
122123 * @return array
123124 */
124- public function getCustomResultObject (string $ className )
125+ public function getCustomResultObject (string $ className, ? DataConverter $ converter = null )
125126 {
126127 if (isset ($ this ->customResultObject [$ className ])) {
127128 return $ this ->customResultObject [$ className ];
@@ -156,7 +157,7 @@ public function getCustomResultObject(string $className)
156157 }
157158 $ this ->customResultObject [$ className ] = [];
158159
159- while ($ row = $ this ->fetchObject ($ className )) {
160+ while ($ row = $ this ->fetchObject ($ className, $ converter )) {
160161 if (! is_subclass_of ($ row , Entity::class) && method_exists ($ row , 'syncOriginal ' )) {
161162 $ row ->syncOriginal ();
162163 }
@@ -172,7 +173,7 @@ public function getCustomResultObject(string $className)
172173 *
173174 * If no results, an empty array is returned.
174175 */
175- public function getResultArray (): array
176+ public function getResultArray (? DataConverter $ converter = null ): array
176177 {
177178 if (! empty ($ this ->resultArray )) {
178179 return $ this ->resultArray ;
@@ -198,6 +199,10 @@ public function getResultArray(): array
198199 }
199200
200201 while ($ row = $ this ->fetchAssoc ()) {
202+ if ($ converter !== null ) {
203+ $ row = $ converter ->fromDatabase ($ row );
204+ }
205+
201206 $ this ->resultArray [] = $ row ;
202207 }
203208
@@ -209,10 +214,9 @@ public function getResultArray(): array
209214 *
210215 * If no results, an empty array is returned.
211216 *
212- * @return array<int, stdClass>
213- * @phpstan-return list<stdClass>
217+ * @return list<stdClass>
214218 */
215- public function getResultObject (): array
219+ public function getResultObject (? DataConverter $ converter = null ): array
216220 {
217221 if (! empty ($ this ->resultObject )) {
218222 return $ this ->resultObject ;
@@ -237,7 +241,7 @@ public function getResultObject(): array
237241 $ this ->dataSeek ();
238242 }
239243
240- while ($ row = $ this ->fetchObject ()) {
244+ while ($ row = $ this ->fetchObject (' stdClass ' , $ converter )) {
241245 if (! is_subclass_of ($ row , Entity::class) && method_exists ($ row , 'syncOriginal ' )) {
242246 $ row ->syncOriginal ();
243247 }
@@ -260,12 +264,12 @@ public function getResultObject(): array
260264 * @return array|object|stdClass|null
261265 * @phpstan-return ($type is 'object' ? stdClass|null : ($type is 'array' ? array|null : object|null))
262266 */
263- public function getRow ($ n = 0 , string $ type = 'object ' )
267+ public function getRow ($ n = 0 , string $ type = 'object ' , ? DataConverter $ converter = null )
264268 {
265269 if (! is_numeric ($ n )) {
266270 // We cache the row data for subsequent uses
267271 if (! is_array ($ this ->rowData )) {
268- $ this ->rowData = $ this ->getRowArray ();
272+ $ this ->rowData = $ this ->getRowArray (0 , $ converter );
269273 }
270274
271275 // array_key_exists() instead of isset() to allow for NULL values
@@ -277,14 +281,14 @@ public function getRow($n = 0, string $type = 'object')
277281 }
278282
279283 if ($ type === 'object ' ) {
280- return $ this ->getRowObject ($ n );
284+ return $ this ->getRowObject ($ n, $ converter );
281285 }
282286
283287 if ($ type === 'array ' ) {
284- return $ this ->getRowArray ($ n );
288+ return $ this ->getRowArray ($ n, $ converter );
285289 }
286290
287- return $ this ->getCustomRowObject ($ n , $ type );
291+ return $ this ->getCustomRowObject ($ n , $ type, $ converter );
288292 }
289293
290294 /**
@@ -294,10 +298,10 @@ public function getRow($n = 0, string $type = 'object')
294298 *
295299 * @return array|null
296300 */
297- public function getCustomRowObject (int $ n , string $ className )
301+ public function getCustomRowObject (int $ n , string $ className, ? DataConverter $ converter = null )
298302 {
299303 if (! isset ($ this ->customResultObject [$ className ])) {
300- $ this ->getCustomResultObject ($ className );
304+ $ this ->getCustomResultObject ($ className, $ converter );
301305 }
302306
303307 if (empty ($ this ->customResultObject [$ className ])) {
@@ -318,9 +322,9 @@ public function getCustomRowObject(int $n, string $className)
318322 *
319323 * @return array|null
320324 */
321- public function getRowArray (int $ n = 0 )
325+ public function getRowArray (int $ n = 0 , ? DataConverter $ converter = null )
322326 {
323- $ result = $ this ->getResultArray ();
327+ $ result = $ this ->getResultArray ($ converter );
324328 if (empty ($ result )) {
325329 return null ;
326330 }
@@ -339,9 +343,10 @@ public function getRowArray(int $n = 0)
339343 *
340344 * @return object|stdClass|null
341345 */
342- public function getRowObject (int $ n = 0 )
346+ public function getRowObject (int $ n = 0 , ? DataConverter $ converter = null )
343347 {
344- $ result = $ this ->getResultObject ();
348+ $ result = $ this ->getResultObject ($ converter );
349+
345350 if (empty ($ result )) {
346351 return null ;
347352 }
@@ -529,5 +534,5 @@ abstract protected function fetchAssoc();
529534 *
530535 * @return Entity|false|object|stdClass
531536 */
532- abstract protected function fetchObject (string $ className = 'stdClass ' );
537+ abstract protected function fetchObject (string $ className = 'stdClass ' , ? DataConverter $ converter = null );
533538}
0 commit comments