1 <?php
2
3 /**
4 * ArangoDB PHP client: single document
5 *
6 * @package triagens\ArangoDb
7 * @author Jan Steemann
8 * @copyright Copyright 2012, triagens GmbH, Cologne, Germany
9 */
10
11 namespace triagens\ArangoDb;
12
13 /**
14 * Value object representing a single collection-based edge document
15 *
16 * <br>
17 *
18 * @package triagens\ArangoDb
19 * @since 1.0
20 */
21 class Edge extends
22 Document
23 {
24 /**
25 * The edge's from (might be NULL for new documents)
26 *
27 * @var mixed
28 */
29 protected $_from;
30
31 /**
32 * The edge's to (might be NULL for new documents)
33 *
34 * @var mixed
35 */
36 protected $_to;
37
38 /**
39 * Document _from index
40 */
41
42 const ENTRY_FROM = '_from';
43
44 /**
45 * Revision _to index
46 */
47 const ENTRY_TO = '_to';
48
49
50 /**
51 * Set a document attribute
52 *
53 * The key (attribute name) must be a string.
54 *
55 * This will validate the value of the attribute and might throw an
56 * exception if the value is invalid.
57 *
58 * @throws ClientException
59 *
60 * @param string $key - attribute name
61 * @param mixed $value - value for attribute
62 *
63 * @return void
64 */
65 public function set($key, $value)
66 {
67 if ($this->_doValidate) {
68 // validate the value passed
69 ValueValidator::validate($value);
70 }
71
72 if ($key[0] === '_') {
73 if ($key === self::ENTRY_ID) {
74 $this->setInternalId($value);
75
76 return;
77 }
78
79 if ($key === self::ENTRY_KEY) {
80 $this->setInternalKey($value);
81
82 return;
83 }
84
85 if ($key === self::ENTRY_REV) {
86 $this->setRevision($value);
87
88 return;
89 }
90
91 if ($key === self::ENTRY_FROM) {
92 $this->setFrom($value);
93
94 return;
95 }
96
97 if ($key === self::ENTRY_TO) {
98 $this->setTo($value);
99
100 return;
101 }
102 }
103
104 if (!$this->_changed) {
105 if (!isset($this->_values[$key]) || $this->_values[$key] !== $value) {
106 // set changed flag
107 $this->_changed = true;
108 }
109 }
110
111 // and store the value
112 $this->_values[$key] = $value;
113 }
114
115
116 /**
117 * Set the 'from' vertex document-handler
118 *
119 * @param mixed $from - from vertex
120 *
121 * @return Edge - edge object
122 */
123 public function setFrom($from)
124 {
125 $this->_from = $from;
126
127 return $this;
128 }
129
130 /**
131 * Get the 'from' vertex document-handler (if already known)
132 *
133 * @return mixed - document-handler
134 */
135 public function getFrom()
136 {
137 return $this->_from;
138 }
139
140 /**
141 * Set the 'to' vertex document-handler
142 *
143 * @param mixed $to - to vertex
144 *
145 * @return Edge - edge object
146 */
147 public function setTo($to)
148 {
149 $this->_to = $to;
150
151 return $this;
152 }
153
154 /**
155 * Get the 'to' vertex document-handler (if already known)
156 *
157 * @return mixed - document-handler
158 */
159 public function getTo()
160 {
161 return $this->_to;
162 }
163
164 /**
165 * Get all document attributes for insertion/update
166 *
167 * @return mixed - associative array of all document attributes/values
168 */
169 public function getAllForInsertUpdate()
170 {
171 $data = parent::getAllForInsertUpdate();
172 $data['_from'] = $this->_from;
173 $data['_to'] = $this->_to;
174
175 return $data;
176 }
177
178 }
179