Просмотр исходного кода

Add support for hasNext() in MongoCursor

Andreas Braun 9 лет назад
Родитель
Сommit
6e6854a66b
3 измененных файлов с 10 добавлено и 4 удалено
  1. 0 2
      README.md
  2. 6 2
      lib/Mongo/MongoCursor.php
  3. 4 0
      tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php

+ 0 - 2
README.md

@@ -116,8 +116,6 @@ unserializing them.
 ## MongoCursor
  - The [explain](https://php.net/manual/en/mongocursor.explain.php)
  method is not yet implemented.
- - The [hasNext](https://php.net/manual/en/mongocursor.hasnext.php)
- method is not yet implemented.
  - The [info](https://php.net/manual/en/mongocursor.info.php) method does not
  reliably fill all fields in the cursor information. This includes the `numReturned`
  and `server` keys once the cursor has started iterating. The `numReturned` field

+ 6 - 2
lib/Mongo/MongoCursor.php

@@ -221,8 +221,12 @@ class MongoCursor extends AbstractCursor implements Iterator
      */
     public function hasNext()
     {
-        $this->errorIfOpened();
-        $this->notImplemented();
+        if ($this->cursorNeedsAdvancing) {
+            $this->ensureIterator()->next();
+            $this->cursorNeedsAdvancing = false;
+        }
+
+        return $this->ensureIterator()->valid();
     }
 
     /**

+ 4 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php

@@ -67,21 +67,25 @@ class MongoCursorTest extends TestCase
         $collection = $this->getCollection();
         $cursor = $collection->find(['foo' => 'bar']);
 
+        $this->assertTrue($cursor->hasNext());
         $item = $cursor->getNext();
         $this->assertNotNull($item);
         $this->assertInstanceOf('MongoId', $item['_id']);
         $this->assertSame('bar', $item['foo']);
 
+        $this->assertTrue($cursor->hasNext());
         $item = $cursor->getNext();
         $this->assertNotNull($item);
         $this->assertInstanceOf('MongoId', $item['_id']);
         $this->assertSame('bar', $item['foo']);
 
+        $this->assertFalse($cursor->hasNext());
         $item = $cursor->getNext();
         $this->assertNull($item);
 
         $cursor->reset();
 
+        $this->assertTrue($cursor->hasNext());
         $item = $cursor->getNext();
         $this->assertNotNull($item);
         $this->assertInstanceOf('MongoId', $item['_id']);