Ejemplo n.º 1
0
 public function testBatchGetNotFound()
 {
     $obj = array("name" => "alice");
     $resp = LeanClient::post("/classes/TestObject", $obj);
     $this->assertNotEmpty($resp["objectId"]);
     $req[] = array("path" => "/1.1/classes/TestObject/{$resp['objectId']}", "method" => "GET");
     $req[] = array("path" => "/1.1/classes/TestObject/nonexistent_id", "method" => "GET");
     $resp2 = LeanClient::batch($req);
     $this->assertNotEmpty($resp2[0]["success"]);
     $this->assertEmpty($resp2[1]["success"]);
     // empty when not found
     LeanClient::delete("/classes/TestObject/{$resp['objectId']}");
 }
Ejemplo n.º 2
0
 /**
  * Delete objects in batch.
  *
  * @param array $objects Array of LeanObjects to destroy
  * @return bool
  */
 public static function destroyAll($objects)
 {
     $batch = array();
     foreach ($objects as $obj) {
         if (!$obj->getObjectId()) {
             throw new \ErrorException("Cannot destroy object without ID");
         }
         // Remove duplicate objects by ID
         $batch[$obj->getObjectId()] = $obj;
     }
     if (empty($batch)) {
         return;
     }
     $requests = array();
     $objects = array();
     foreach ($batch as $obj) {
         $requests[] = array("path" => "/1.1/classes/{$obj->getClassName()}" . "/{$obj->getObjectId()}", "method" => "DELETE");
         $objects[] = $obj;
     }
     $response = LeanClient::batch($requests);
     $errors = array();
     foreach ($objects as $i => $obj) {
         if (isset($response[$i]["error"])) {
             $errors[] = array("request" => $requests[$i], "error" => $response[$i]["error"]);
         }
     }
     if (count($errors) > 0) {
         throw new \LeanException("Batch requests error: " . json_encode($errors));
     }
 }