forked from googleapis/google-cloud-php-datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.php
363 lines (338 loc) · 9.6 KB
/
Entity.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Datastore;
use ArrayAccess;
use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
/**
* A Datastore Entity
*
* Entity implements PHP's [ArrayAccess](http://php.net/arrayaccess), allowing
* access via the array syntax (example below).
*
* Properties are mapped automatically to their corresponding Datastore value
* types. Refer to the table below for a guide to how types are stored.
*
* | **PHP Type** | **Datastore Value Type** |
* |--------------------------------------------|--------------------------------------|
* | `\DateTimeInterface` | `timestampValue` |
* | {@see Google\Cloud\Datastore\Key} | `keyValue` |
* | {@see Google\Cloud\Datastore\GeoPoint} | `geoPointValue` |
* | {@see Google\Cloud\Datastore\Entity} | `entityValue` |
* | {@see Google\Cloud\Datastore\Blob} | `blobValue` |
* | {@see Google\Cloud\Core\Int64} | `integerValue` |
* | Associative Array | `entityValue` (No Key) |
* | Non-Associative Array | `arrayValue` |
* | `float` | `doubleValue` |
* | `int` | `integerValue` |
* | `string` | `stringValue` |
* | `resource` | `blobValue` |
* | `NULL` | `nullValue` |
* | `bool` | `booleanValue` |
* | `object` (Outside types specified above) | **ERROR** `InvalidArgumentException` |
*
* Example:
* ```
* use Google\Cloud\Datastore\DatastoreClient;
*
* $datastore = new DatastoreClient();
*
* $key = $datastore->key('Person', 'Bob');
* $entity = $datastore->entity($key, [
* 'firstName' => 'Bob',
* 'lastName' => 'Testguy'
* ]);
*
* echo $entity['firstName']; // 'Bob'
* $entity['location'] = 'Detroit, MI';
* ```
*
* @see https://cloud.google.com/datastore/docs/reference/rest/v1/Entity Entity API documentation
*/
class Entity implements ArrayAccess
{
use DatastoreTrait;
const EXCLUDE_FROM_INDEXES = '___GOOGLECLOUDPHP___EXCLUDEFROMINDEXES___';
/**
* @var Key
*/
private $key;
/**
* @var array
*/
private $entity;
/**
* @var array
*/
private $options;
/**
* @param Key $key The Entity's Key, defining its unique identifier.
* @param array $entity [optional] The entity body.
* @param array $options [optional] {
* Configuration Options
*
* @type string $cursor Set only when the entity is obtained by a query
* result. If set, the entity cursor can be retrieved from
* {@see Google\Cloud\Datastore\Entity::cursor()}.
* @type string $baseVersion Set only when the entity is obtained by a
* query result. If set, the entity cursor can be retrieved from
* {@see Google\Cloud\Datastore\Entity::baseVersion()}.
* @type array $excludeFromIndexes A list of entity keys to exclude from
* datastore indexes.
* @type array $meanings A list of meaning values for entity properties.
* @type bool $populatedByService Indicates whether the entity was
* created as the result of a service request.
* }
* @throws InvalidArgumentException
*/
public function __construct(Key $key, array $entity = [], array $options = [])
{
$this->key = $key;
$this->entity = $entity;
$this->options = $options + [
'cursor' => null,
'baseVersion' => null,
'populatedByService' => false,
'excludeFromIndexes' => [],
'meanings' => []
];
}
/**
* Get the entity data
*
* Example:
* ```
* $data = $entity->get();
* ```
*
* @return array
*/
public function get()
{
return $this->entity;
}
/**
* Set the entity data
*
* Calling this method replaces the entire entity body. To add or modify a
* single value on the entity, use the array syntax for assignment.
*
* Example:
* ```
* $entity->set([
* 'firstName' => 'Dave'
* ]);
* ```
*
* @param array $entity The new entity body.
* @return void
*/
public function set(array $entity)
{
$this->entity = $entity;
}
/**
* Get the Entity Key
*
* Example:
* ```
* $key = $entity->key();
* ```
*
* @return Key
*/
public function key()
{
return $this->key;
}
/**
* Fetch the cursor
*
* This is only set when the entity was obtained from a query result. It
* can be used to manually paginate results.
*
* Example:
* ```
* $cursor = $entity->cursor();
* ```
*
* @see https://cloud.google.com/datastore/docs/reference/rest/v1/EntityResult EntityResult.cursor
*
* @return string|null
*/
public function cursor()
{
return $this->options['cursor'];
}
/**
* Fetch the baseVersion
*
* This is only set when the entity was obtained from a query result. It
* is used for concurrency control internally.
*
* Example:
* ```
* $baseVersion = $entity->baseVersion();
* ```
*
* @see https://cloud.google.com/datastore/docs/reference/rest/v1/EntityResult EntitResult.version
*
* @return string|null
*/
public function baseVersion()
{
return $this->options['baseVersion'];
}
/**
* Indicate whether the entity was created as the result of an API call.
*
* Example:
* ```
* $populatedByService = $entity->populatedByService();
* ```
*
* @return bool
*/
public function populatedByService()
{
return $this->options['populatedByService'];
}
/**
* A list of entity properties to exclude from datastore indexes.
*
* Example:
* ```
* $entity['birthDate'] = new DateTime('December 31, 1969');
* $entity->setExcludeFromIndexes([
* 'birthDate'
* ]);
* ```
*
* @param array $properties A list of properties to exclude from indexes.
* @return void
*/
public function setExcludeFromIndexes(array $properties)
{
$this->options['excludeFromIndexes'] = $properties;
}
/**
* Return a list of properties excluded from datastore indexes
*
* Example:
* ```
* $excludedFromIndexes = $entity->excludedProperties();
* ```
*
* @return array
*/
public function excludedProperties()
{
return $this->options['excludeFromIndexes'];
}
/**
* return a list of meaning values
*
* Example:
* ```
* $meanings = $entity->meanings();
* ```
*
* @return array
*/
public function meanings()
{
return $this->options['meanings'];
}
/**
* @param string $key The value name.
* @param mixed $value The value.
* @return void
* @access private
*/
public function offsetSet($key, $val)
{
$this->entity[$key] = $val;
}
/**
* @param string $key the value to check.
* @return bool
* @access private
*/
public function offsetExists($key)
{
return isset($this->entity[$key]);
}
/**
* @param string $key the value to remove.
* @return void
* @access private
*/
public function offsetUnset($key)
{
unset($this->entity[$key]);
}
/**
* @param string $key the value to retrieve.
* @return mixed
* @access private
*/
public function offsetGet($key)
{
return isset($this->entity[$key])
? $this->entity[$key]
: null;
}
/**
* @param string $property
* @return mixed
* @access private
*/
public function __get($property)
{
return $this->offsetGet($property);
}
/**
* @param string $property
* @param mixed $value
* @return void
* @access private
*/
public function __set($property, $value)
{
$this->offsetSet($property, $value);
}
/**
* @param string $property
* @return void
* @access private
*/
public function __unset($property)
{
if ($this->offsetExists($property)) {
$this->offsetUnset($property);
}
}
/**
* @param string $property
* @return bool
* @access private
*/
public function __isset($property)
{
return $this->offsetExists($property) && $this->offsetGet($property) !== null;
}
}