Skip to content
This repository was archived by the owner on Feb 5, 2024. It is now read-only.

Persist and remove methods for Manager #155

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions src/Doctrine/ODM/OrientDB/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function findRecords(Array $rids, $fetchPlan = null, $lazy = true)
*/
public function flush()
{
throw new \Exception;
return;
}

/**
Expand Down Expand Up @@ -238,7 +238,16 @@ public function merge($object)
*/
public function persist($object)
{
throw new \Exception();
$json = $this->toJson($object);
if($this->checkExists($object) === false){
$response = $this->getBinding()->postDocument($json);
$rid = $response->getData();
$object->setRid($rid);
} else {
$response = $this->getBinding()->putDocument($object->getRid(), $json);
$result = $response->getData();
}
return $object;
}

/**
Expand All @@ -248,7 +257,13 @@ public function persist($object)
*/
public function remove($object)
{
throw new \Exception();
if($this->checkExists($object) === false){
return false;
}
$response = $this->getBinding()->deleteDocument($object->getRid());
$result = $response->getData();
$object->setRid(null);
return $result;
}

/**
Expand Down Expand Up @@ -366,4 +381,39 @@ protected function getBinding()
{
return $this->binding;
}

protected function toJson($object)
{
$data = (array) $object;
foreach ($data as $key => $value) {
$newKey = preg_replace('/[^a-z]/i', null, $key);
$data[$newKey] = $value;
unset($data[$key]);
if ( $value instanceof \Doctrine\ODM\OrientDB\Proxy\Value ) {
$object = $value->__invoke();
$data[$newKey] = $object->getRid();
}elseif ( $value instanceof \DateTime ) {
$data[$newKey] = $value->format('Y-m-d H:i:s');
}
}
$data['@class'] = join('', array_slice(explode('\\', get_class($object)), -1));
if(array_key_exists('rid', $data)){
unset($data['rid']);
}
if(array_key_exists('version', $data)){
$data['@version'] = $data['version'];
unset($data['version']);
}
return json_encode($data);
}

protected function checkExists($object)
{
$rid = $object->getRid();
if(empty($rid)){
return false;
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ public function getData()
if (!$this->isValid()) {
throw new InvalidQueryException($this->response->getBody(), $this);
}
$body = $this->response->getBody();

if (false === $json = json_decode($this->response->getBody())) {
if (null === $json = json_decode($body)) {
if ($this->isValidRid($body)) {
return $body;
} elseif ($body === "") {
return true;
}
throw new \RuntimeException("Invalid JSON payload");
}

Expand Down Expand Up @@ -77,4 +83,12 @@ public function getInnerResponse()
{
return $this->response;
}

/**
* {@inheritdoc}
*/
protected function isValidRid($body)
{
return preg_match('/#\d+:\d+/', $body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function getValidStatusCodes()
self::STATUS_NO_CONTENT,
self::STATUS_RESET_CONTENT,
self::STATUS_PARTIAL_CONTENT,
self::STATUS_CREATED
);
}

Expand Down