11
11
12
12
namespace CodeIgniter \Database ;
13
13
14
+ use CodeIgniter \Database \DataConverter \DataConverter ;
14
15
use CodeIgniter \Entity \Entity ;
15
16
use stdClass ;
16
17
@@ -101,17 +102,17 @@ public function __construct(&$connID, &$resultID)
101
102
*
102
103
* @param string $type The row type. Either 'array', 'object', or a class name to use
103
104
*/
104
- public function getResult (string $ type = 'object ' ): array
105
+ public function getResult (string $ type = 'object ' , ? DataConverter $ converter = null ): array
105
106
{
106
107
if ($ type === 'array ' ) {
107
- return $ this ->getResultArray ();
108
+ return $ this ->getResultArray ($ converter );
108
109
}
109
110
110
111
if ($ type === 'object ' ) {
111
- return $ this ->getResultObject ();
112
+ return $ this ->getResultObject ($ converter );
112
113
}
113
114
114
- return $ this ->getCustomResultObject ($ type );
115
+ return $ this ->getCustomResultObject ($ type, $ converter );
115
116
}
116
117
117
118
/**
@@ -121,7 +122,7 @@ public function getResult(string $type = 'object'): array
121
122
*
122
123
* @return array
123
124
*/
124
- public function getCustomResultObject (string $ className )
125
+ public function getCustomResultObject (string $ className, ? DataConverter $ converter = null )
125
126
{
126
127
if (isset ($ this ->customResultObject [$ className ])) {
127
128
return $ this ->customResultObject [$ className ];
@@ -156,7 +157,7 @@ public function getCustomResultObject(string $className)
156
157
}
157
158
$ this ->customResultObject [$ className ] = [];
158
159
159
- while ($ row = $ this ->fetchObject ($ className )) {
160
+ while ($ row = $ this ->fetchObject ($ className, $ converter )) {
160
161
if (! is_subclass_of ($ row , Entity::class) && method_exists ($ row , 'syncOriginal ' )) {
161
162
$ row ->syncOriginal ();
162
163
}
@@ -172,7 +173,7 @@ public function getCustomResultObject(string $className)
172
173
*
173
174
* If no results, an empty array is returned.
174
175
*/
175
- public function getResultArray (): array
176
+ public function getResultArray (? DataConverter $ converter = null ): array
176
177
{
177
178
if (! empty ($ this ->resultArray )) {
178
179
return $ this ->resultArray ;
@@ -198,6 +199,10 @@ public function getResultArray(): array
198
199
}
199
200
200
201
while ($ row = $ this ->fetchAssoc ()) {
202
+ if ($ converter !== null ) {
203
+ $ row = $ converter ->fromDatabase ($ row );
204
+ }
205
+
201
206
$ this ->resultArray [] = $ row ;
202
207
}
203
208
@@ -209,10 +214,9 @@ public function getResultArray(): array
209
214
*
210
215
* If no results, an empty array is returned.
211
216
*
212
- * @return array<int, stdClass>
213
- * @phpstan-return list<stdClass>
217
+ * @return list<stdClass>
214
218
*/
215
- public function getResultObject (): array
219
+ public function getResultObject (? DataConverter $ converter = null ): array
216
220
{
217
221
if (! empty ($ this ->resultObject )) {
218
222
return $ this ->resultObject ;
@@ -237,7 +241,7 @@ public function getResultObject(): array
237
241
$ this ->dataSeek ();
238
242
}
239
243
240
- while ($ row = $ this ->fetchObject ()) {
244
+ while ($ row = $ this ->fetchObject (' stdClass ' , $ converter )) {
241
245
if (! is_subclass_of ($ row , Entity::class) && method_exists ($ row , 'syncOriginal ' )) {
242
246
$ row ->syncOriginal ();
243
247
}
@@ -260,12 +264,12 @@ public function getResultObject(): array
260
264
* @return array|object|stdClass|null
261
265
* @phpstan-return ($type is 'object' ? stdClass|null : ($type is 'array' ? array|null : object|null))
262
266
*/
263
- public function getRow ($ n = 0 , string $ type = 'object ' )
267
+ public function getRow ($ n = 0 , string $ type = 'object ' , ? DataConverter $ converter = null )
264
268
{
265
269
if (! is_numeric ($ n )) {
266
270
// We cache the row data for subsequent uses
267
271
if (! is_array ($ this ->rowData )) {
268
- $ this ->rowData = $ this ->getRowArray ();
272
+ $ this ->rowData = $ this ->getRowArray (0 , $ converter );
269
273
}
270
274
271
275
// array_key_exists() instead of isset() to allow for NULL values
@@ -277,14 +281,14 @@ public function getRow($n = 0, string $type = 'object')
277
281
}
278
282
279
283
if ($ type === 'object ' ) {
280
- return $ this ->getRowObject ($ n );
284
+ return $ this ->getRowObject ($ n, $ converter );
281
285
}
282
286
283
287
if ($ type === 'array ' ) {
284
- return $ this ->getRowArray ($ n );
288
+ return $ this ->getRowArray ($ n, $ converter );
285
289
}
286
290
287
- return $ this ->getCustomRowObject ($ n , $ type );
291
+ return $ this ->getCustomRowObject ($ n , $ type, $ converter );
288
292
}
289
293
290
294
/**
@@ -294,10 +298,10 @@ public function getRow($n = 0, string $type = 'object')
294
298
*
295
299
* @return array|null
296
300
*/
297
- public function getCustomRowObject (int $ n , string $ className )
301
+ public function getCustomRowObject (int $ n , string $ className, ? DataConverter $ converter = null )
298
302
{
299
303
if (! isset ($ this ->customResultObject [$ className ])) {
300
- $ this ->getCustomResultObject ($ className );
304
+ $ this ->getCustomResultObject ($ className, $ converter );
301
305
}
302
306
303
307
if (empty ($ this ->customResultObject [$ className ])) {
@@ -318,9 +322,9 @@ public function getCustomRowObject(int $n, string $className)
318
322
*
319
323
* @return array|null
320
324
*/
321
- public function getRowArray (int $ n = 0 )
325
+ public function getRowArray (int $ n = 0 , ? DataConverter $ converter = null )
322
326
{
323
- $ result = $ this ->getResultArray ();
327
+ $ result = $ this ->getResultArray ($ converter );
324
328
if (empty ($ result )) {
325
329
return null ;
326
330
}
@@ -339,9 +343,10 @@ public function getRowArray(int $n = 0)
339
343
*
340
344
* @return object|stdClass|null
341
345
*/
342
- public function getRowObject (int $ n = 0 )
346
+ public function getRowObject (int $ n = 0 , ? DataConverter $ converter = null )
343
347
{
344
- $ result = $ this ->getResultObject ();
348
+ $ result = $ this ->getResultObject ($ converter );
349
+
345
350
if (empty ($ result )) {
346
351
return null ;
347
352
}
@@ -529,5 +534,5 @@ abstract protected function fetchAssoc();
529
534
*
530
535
* @return Entity|false|object|stdClass
531
536
*/
532
- abstract protected function fetchObject (string $ className = 'stdClass ' );
537
+ abstract protected function fetchObject (string $ className = 'stdClass ' , ? DataConverter $ converter = null );
533
538
}
0 commit comments