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

Improve checks for object or array

Andreas Braun 8 лет назад
Родитель
Сommit
ec44ecaf03
1 измененных файлов с 8 добавлено и 3 удалено
  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');
+        }
+    }
 }