Quellcode durchsuchen

Improve checks for object or array

Andreas Braun vor 8 Jahren
Ursprung
Commit
ec44ecaf03
1 geänderte Dateien mit 8 neuen und 3 gelöschten Zeilen
  1. 8 3
      lib/Mongo/MongoCollection.php

+ 8 - 3
lib/Mongo/MongoCollection.php

@@ -282,9 +282,7 @@ class MongoCollection
             return;
         }
 
-        if (! count((array)$a)) {
-            throw new \MongoException('document must be an array or object');
-        }
+        $this->mustBeArrayOrObject($a);
 
         try {
             $result = $this->collection->insertOne(
@@ -1038,4 +1036,11 @@ class MongoCollection
     {
         return ['db', 'name'];
     }
+
+    private function mustBeArrayOrObject($a)
+    {
+        if (!is_array($a) && !is_object($a)) {
+            throw new \MongoException('document must be an array or object');
+        }
+    }
 }