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

Implement bson_encode and bson_decode

Andreas Braun 10 лет назад
Родитель
Сommit
f67df24eaf
3 измененных файлов с 83 добавлено и 1 удалено
  1. 2 1
      composer.json
  2. 38 0
      lib/Mongo/functions.php
  3. 43 0
      tests/Alcaeus/MongoDbAdapter/FunctionsTest.php

+ 2 - 1
composer.json

@@ -22,7 +22,8 @@
         "psr-4": {
             "": "lib/Mongo",
             "Alcaeus\\MongoDbAdapter\\": "lib/Alcaeus/MongoDbAdapter"
-        }
+        },
+        "files": [ "lib/Mongo/functions.php" ]
     },
     "autoload-dev": {
         "psr-4": { "Alcaeus\\MongoDbAdapter\\Tests\\": "tests/Alcaeus/MongoDbAdapter" }

+ 38 - 0
lib/Mongo/functions.php

@@ -0,0 +1,38 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+use Alcaeus\MongoDbAdapter\TypeConverter;
+
+/**
+ * Deserializes a BSON object into a PHP array
+ *
+ * @param string $bson The BSON to be deserialized.
+ * @return array Returns the deserialized BSON object.
+ */
+function bson_decode($bson)
+{
+    return TypeConverter::toLegacy(\MongoDB\BSON\toPHP($bson));
+}
+
+/**
+ * Serializes a PHP variable into a BSON string
+ *
+ * @param mixed $anything The variable to be serialized.
+ * @return string Returns the serialized string.
+ */
+function bson_encode($anything)
+{
+    return \MongoDB\BSON\fromPHP(TypeConverter::fromLegacy($anything));
+}

+ 43 - 0
tests/Alcaeus/MongoDbAdapter/FunctionsTest.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace Alcaeus\MongoDbAdapter\Tests;
+
+/**
+ * @author alcaeus <alcaeus@alcaeus.org>
+ */
+class FunctionsTest extends TestCase
+{
+    /**
+     * @return array Returns tupels: [$encoded, $decoded]
+     */
+    public static function data()
+    {
+        // The encoded values were retrieved by encoding data with the legacy driver and encoding them base64
+        $simpleArray = ['foo' => 'bar'];
+        $simpleArrayEncoded = "EgAAAAJmb28ABAAAAGJhcgAA";
+
+        $arrayWithObjectId = ['_id' => new \MongoId('1234567890abcdef12345678')];
+        $arrayWithObjectIdEncoded = "FgAAAAdfaWQAEjRWeJCrze8SNFZ4AA==";
+
+        return [
+            'simpleArray' => [base64_decode($simpleArrayEncoded), $simpleArray],
+            'arrayWithObjectId' => [base64_decode($arrayWithObjectIdEncoded), $arrayWithObjectId],
+        ];
+    }
+
+    /**
+     * @dataProvider data
+     */
+    public function testEncode($encoded, $decoded)
+    {
+        $this->assertEquals($encoded, bson_encode($decoded));
+    }
+
+    /**
+     * @dataProvider data
+     */
+    public function testDecode($encoded, $decoded)
+    {
+        $this->assertEquals($decoded, bson_decode($encoded));
+    }
+}