33
44namespace TheCodingMachine \TDBM ;
55
6+ use Psr \Log \NullLogger ;
67use function array_map ;
78use Doctrine \DBAL \Connection ;
89use Doctrine \DBAL \Statement ;
@@ -66,23 +67,37 @@ class ResultIterator implements Result, \ArrayAccess, \JsonSerializable
6667
6768 private $ logger ;
6869
70+ private function __construct ()
71+ {
72+ }
73+
6974 /**
7075 * @param mixed[] $parameters
7176 */
72- public function __construct (QueryFactory $ queryFactory , array $ parameters , ObjectStorageInterface $ objectStorage , ?string $ className , TDBMService $ tdbmService , MagicQuery $ magicQuery , int $ mode , LoggerInterface $ logger )
77+ public static function createResultIterator (QueryFactory $ queryFactory , array $ parameters , ObjectStorageInterface $ objectStorage , ?string $ className , TDBMService $ tdbmService , MagicQuery $ magicQuery , int $ mode , LoggerInterface $ logger ): self
7378 {
79+ $ iterator = new self ();
7480 if ($ mode !== TDBMService::MODE_CURSOR && $ mode !== TDBMService::MODE_ARRAY ) {
7581 throw new TDBMException ("Unknown fetch mode: ' " .$ mode ."' " );
7682 }
7783
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 ;
84+ $ iterator ->queryFactory = $ queryFactory ;
85+ $ iterator ->objectStorage = $ objectStorage ;
86+ $ iterator ->className = $ className ;
87+ $ iterator ->tdbmService = $ tdbmService ;
88+ $ iterator ->parameters = $ parameters ;
89+ $ iterator ->magicQuery = $ magicQuery ;
90+ $ iterator ->mode = $ mode ;
91+ $ iterator ->logger = $ logger ;
92+ return $ iterator ;
93+ }
94+
95+ public static function createEmpyIterator (): self
96+ {
97+ $ iterator = new self ();
98+ $ iterator ->totalCount = 0 ;
99+ $ iterator ->logger = new NullLogger ();
100+ return $ iterator ;
86101 }
87102
88103 protected function executeCountQuery (): void
@@ -113,6 +128,9 @@ public function count(): int
113128 */
114129 public function toArray (): array
115130 {
131+ if ($ this ->totalCount === 0 ) {
132+ return [];
133+ }
116134 return iterator_to_array ($ this ->getIterator ());
117135 }
118136
@@ -125,6 +143,9 @@ public function toArray(): array
125143 */
126144 public function map (callable $ callable ): MapIterator
127145 {
146+ if ($ this ->totalCount === 0 ) {
147+ return new MapIterator ([], $ callable );
148+ }
128149 return new MapIterator ($ this ->getIterator (), $ callable );
129150 }
130151
@@ -141,10 +162,12 @@ public function map(callable $callable): MapIterator
141162 public function getIterator ()
142163 {
143164 if ($ this ->innerResultIterator === null ) {
144- if ($ this ->mode === TDBMService::MODE_CURSOR ) {
145- $ this ->innerResultIterator = new InnerResultIterator ($ this ->queryFactory ->getMagicSql (), $ this ->parameters , null , null , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->logger );
165+ if ($ this ->totalCount === 0 ) {
166+ $ this ->innerResultIterator = InnerResultArray::createEmpyIterator ();
167+ } elseif ($ this ->mode === TDBMService::MODE_CURSOR ) {
168+ $ this ->innerResultIterator = InnerResultIterator::createInnerResultIterator ($ this ->queryFactory ->getMagicSql (), $ this ->parameters , null , null , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->logger );
146169 } else {
147- $ this ->innerResultIterator = new InnerResultArray ($ this ->queryFactory ->getMagicSql (), $ this ->parameters , null , null , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->logger );
170+ $ this ->innerResultIterator = InnerResultArray:: createInnerResultIterator ($ this ->queryFactory ->getMagicSql (), $ this ->parameters , null , null , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->logger );
148171 }
149172 }
150173
@@ -159,7 +182,10 @@ public function getIterator()
159182 */
160183 public function take ($ offset , $ limit )
161184 {
162- 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 );
185+ if ($ this ->totalCount === 0 ) {
186+ return PageIterator::createEmpyIterator ($ this );
187+ }
188+ return PageIterator::createResultIterator ($ this , $ this ->queryFactory ->getMagicSql (), $ this ->parameters , $ limit , $ offset , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->mode , $ this ->logger );
163189 }
164190
165191 /**
@@ -265,12 +291,15 @@ public function jsonSerialize($stopRecursion = false)
265291 */
266292 public function first ()
267293 {
294+ if ($ this ->totalCount === 0 ) {
295+ return null ;
296+ }
268297 $ page = $ this ->take (0 , 1 );
269298 foreach ($ page as $ bean ) {
270299 return $ bean ;
271300 }
272301
273- return ;
302+ return null ;
274303 }
275304
276305 /**
@@ -292,6 +321,9 @@ public function first()
292321 public function withOrder ($ orderBy ) : ResultIterator
293322 {
294323 $ clone = clone $ this ;
324+ if ($ this ->totalCount === 0 ) {
325+ return $ clone ;
326+ }
295327 $ clone ->queryFactory = clone $ this ->queryFactory ;
296328 $ clone ->queryFactory ->sort ($ orderBy );
297329 $ clone ->innerResultIterator = null ;
@@ -313,6 +345,9 @@ public function withOrder($orderBy) : ResultIterator
313345 public function withParameters (array $ parameters ) : ResultIterator
314346 {
315347 $ clone = clone $ this ;
348+ if ($ this ->totalCount === 0 ) {
349+ return $ clone ;
350+ }
316351 $ clone ->parameters = $ parameters ;
317352 $ clone ->innerResultIterator = null ;
318353 $ clone ->totalCount = null ;
0 commit comments