diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/src/CosmosDb.php b/src/CosmosDb.php index dada073..6a150a9 100644 --- a/src/CosmosDb.php +++ b/src/CosmosDb.php @@ -453,11 +453,13 @@ public function getDocument($rid_id, $rid_col, $rid_doc) * @param string $rid_id Resource ID * @param string $rid_col Resource Collection ID * @param string $json JSON request + * @param array $headers Optional headers to send along with the request * @return string JSON response */ - public function createDocument($rid_id, $rid_col, $json) + public function createDocument($rid_id, $rid_col, $json, array $headers = []) { - $headers = $this->getAuthHeaders('POST', 'docs', $rid_col); + $authHeaders = $this->getAuthHeaders('POST', 'docs', $rid_col); + $headers = \array_merge($headers, $authHeaders); $headers['Content-Length'] = strlen($json); return $this->request("/dbs/" . $rid_id . "/colls/" . $rid_col . "/docs", "POST", $headers, $json); } @@ -471,11 +473,13 @@ public function createDocument($rid_id, $rid_col, $json) * @param string $rid_col Resource Collection ID * @param string $rid_doc Resource Doc ID * @param string $json JSON request + * @param array $headers Optional headers to send along with the request * @return string JSON response */ - public function replaceDocument($rid_id, $rid_col, $rid_doc, $json) + public function replaceDocument($rid_id, $rid_col, $rid_doc, $json, array $headers = []) { $headers = $this->getAuthHeaders('PUT', 'docs', $rid_doc); + $headers = \array_merge($headers, $authHeaders); $headers['Content-Length'] = strlen($json); return $this->request("/dbs/" . $rid_id . "/colls/" . $rid_col . "/docs/" . $rid_doc, "PUT", $headers, $json); } @@ -488,11 +492,13 @@ public function replaceDocument($rid_id, $rid_col, $rid_doc, $json) * @param string $rid_id Resource ID * @param string $rid_col Resource Collection ID * @param string $rid_doc Resource Doc ID + * @param array $headers Optional headers to send along with the request * @return string JSON response */ - public function deleteDocument($rid_id, $rid_col, $rid_doc) + public function deleteDocument($rid_id, $rid_col, $rid_doc, array $headers = []) { $headers = $this->getAuthHeaders('DELETE', 'docs', $rid_doc); + $headers = \array_merge($headers, $authHeaders); $headers['Content-Length'] = '0'; return $this->request("/dbs/" . $rid_id . "/colls/" . $rid_col . "/docs/" . $rid_doc, "DELETE", $headers); } diff --git a/src/CosmosDbCollection.php b/src/CosmosDbCollection.php index 7b88620..35cb8d9 100644 --- a/src/CosmosDbCollection.php +++ b/src/CosmosDbCollection.php @@ -74,11 +74,12 @@ public function query($query, $params = []) * * @access public * @param string $json JSON formatted document + * @param array $headers Optional headers to send along with the request * @return string JSON strings */ - public function createDocument($json) + public function createDocument($json, array $headers = []) { - return $this->document_db->createDocument($this->rid_db, $this->rid_col, $json); + return $this->document_db->createDocument($this->rid_db, $this->rid_col, $json, $headers); } /** @@ -87,11 +88,12 @@ public function createDocument($json) * @access public * @param string $rid document ResourceID (_rid) * @param string $json JSON formatted document + * @param array $headers Optional headers to send along with the request * @return string JSON strings */ - public function replaceDocument($rid, $json) + public function replaceDocument($rid, $json, array $headers = []) { - return $this->document_db->replaceDocument($this->rid_db, $this->rid_col, $rid, $json); + return $this->document_db->replaceDocument($this->rid_db, $this->rid_col, $rid, $json, $headers); } /** @@ -99,11 +101,12 @@ public function replaceDocument($rid, $json) * * @access public * @param string $rid document ResourceID (_rid) + * @param array $headers Optional headers to send along with the request * @return string JSON strings */ - public function deleteDocument($rid) + public function deleteDocument($rid, array $headers = []) { - return $this->document_db->deleteDocument($this->rid_db, $this->rid_col, $rid); + return $this->document_db->deleteDocument($this->rid_db, $this->rid_col, $rid, $headers); } /* diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 8fdb07b..417378a 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -29,6 +29,7 @@ class QueryBuilder private $where = ""; private $order = null; private $limit = null; + private $triggers = []; private $params = []; private $response = null; @@ -211,9 +212,9 @@ public function save($document) } $col = $this->connection->selectCollection($this->collection); - $result = $rid ? - $col->replaceDocument($rid, $document) : - $col->createDocument($document); + $result = $rid ? + $col->replaceDocument($rid, $document, $this->triggersAsHeaders("replace")) : + $col->createDocument($document, $this->triggersAsHeaders("create")); $resultObj = json_decode($result); if (isset($resultObj->code) && isset($resultObj->message)) { @@ -223,6 +224,47 @@ public function save($document) return $resultObj->_rid ?? null; } + public function addTrigger(string $operation, string $type, string $id): self + { + $operation = \strtolower($operation); + if (!\in_array($operation, ["all", "create", "delete", "replace"])) + throw new \Exception("Trigger: Invalid operation \"{$operation}\""); + + $type = \strtolower($type); + if (!\in_array($type, ["post", "pre"])) + throw new \Exception("Trigger: Invalid type \"{$type}\""); + + if (!isset($this->triggers[$operation][$type])) + $this->triggers[$operation][$type] = []; + + $this->triggers[$operation][$type][] = $id; + return $this; + } + + protected function triggersAsHeaders(string $operation): array + { + $headers = []; + + // Add headers for the current operation type at $operation (create|detete!replace) + if (isset($this->triggers[$operation])) { + foreach ($this->triggers[$operation] as $name => $ids) { + $ids = \is_array($ids) ? $ids : [$ids]; + $headers["x-ms-documentdb-{$name}-trigger-include"] = \implode(",", $ids); + } + } + + // Add headers for the special "all" operations type that should always run + if (isset($this->triggers["all"])) { + foreach ($this->triggers["all"] as $name => $ids) { + $headerKey = "x-ms-documentdb-{$name}-trigger-include"; + $ids = \implode(",", \is_array($ids) ? $ids : [$ids]); + $headers[$headerKey] = isset($headers[$headerKey]) ? $headers[$headerKey] .= "," . $ids : $headers[$headerKey] = $ids; + } + } + + return $headers; + } + /* DELETE */ /** @@ -235,7 +277,7 @@ public function delete() if ($doc) { $col = $this->connection->selectCollection($this->collection); - $this->response = $col->deleteDocument($doc->_rid); + $this->response = $col->deleteDocument($doc->_rid, $this->triggersAsHeaders("delete")); } return $this; @@ -252,7 +294,7 @@ public function deleteAll() $response = []; foreach ((array)$this->findAll()->toObject() as $doc) { - $response[] = $col->deleteDocument($doc->_rid); + $response[] = $col->deleteDocument($doc->_rid, $this->triggersAsHeaders("delete")); } $this->response = $response;