Skip to content

Commit

Permalink
Merge pull request #14 from purplekrayons/master
Browse files Browse the repository at this point in the history
support nested property keys
  • Loading branch information
jupitern authored May 23, 2019
2 parents 48b6c5f + 832fe77 commit c12e02d
Showing 1 changed file with 59 additions and 10 deletions.
69 changes: 59 additions & 10 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,55 @@ public function getPartitionKey()
return $this->partitionKey;
}

/**
* @param string partition key
* @param object document
* @param bool if true, return property structure as string
* @return string partition value
*/
public function getPartitionValue(object $document, bool $toString = false)
{
# strip any slashes from the beginning
# and end of the partition key
$partitionKey = trim($this->partitionKey, '/');

# if the partition key contains slashes, the user
# is referencing a nested value, so we should search for it
if (strpos($partitionKey, '/') !== false) {

# explode the key into its parts
$properties = explode("/", $partitionKey);

# return the property structure as a
# string for use in cosmos queries
if ($toString) {
# build the property string
$docString = "";
foreach( $properties as $p ) {
$docString .= ".{$p}";
}

# return the property string
return $docString;
}
# otherwise, fetch the property key value
else {

# iterate through the document to find the nested value
foreach( $properties as $p ) {
$document = (object)$document->{$p};
}

# return the partition value
return $document->scalar;
}

} else {
# return as if key were in root of the document
return $document->{$partitionKey};
}
}

/**
* @param $document
* @return string|null
Expand All @@ -282,7 +331,7 @@ public function save($document)
$document = (object)$document;

$rid = is_object($document) && isset($document->_rid) ? $document->_rid : null;
$partitionValue = $this->partitionKey != null ? $document->{$this->partitionKey} : null;
$partitionValue = $this->partitionKey != null ? $this->getPartitionValue($document) : null;
$document = json_encode($document);

$result = $rid ?
Expand All @@ -308,13 +357,13 @@ public function delete($isCrossPartition = false)
$this->response = null;

$select = $this->fields != "" ?
$this->fields : "c._rid" . ($this->partitionKey != null ? ", c.{$this->partitionKey}" : "");
$this->fields : "c._rid" . ($this->partitionKey != null ? ", c.{$this->getPartitionValue($document, true)}" : "");

$doc = $this->select($select)->find($isCrossPartition)->toObject();
$document = $this->select($select)->find($isCrossPartition)->toObject();

if ($doc) {
$partitionValue = $this->partitionKey != null ? $doc->{$this->partitionKey} : null;
$this->response = $this->collection->deleteDocument($doc->_rid, $partitionValue, $this->triggersAsHeaders("delete"));
if ($document) {
$partitionValue = $this->partitionKey != null ? $this->getPartitionValue($document) : null;
$this->response = $this->collection->deleteDocument($document->_rid, $partitionValue, $this->triggersAsHeaders("delete"));

return true;
}
Expand All @@ -332,12 +381,12 @@ public function deleteAll($isCrossPartition = false)
$this->response = null;

$select = $this->fields != "" ?
$this->fields : "c._rid" . ($this->partitionKey != null ? ", c.{$this->partitionKey}" : "");
$this->fields : "c._rid" . ($this->partitionKey != null ? ", c.{$this->getPartitionValue($document, true)}" : "");

$response = [];
foreach ((array)$this->select($select)->findAll($isCrossPartition)->toObject() as $doc) {
$partitionValue = $this->partitionKey != null ? $doc->{$this->partitionKey} : null;
$response[] = $this->collection->deleteDocument($doc->_rid, $partitionValue, $this->triggersAsHeaders("delete"));
foreach ((array)$this->select($select)->findAll($isCrossPartition)->toObject() as $document) {
$partitionValue = $this->partitionKey != null ? $this->getPartitionValue($document) : null;
$response[] = $this->collection->deleteDocument($document->_rid, $partitionValue, $this->triggersAsHeaders("delete"));
}

$this->response = $response;
Expand Down

0 comments on commit c12e02d

Please sign in to comment.