-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathClientObjectCollection.php
491 lines (420 loc) · 11 KB
/
ClientObjectCollection.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
namespace Office365\Runtime;
use ArrayAccess;
use Exception;
use Generator;
use IteratorAggregate;
use Office365\Runtime\Http\RequestOptions;
use Office365\Runtime\OData\V3\JsonLightFormat;
use Office365\Runtime\Types\EventHandler;
use Traversable;
class PageInfo {
public function __construct()
{
$this->startIndex = 0;
$this->endIndex = 0;
}
public function setNextPage($index){
if($index == 0)
return;
if($this->endIndex < $index){
$this->startIndex = $this->endIndex;
$this->endIndex = $index;
}
}
public function __toString()
{
return "$this->endIndex";
}
/**
* @var int
*/
public $startIndex;
/**
* @var int
*/
public $endIndex;
}
/**
* Client objects collection (represents EntitySet in terms of OData)
*/
class ClientObjectCollection extends ClientObject implements IteratorAggregate, ArrayAccess
{
/**
* @var array
*/
private $data = null;
/**
* @var string|null
*/
public $NextRequestUrl;
/**
* @var string
*/
protected $itemTypeName;
/**
* @var bool
*/
protected $pagedMode;
/**
* @var EventHandler
*/
protected $pageLoaded;
/**
* @var PageInfo
*/
protected $pageInfo;
/**
* @param ClientRuntimeContext $ctx
* @param ResourcePath $resourcePath
* @param string $itemTypeName
*/
public function __construct(ClientRuntimeContext $ctx, ?ResourcePath $resourcePath = null, $itemTypeName=null)
{
parent::__construct($ctx, $resourcePath);
$this->data = array();
$this->NextRequestUrl = null;
$this->itemTypeName = $itemTypeName;
$this->pagedMode = false;
$this->pageInfo = new PageInfo();
$this->pageLoaded = new EventHandler();
}
/**
* @return bool
*/
function getServerObjectIsNull()
{
return !is_array($this->data);
}
function isPropertyAvailable($name)
{
return isset($this->data[$name]);
}
/**
* Adds client object into collection
* @param ClientObject $clientObject
* @param int $index
*/
public function addChildAt(ClientObject $clientObject, $index)
{
array_splice($this->data, $index, 0, [$clientObject]);
if (is_null($clientObject->parentCollection))
$clientObject->parentCollection = $this;
}
/**
* Adds client object into collection
* @param ClientObject $clientObject
*/
public function addChild($clientObject)
{
$this->data[] = $clientObject;
if (is_null($clientObject->parentCollection))
$clientObject->parentCollection = $this;
return $this;
}
/**
* @param ClientObject $clientObject
*/
public function removeChild(ClientObject $clientObject)
{
if (($key = array_search($clientObject, $this->data)) !== false) {
unset($this->data[$key]);
}
}
/**
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* Finds the first item
* @param string $key
* @param string $value
* @return ClientObject|null
*/
public function findFirst($key, $value)
{
$result = $this->findItems(
function (ClientObject $item) use ($key, $value) {
return $item->getProperty($key) === $value;
});
return (is_array($result) && (count($result) > 0) ? array_values($result)[0] : null);
}
/**
* Finds items by entity property
* @param callable $callback
* @return array
*/
public function findItems(callable $callback)
{
$result = array_filter(
$this->data,
function (ClientObject $item) use ($callback) {
return call_user_func($callback, $item);
}
);
if (count($result) > 0)
return array_values($result);
return null;
}
/**
* Gets the item at the specified index
* @param $index
* @return ClientObject
*/
public function getItem($index)
{
return $this->data[$index];
}
/**
* Returns total items count
* @return int
* @throws Exception
*/
public function getCount()
{
$it = $this->getIterator();
while($it->valid() ){
$it->next();
}
return count($this->data);
}
public function clearData()
{
if(!$this->pagedMode){
$this->data = array();
}
$this->NextRequestUrl = null;
}
/**
* Specifies an expression or function that must evaluate to true for a record to be returned in the collection.
* @param string $value
* @return ClientObjectCollection $this
*/
public function filter($value)
{
$this->queryOptions->Filter = rawurlencode($value);
return $this;
}
/**
* Retrieves via server-driven paging mode
* @param int $pageSize
* @param callable $pageLoaded
*/
public function paged($pageSize=null, $pageLoaded=null){
$this->pagedMode = true;
if(isset($pageLoaded))
$this->pageLoaded->addEvent($pageLoaded);
if(isset($pageSize)){
$this->top($pageSize);
}
return $this;
}
/**
* Determines the maximum number of records to return.
* @param string $value
* @return ClientObjectCollection $this
*/
public function orderBy($value)
{
$this->queryOptions->OrderBy = rawurlencode($value);
return $this;
}
/**
* Determines the maximum number of records to return.
* @param int $value
* @return ClientObjectCollection $this
*/
public function top($value)
{
$this->queryOptions->Top = $value;
return $this;
}
/**
* Sets the number of records to skip before it retrieves records in a collection.
* @param int $value
* @return ClientObjectCollection $this
*/
public function skip($value)
{
$this->queryOptions->Skip = $value;
return $this;
}
/**
* Sets the number of records to skip before it retrieves records in a collection.
* @param string $value
* @return ClientObjectCollection $this
*/
public function skiptoken($value)
{
$this->queryOptions->SkipToken = rawurlencode($value);
return $this;
}
/**
* Creates resource for a collection
* @param ResourcePath|null $resourcePath
* @return ClientObject
*/
public function createType($resourcePath=null)
{
$itemTypeName = $this->getItemTypeName();
$clientObject = new $itemTypeName($this->getContext(),$resourcePath);
$clientObject->parentCollection = $this;
return $clientObject;
}
/**
* @return string
*/
public function getItemTypeName()
{
if(!is_null($this->itemTypeName))
return $this->itemTypeName;
return str_replace("Collection", "", get_class($this));
}
/**
* @return array
*/
function toJson($onlyChanges=false)
{
return array_map(function (ClientObject $item) use($onlyChanges) {
return $item->toJson($onlyChanges);
}, $this->getData());
}
/**
* @param string|integer $index
* @param mixed $value
* @param bool $persistChanges
*/
function setProperty($index, $value, $persistChanges = true)
{
if((is_int($index))){
$itemType = $this->createType();
foreach ($value as $k=>$v) {
$itemType->setProperty($k, $v, $persistChanges);
}
$this->addChild($itemType);
}
else{
parent::setProperty($index,$value,$persistChanges);
}
}
/**
* @return Traversable
* @throws Exception
*/
public function getIterator(): Traversable
{
/** @var ClientObject $item */
foreach ($this->data as $index => $item) {
yield $index => $item;
}
if($this->pagedMode){
if($this->hasNext()){
$nextItems = $this->getNext()->executeQuery();
foreach ($nextItems as $item){
yield $item;
}
}
}
}
protected function hasNext(){
return !is_null($this->NextRequestUrl);
}
/**
* @return self
*/
public function get()
{
$this->getContext()->getPendingRequest()->afterExecuteRequest(function (){
$this->pageInfo->setNextPage(count($this->data));
$this->pageLoaded->triggerEvent(array($this));
});
return parent::get();
}
/**
* Gets all the items in a collection, regardless of the size.
* @param int $pageSize
* @param callable $pageLoaded
* @return self
*/
public function getAll($pageSize=null, $pageLoaded=null)
{
$this->paged($pageSize,$pageLoaded);
$this->getContext()->getPendingRequest()->afterExecuteRequest(function (){
if($this->hasNext()) {
$this->getNext();
}
}, false);
return $this->get();
}
/**
* @return ClientObjectCollection
* @throws Exception
*/
private function getNext(){
$this->getContext()->getPendingRequest()->beforeExecuteRequest(function (RequestOptions $request){
$request->Url = $this->NextRequestUrl;
});
$this->get();
return $this;
}
/**
* Whether or not an offset exists
*
* @param int An offset to check for
* @access public
* @return boolean
* @abstracting ArrayAccess
*/
public function offsetExists($offset): bool
{
return isset($this->data[$offset]);
}
#[\ReturnTypeWillChange]
/**
* Returns the value at specified offset
*
* @param int The offset to retrieve
* @access public
* @return ClientObject
* @abstracting ArrayAccess
*/
public function offsetGet($offset)
{
return $this->offsetExists($offset) ? $this->data[$offset] : null;
}
/**
* Assigns a value to the specified offset
*
* @param int The offset to assign the value to
* @param ClientObject The value to set
* @access public
* @abstracting ArrayAccess
*/
public function offsetSet($offset, $value): void
{
if (is_null($offset)) {
$this->data[] = $value;
} else {
$this->data[$offset] = $value;
}
}
/**
* Unsets an offset
*
* @param int The offset to unset
* @access public
* @abstracting ArrayAccess
*/
public function offsetUnset($offset): void
{
if ($this->offsetExists($offset)) {
unset($this->data[$offset]);
}
}
public function getPageInfo(){
return $this->pageInfo;
}
}