瀏覽代碼

Implement MongoGridFSCursor

Olivier Lechevalier 10 年之前
父節點
當前提交
95de3c7882

+ 22 - 5
lib/Mongo/MongoGridFSCursor.php

@@ -13,7 +13,8 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-class MongoGridFSCursor extends MongoCursor implements Traversable, Iterator {
+class MongoGridFSCursor extends MongoCursor implements Traversable, Iterator
+{
     /**
      * @static
      * @var $slaveOkay
@@ -36,27 +37,43 @@ class MongoGridFSCursor extends MongoCursor implements Traversable, Iterator {
      * @param array $fields Fields to return
      * @return MongoGridFSCursor Returns the new cursor
      */
-    public function __construct($gridfs, $connection, $ns, $query, $fields) {}
+    public function __construct(MongoGridFS $gridfs, $connection, $ns, array $query = array(), array $fields = array())
+    {
+        $this->gridfs = $gridfs;
+        parent::__construct($connection, $ns, $query, $fields);
+    }
 
     /**
      * Return the next file to which this cursor points, and advance the cursor
      * @link http://php.net/manual/en/mongogridfscursor.getnext.php
      * @return MongoGridFSFile Returns the next file
      */
-    public function getNext() {}
+    public function getNext()
+    {
+        $file = parent::next();
+        return new MongoGridFSFile($this->gridfs, $file);
+    }
 
     /**
      * Returns the current file
      * @link http://php.net/manual/en/mongogridfscursor.current.php
      * @return MongoGridFSFile The current file
      */
-    public function current() {}
+    public function current()
+    {
+        $file = parent::current();
+        return new MongoGridFSFile($this->gridfs, $file);
+    }
 
     /**
      * Returns the current result's filename
      * @link http://php.net/manual/en/mongogridfscursor.key.php
      * @return string The current results filename
      */
-    public function key() {}
+    public function key()
+    {
+        $file = parent::current();
+        return isset($file['filename']) ? $file['filename'] : null;
+    }
 
 }

+ 1 - 1
lib/Mongo/MongoGridFSFile.php

@@ -110,7 +110,7 @@ class MongoGridFSFile
     {
         $handle = tmpfile();
         $this->writeFromRessource($handle);
-        fseek($handle, 0);
+        rewind($handle);
         return $handle;
     }
 

+ 46 - 0
tests/Alcaeus/MongoDbAdapter/MongoGridFSCursorTest.php

@@ -0,0 +1,46 @@
+<?php
+
+namespace Alcaeus\MongoDbAdapter\Tests;
+
+class MongoGridFSCursorTest extends TestCase
+{
+    public function testCursor()
+    {
+        $cursor = $this->getCursor();
+        $array = iterator_to_array($cursor);
+
+
+        $this->assertCount(2, $array);
+        $this->assertArrayHasKey('One.txt', $array);
+        $this->assertArrayHasKey('Two.txt', $array);
+        $firstFile = $array['One.txt'];
+        $this->assertInstanceOf('MongoGridFSFile', $firstFile);
+        $this->assertArraySubset(['length' => 3, 'filename' => 'One.txt'], $firstFile->file);
+        $secondFile = $array['Two.txt'];
+        $this->assertInstanceOf('MongoGridFSFile', $secondFile);
+        $this->assertArraySubset(['length' => 3, 'filename' => 'Two.txt'], $secondFile->file);
+    }
+
+    private function getCursor()
+    {
+        $gridFS = $this->getGridFS();
+        $gridFS->storeBytes('One', ['filename' => 'One.txt']);
+        $gridFS->storeBytes('Two', ['filename' => 'Two.txt']);
+
+        return $gridFS->find();
+    }
+
+    /**
+     * @param string $name
+     * @param \MongoDB|null $database
+     * @return \MongoGridFS
+     */
+    private function getGridFS($name = 'testfs', \MongoDB $database = null)
+    {
+        if ($database === null) {
+            $database = $this->getDatabase();
+        }
+
+        return new \MongoGridFS($database, $name);
+    }
+}