-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.php
More file actions
149 lines (126 loc) · 3.79 KB
/
Copy pathnode.php
File metadata and controls
149 lines (126 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
// Created: Antti Stenvall (antti@stenvall.fi)
//
// Single node data structure
namespace neoData;
require_once 'query.php';
class node {
private $data = array();
private $labels = array();
private $nodeId = null;
protected $defaultValues = array();
// constructor
public function __construct($labelOrNodeId = null, $indexKey = null, $indexValue = null) {
if (!is_null($labelOrNodeId)) {
if (is_null($indexKey)) {
$cypher = 'MATCH (n) where id(n)={id} RETURN n, LABELS(n)';
$res = query::cypher($cypher, array('id' => $labelOrNodeId));
} else {
$cypher = "MATCH (n:$labelOrNodeId) WHERE n.$indexKey = {indexValue} RETURN n, LABELS(n)";
$res = query::cypher($cypher, array('indexValue' => $indexValue));
}
if (count($res['data']) == 0) {
throw new \Exception('Node not found.', 204);
} else if (count($res['data']) > 1) {
throw new \Exception('Many nodes found with same key.', 204);
}
$this->initFromQueryResult($res['data'][0][0], $res['data'][0][1]);
}
}
/*
* Static
*/
static public function create($label, $data, $indexKey = null) {
if (!is_null($indexKey)) {
$labelTemp = $label;
if (is_array($labelTemp)) {
$labelTemp = implode(':', $labelTemp);
}
// search first if exists, if index key is given (this could be done also via neo4j RESTapi)
$cypher = "MATCH (n:$labelTemp) WHERE n.$indexKey = {indexValue} RETURN n";
$res = query::cypher($cypher, array('indexValue' => $data[$indexKey]));
if (count($res['data']) != 0) {
throw new \Exception('Node with given index value already exists.', 204);
}
}
$res = query::createNode($label, $data);
$node = new node();
if (!is_array($label)) {
$label = array($label);
}
$node->initFromQueryResult($res['data'][0][0], $label);
return $node;
}
/*
* Public
*/
public function deleteProperties($data) {
if (!is_array($data)) {
$data = array($data);
}
$res = query::removeData($this->getNodeId(), $data);
foreach ($data as $value) {
unset($this->data[$value]);
}
return $res;
}
public function getData() {
return $this->data;
}
public function getDataWithDefaultValues() {
$data = $this->data;
foreach ($this->defaultValues as $key => $value) {
if (!isset($data[$key])) {
$data[$key] = $value;
}
}
return $data;
}
public function getLabels() {
return $this->labels;
}
public function getNodeId() {
return $this->nodeId;
}
public function getProperties(){
return array_keys($this->data);
}
public function getValue($key) {
if (!isset($this->data[$key])) {
if (!isset($this->defaultValues[$key])) {
throw new \Exception("Key '$key' does not exists.", 400);
}
return $this->defaultValues[$key];
}
return $this->data[$key];
}
public function hasLabel($label) {
return in_array($label, $this->labels);
}
public function hasProperty($key){
return array_key_exists($key, $this->data);
}
public function initFromQueryResult($data, $labels = null) {
// find id, self url is like: http://localhost:7474/db/data/node/279
if (!isset($data['self'])) {
throw new \Exception('Remember to pass all node data.', 400);
}
$selfUrl = $data['self'];
$this->nodeId = query::getNodeIdFromSelf($selfUrl);
if (!is_null($labels)) {
$this->labels = $labels;
}
$this->data = $data['data'];
}
public function setDefaultValues($defaultValues) {
$this->defaultValues = $defaultValues;
}
public function update($data) {
$res = query::updateData($this->getNodeId(), $data);
foreach ($data as $key => $value) {
$this->data[$key] = $value;
}
return $res;
}
}
?>