3
3
4
4
namespace TheCodingMachine \TDBM ;
5
5
6
+ use Psr \Log \NullLogger ;
6
7
use function array_map ;
7
8
use Doctrine \DBAL \Connection ;
8
9
use Doctrine \DBAL \Statement ;
@@ -69,20 +70,29 @@ class ResultIterator implements Result, \ArrayAccess, \JsonSerializable
69
70
/**
70
71
* @param mixed[] $parameters
71
72
*/
72
- public function __construct (QueryFactory $ queryFactory , array $ parameters , ObjectStorageInterface $ objectStorage , ?string $ className , TDBMService $ tdbmService , MagicQuery $ magicQuery , int $ mode , LoggerInterface $ logger )
73
+ public function __construct (? QueryFactory $ queryFactory , ? array $ parameters , ? ObjectStorageInterface $ objectStorage , ?string $ className , ? TDBMService $ tdbmService , ? MagicQuery $ magicQuery , int $ mode , ? LoggerInterface $ logger )
73
74
{
74
75
if ($ mode !== TDBMService::MODE_CURSOR && $ mode !== TDBMService::MODE_ARRAY ) {
75
76
throw new TDBMException ("Unknown fetch mode: ' " .$ mode ."' " );
76
77
}
77
78
78
- $ this ->queryFactory = $ queryFactory ;
79
- $ this ->objectStorage = $ objectStorage ;
80
- $ this ->className = $ className ;
81
- $ this ->tdbmService = $ tdbmService ;
82
- $ this ->parameters = $ parameters ;
83
- $ this ->magicQuery = $ magicQuery ;
84
- $ this ->mode = $ mode ;
85
- $ this ->logger = $ logger ;
79
+ if (!$ queryFactory ) {
80
+ $ this ->totalCount = 0 ;
81
+ } else {
82
+ $ this ->queryFactory = $ queryFactory ;
83
+ $ this ->objectStorage = $ objectStorage ;
84
+ $ this ->className = $ className ;
85
+ $ this ->tdbmService = $ tdbmService ;
86
+ $ this ->parameters = $ parameters ;
87
+ $ this ->magicQuery = $ magicQuery ;
88
+ $ this ->mode = $ mode ;
89
+ }
90
+ $ this ->logger = $ logger ?? new NullLogger ();
91
+ }
92
+
93
+ public static function createEmpyIterator (): self
94
+ {
95
+ return new self (null , null , null , null , null , null , TDBMService::MODE_ARRAY , null );
86
96
}
87
97
88
98
protected function executeCountQuery (): void
@@ -113,6 +123,9 @@ public function count(): int
113
123
*/
114
124
public function toArray (): array
115
125
{
126
+ if ($ this ->totalCount === 0 ) {
127
+ return [];
128
+ }
116
129
return iterator_to_array ($ this ->getIterator ());
117
130
}
118
131
@@ -125,6 +138,9 @@ public function toArray(): array
125
138
*/
126
139
public function map (callable $ callable ): MapIterator
127
140
{
141
+ if ($ this ->totalCount === 0 ) {
142
+ return new MapIterator ([], $ callable );
143
+ }
128
144
return new MapIterator ($ this ->getIterator (), $ callable );
129
145
}
130
146
@@ -141,7 +157,9 @@ public function map(callable $callable): MapIterator
141
157
public function getIterator ()
142
158
{
143
159
if ($ this ->innerResultIterator === null ) {
144
- if ($ this ->mode === TDBMService::MODE_CURSOR ) {
160
+ if ($ this ->totalCount === 0 ) {
161
+ $ this ->innerResultIterator = new InnerResultArray (null , null , null , null , null , null , null , null , null , null );
162
+ } elseif ($ this ->mode === TDBMService::MODE_CURSOR ) {
145
163
$ this ->innerResultIterator = new InnerResultIterator ($ this ->queryFactory ->getMagicSql (), $ this ->parameters , null , null , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->logger );
146
164
} else {
147
165
$ this ->innerResultIterator = new InnerResultArray ($ this ->queryFactory ->getMagicSql (), $ this ->parameters , null , null , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->logger );
@@ -159,6 +177,9 @@ public function getIterator()
159
177
*/
160
178
public function take ($ offset , $ limit )
161
179
{
180
+ if ($ this ->totalCount === 0 ) {
181
+ return new PageIterator ($ this , null , [], null , null , null , null , null , null , null , null , null );
182
+ }
162
183
return new PageIterator ($ this , $ this ->queryFactory ->getMagicSql (), $ this ->parameters , $ limit , $ offset , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->mode , $ this ->logger );
163
184
}
164
185
@@ -265,12 +286,15 @@ public function jsonSerialize($stopRecursion = false)
265
286
*/
266
287
public function first ()
267
288
{
289
+ if ($ this ->totalCount === 0 ) {
290
+ return null ;
291
+ }
268
292
$ page = $ this ->take (0 , 1 );
269
293
foreach ($ page as $ bean ) {
270
294
return $ bean ;
271
295
}
272
296
273
- return ;
297
+ return null ;
274
298
}
275
299
276
300
/**
@@ -292,6 +316,9 @@ public function first()
292
316
public function withOrder ($ orderBy ) : ResultIterator
293
317
{
294
318
$ clone = clone $ this ;
319
+ if ($ this ->totalCount === 0 ) {
320
+ return $ clone ;
321
+ }
295
322
$ clone ->queryFactory = clone $ this ->queryFactory ;
296
323
$ clone ->queryFactory ->sort ($ orderBy );
297
324
$ clone ->innerResultIterator = null ;
@@ -313,6 +340,9 @@ public function withOrder($orderBy) : ResultIterator
313
340
public function withParameters (array $ parameters ) : ResultIterator
314
341
{
315
342
$ clone = clone $ this ;
343
+ if ($ this ->totalCount === 0 ) {
344
+ return $ clone ;
345
+ }
316
346
$ clone ->parameters = $ parameters ;
317
347
$ clone ->innerResultIterator = null ;
318
348
$ clone ->totalCount = null ;
0 commit comments