Skip to content

Commit

Permalink
Upgraded client to support Neo REST API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
electric-al committed Jun 3, 2010
1 parent 5f8c076 commit 274ee8f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 23 deletions.
9 changes: 9 additions & 0 deletions demo.php
Expand Up @@ -23,8 +23,13 @@
* Assign some attributes to the nodes and save the,
*/
$firstNode->message = "Hello, ";
$firstNode->blah = "blah blah";
$firstNode->save();

$firstNode->blah = NULL; // Setting to null removes the property
$firstNode->save();


$secondNode->message = "world!";
$secondNode->someOtherAttribute = 'blah blah blah';
$secondNode->save();
Expand All @@ -38,6 +43,10 @@
*/
$relationship = $firstNode->createRelationshipTo($secondNode, 'KNOWS');
$relationship->message = "brave Neo4j";
$relationship->blah = "blah blah";
$relationship->save();

$relationship->blah = NULL; // Setting to NULL removed the property
$relationship->save();

$relationship2 = $thirdNode->createRelationshipTo($secondNode, 'LOVES');
Expand Down
74 changes: 51 additions & 23 deletions php-neo-rest.php
Expand Up @@ -45,10 +45,10 @@ class PropertyContainer

public function __set($k, $v)
{
// because neo cant handle NULLs
if ($v===NULL) $v = '';

$this->_data[$k] = $v;
if ($v===NULL && isset($this->_data[$k]))
unset($this->_data[$k]);
else
$this->_data[$k] = $v;
}

public function __get($k)
Expand Down Expand Up @@ -87,16 +87,23 @@ public function delete()
if (!$this->_is_new)
{
list($response, $http_code) = HTTPUtil::deleteRequest($this->getUri());

if ($http_code!=204) throw new HttpException($http_code);

$this->_id = NULL;
$this->_id_new = TRUE;
}
}

public function save()
{
list($response, $http_code) = HTTPUtil::jsonPostRequest($this->getUri(), $this->_data);

if ($http_code!=201) throw new HttpException($http_code);
if ($this->_is_new) {
list($response, $http_code) = HTTPUtil::jsonPostRequest($this->getUri(), $this->_data);
if ($http_code!=201) throw new HttpException($http_code);
} else {
list($response, $http_code) = HTTPUtil::jsonPutRequest($this->getUri().'/properties', $this->_data);
if ($http_code!=204) throw new HttpException($http_code);
}

if ($this->_is_new)
{
Expand Down Expand Up @@ -235,16 +242,21 @@ public function getOtherNode($node)

public function save()
{
$payload = array(
if ($this->_is_new) {
$payload = array(
'to' => $this->getEndNode()->getUri(),
'type' => $this->_type,
'data'=>$this->_data
);

list($response, $http_code) = HTTPUtil::jsonPostRequest($this->getUri(), $payload);

if ($http_code!=201) throw new HttpException($http_code);


list($response, $http_code) = HTTPUtil::jsonPostRequest($this->getUri(), $payload);

if ($http_code!=201) throw new HttpException($http_code);
} else {
list($response, $http_code) = HTTPUtil::jsonPutRequest($this->getUri().'/properties', $this->_data);
if ($http_code!=204) throw new HttpException($http_code);
}

if ($this->_is_new)
{
$this->_id = end(explode("/", $response['self']));
Expand All @@ -257,16 +269,22 @@ public function delete()
if (!$this->_is_new)
{
list($response, $http_code) = HTTPUtil::deleteRequest($this->getUri());

if ($http_code!=204) throw new HttpException($http_code);

$this->_id = NULL;
$this->_id_new = TRUE;
}
}

public function getUri()
{
$uri = $this->getStartNode()->getUri().'/relationships';
if ($this->_is_new)
$uri = $this->getStartNode()->getUri().'/relationships';
else
$uri = $this->_neo_db->getBaseUri().'relationship/'.$this->getId();

if (!$this->_is_new) $uri .= '/'.$this->getId();
//if (!$this->_is_new) $uri .= '/'.$this->getId();

return $uri;
}
Expand Down Expand Up @@ -314,30 +332,35 @@ class HTTPUtil
function request($url, $method='GET', $post_data='', $content_type='', $accept_type='')
{
// Uncomment for debugging
//echo 'HTTP: ', $method, " : " ,$url , "\n";
//echo 'HTTP: ', $method, " : " ,$url , " : ", $post_data, "\n";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);


//if ($method==self::POST){
// curl_setopt($ch, CURLOPT_POST, true);
//} else {
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
//}

if ($method==self::POST){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

if ($post_data)
{
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$headers = array(
'Content-Length: ' . strlen($post_data),
'Content-Type: '.$content_type,
'Accept: '.$accept_type
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

$response = curl_exec($ch);

$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Expand All @@ -358,6 +381,11 @@ function jsonRequest($url, $method, $data=NULL)
return $ret;
}

function jsonPutRequest($url, $data)
{
return self::jsonRequest($url, self::PUT, $data);
}

function jsonPostRequest($url, $data)
{
return self::jsonRequest($url, self::POST, $data);
Expand Down

0 comments on commit 274ee8f

Please sign in to comment.