Browse Source

Read code and scope from Javascript objects

Andreas Braun 9 years ago
parent
commit
beb7dceb0f
3 changed files with 7 additions and 10 deletions
  1. 0 6
      README.md
  2. 5 2
      lib/Mongo/MongoCode.php
  3. 2 2
      tests/Alcaeus/MongoDbAdapter/Mongo/MongoCodeTest.php

+ 0 - 6
README.md

@@ -131,9 +131,3 @@ unserializing them.
  fields always return 0 for compatibility to MongoCursor. The `firstBatchAt` and
  `firstBatchNumReturned` fields will contain the same value, which is the internal
  position of the iterator.
-
-## Types
-
- - Return values containing objects of the [MongoDB\BSON\Javascript](https://secure.php.net/manual/en/class.mongodb-bson-javascript.php)
- class cannot be converted to full [MongoCode](https://secure.php.net/manual/en/class.mongocode.php)
- objects because there are no accessors for the code and scope properties.

+ 5 - 2
lib/Mongo/MongoCode.php

@@ -13,6 +13,8 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+use Alcaeus\MongoDbAdapter\TypeConverter;
+
 if (class_exists('MongoCode', false)) {
     return;
 }
@@ -37,8 +39,9 @@ class MongoCode implements \Alcaeus\MongoDbAdapter\TypeInterface
     public function __construct($code, array $scope = [])
     {
         if ($code instanceof \MongoDB\BSON\Javascript) {
-            // @todo Use properties from object once they are accessible
-            $code = '';
+            $javascript = $code;
+            $code = $javascript->getCode();
+            $scope = TypeConverter::toLegacy($javascript->getScope());
         }
 
         $this->code = $code;

+ 2 - 2
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCodeTest.php

@@ -39,7 +39,7 @@ class MongoCodeTest extends TestCase
         $bsonCode = new \MongoDB\BSON\Javascript('code', ['scope' => 'bleh']);
         $code = new \MongoCode($bsonCode);
 
-        $this->assertAttributeSame('', 'code', $code);
-        $this->assertAttributeSame([], 'scope', $code);
+        $this->assertAttributeSame('code', 'code', $code);
+        $this->assertAttributeSame(['scope' => 'bleh'], 'scope', $code);
     }
 }