forked from ColFusion/ColfusionWeb
/
AddToIndex.php
117 lines (103 loc) · 2.58 KB
/
AddToIndex.php
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
<?php
namespace Everyman\Neo4j\Command;
use Everyman\Neo4j\Command,
Everyman\Neo4j\Client,
Everyman\Neo4j\Exception,
Everyman\Neo4j\PropertyContainer,
Everyman\Neo4j\Relationship,
Everyman\Neo4j\Node,
Everyman\Neo4j\Index;
/**
* Add an entity to an index
*/
class AddToIndex extends Command
{
protected $index = null;
protected $entity = null;
protected $key = null;
protected $value = null;
/**
* Set the index to drive the command
*
* @param Client $client
* @param Index $index
* @param PropertyContainer $entity
* @param string $key
* @param string $value
*/
public function __construct(Client $client, Index $index, PropertyContainer $entity, $key, $value)
{
parent::__construct($client);
$this->index = $index;
$this->entity = $entity;
$this->key = $key;
$this->value = $value;
}
/**
* Return the data to pass
*
* @return mixed
*/
protected function getData()
{
if (!$this->entity || !$this->entity->hasId()) {
throw new Exception('No entity to index specified');
}
$data = array();
$type = trim((string)$this->index->getType());
$data['uri'] = $this->getTransport()->getEndpoint().'/'.$type.'/'.$this->entity->getId();
$key = trim((string)$this->key);
if (!$key) {
throw new Exception('No key specified to add to index');
}
$data['key'] = $key;
$data['value'] = $this->value;
return $data;
}
/**
* Return the transport method to call
*
* @return string
*/
protected function getMethod()
{
return 'post';
}
/**
* Return the path to use
*
* @return string
*/
protected function getPath()
{
$type = trim((string)$this->index->getType());
if ($type != Index::TypeNode && $type != Index::TypeRelationship) {
throw new Exception('No type specified for index');
} else if ($type == Index::TypeNode && !($this->entity instanceof Node)) {
throw new Exception('Cannot add a node to a non-node index');
} else if ($type == Index::TypeRelationship && !($this->entity instanceof Relationship)) {
throw new Exception('Cannot add a relationship to a non-relationship index');
}
$name = trim((string)$this->index->getName());
if (!$name) {
throw new Exception('No name specified for index');
}
$name = rawurlencode($name);
return '/index/'.$type.'/'.$name;
}
/**
* Use the results
*
* @param integer $code
* @param array $headers
* @param array $data
* @return integer on failure
*/
protected function handleResult($code, $headers, $data)
{
if ((int)($code / 100) != 2) {
$this->throwException('Unable to add entity to index', $code, $headers, $data);
}
return true;
}
}