33
44namespace TheCodingMachine \TDBM ;
55
6+ use Psr \Log \NullLogger ;
67use function array_map ;
78use Doctrine \DBAL \Connection ;
89use Doctrine \DBAL \Statement ;
@@ -69,20 +70,29 @@ class ResultIterator implements Result, \ArrayAccess, \JsonSerializable
6970 /**
7071 * @param mixed[] $parameters
7172 */
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 )
7374 {
7475 if ($ mode !== TDBMService::MODE_CURSOR && $ mode !== TDBMService::MODE_ARRAY ) {
7576 throw new TDBMException ("Unknown fetch mode: ' " .$ mode ."' " );
7677 }
7778
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 );
8696 }
8797
8898 protected function executeCountQuery (): void
@@ -113,6 +123,9 @@ public function count(): int
113123 */
114124 public function toArray (): array
115125 {
126+ if ($ this ->totalCount === 0 ) {
127+ return [];
128+ }
116129 return iterator_to_array ($ this ->getIterator ());
117130 }
118131
@@ -125,6 +138,9 @@ public function toArray(): array
125138 */
126139 public function map (callable $ callable ): MapIterator
127140 {
141+ if ($ this ->totalCount === 0 ) {
142+ return new MapIterator ([], $ callable );
143+ }
128144 return new MapIterator ($ this ->getIterator (), $ callable );
129145 }
130146
@@ -141,7 +157,9 @@ public function map(callable $callable): MapIterator
141157 public function getIterator ()
142158 {
143159 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 ) {
145163 $ this ->innerResultIterator = new InnerResultIterator ($ this ->queryFactory ->getMagicSql (), $ this ->parameters , null , null , $ this ->queryFactory ->getColumnDescriptors (), $ this ->objectStorage , $ this ->className , $ this ->tdbmService , $ this ->magicQuery , $ this ->logger );
146164 } else {
147165 $ 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()
159177 */
160178 public function take ($ offset , $ limit )
161179 {
180+ if ($ this ->totalCount === 0 ) {
181+ return new PageIterator ($ this , null , [], null , null , null , null , null , null , null , null , null );
182+ }
162183 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 );
163184 }
164185
@@ -265,12 +286,15 @@ public function jsonSerialize($stopRecursion = false)
265286 */
266287 public function first ()
267288 {
289+ if ($ this ->totalCount === 0 ) {
290+ return null ;
291+ }
268292 $ page = $ this ->take (0 , 1 );
269293 foreach ($ page as $ bean ) {
270294 return $ bean ;
271295 }
272296
273- return ;
297+ return null ;
274298 }
275299
276300 /**
@@ -292,6 +316,9 @@ public function first()
292316 public function withOrder ($ orderBy ) : ResultIterator
293317 {
294318 $ clone = clone $ this ;
319+ if ($ this ->totalCount === 0 ) {
320+ return $ clone ;
321+ }
295322 $ clone ->queryFactory = clone $ this ->queryFactory ;
296323 $ clone ->queryFactory ->sort ($ orderBy );
297324 $ clone ->innerResultIterator = null ;
@@ -313,6 +340,9 @@ public function withOrder($orderBy) : ResultIterator
313340 public function withParameters (array $ parameters ) : ResultIterator
314341 {
315342 $ clone = clone $ this ;
343+ if ($ this ->totalCount === 0 ) {
344+ return $ clone ;
345+ }
316346 $ clone ->parameters = $ parameters ;
317347 $ clone ->innerResultIterator = null ;
318348 $ clone ->totalCount = null ;
0 commit comments