-
-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathParseObject.php
1648 lines (1521 loc) · 48.1 KB
/
ParseObject.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Class ParseObject | Parse/ParseObject.php
*/
namespace Parse;
use Exception;
use Parse\Internal\AddOperation;
use Parse\Internal\AddUniqueOperation;
use Parse\Internal\DeleteOperation;
use Parse\Internal\Encodable;
use Parse\Internal\FieldOperation;
use Parse\Internal\IncrementOperation;
use Parse\Internal\ParseRelationOperation;
use Parse\Internal\RemoveOperation;
use Parse\Internal\SetOperation;
/**
* Class ParseObject - Representation of an object stored on Parse.
*
* @author Fosco Marotto <fjm@fb.com>
* @package Parse
*/
class ParseObject implements Encodable
{
/**
* Data as it exists on the server.
*
* @var array
*/
protected $serverData;
/**
* Set of unsaved operations.
*
* @var array
*/
protected $operationSet;
/**
* Estimated value of applying operationSet to serverData.
*
* @var array
*/
private $estimatedData;
/**
* Determine if data available for a given key or not.
*
* @var array
*/
private $dataAvailability;
/**
* Class name for data on Parse.
*
* @var string
*/
private $className;
/**
* Unique identifier on Parse.
*
* @var string
*/
private $objectId;
/**
* Timestamp when object was created.
*
* @var \DateTime
*/
private $createdAt;
/**
* Timestamp when object was last updated.
*
* @var \DateTime
*/
private $updatedAt;
/**
* Whether the object has been fully fetched from Parse.
*
* @var bool
*/
private $hasBeenFetched;
/**
* Holds the registered subclasses and Parse class names.
*
* @var array
*/
private static $registeredSubclasses = [];
/**
* Parse Class name, overridden by classes subclassing ParseObject
*
* @var string
*/
public static $parseClassName;
/**
* Create a Parse Object.
*
* Creates a pointer object if an objectId is provided,
* otherwise creates a new object.
*
* @param string $className Class Name for data on Parse.
* @param mixed $objectId Object Id for Existing object.
* @param bool $isPointer
*
* @throws Exception
*/
public function __construct($className = null, $objectId = null, $isPointer = false)
{
if (empty(self::$registeredSubclasses)) {
throw new Exception(
'You must initialize the ParseClient using ParseClient::initialize '.
'and your Parse API keys before you can begin working with Objects.',
109
);
}
$subclass = static::getSubclass();
$class = get_called_class();
if (!$className && $subclass !== false) {
$className = $subclass;
}
if ($class !== __CLASS__ && $className !== $subclass) {
throw new Exception(
'You must specify a Parse class name or register the appropriate '.
'subclass when creating a new Object. Use ParseObject::create to '.
'create a subclass object.'
);
}
$this->className = $className;
$this->serverData = [];
$this->operationSet = [];
$this->estimatedData = [];
$this->dataAvailability = [];
$this->objectId = $objectId;
$this->hasBeenFetched = false;
if (!$objectId || $isPointer) {
$this->hasBeenFetched = true;
}
}
/**
* Gets the Subclass className if exists, otherwise false.
*/
private static function getSubclass()
{
return array_search(get_called_class(), self::$registeredSubclasses);
}
/**
* Setter to catch property calls and protect certain fields.
*
* @param string $key Key to set a value on.
* @param mixed $value Value to assign.
*
* @throws Exception
*/
public function __set($key, $value)
{
if ($key != 'objectId'
&& $key != 'createdAt'
&& $key != 'updatedAt'
&& $key != 'className'
) {
$this->set($key, $value);
} else {
throw new Exception('Protected field could not be set.', 139);
}
}
/**
* Getter to catch direct property calls and pass them to the get function.
*
* @param string $key Key to retrieve from the Object.
*
* @return mixed
*/
public function __get($key)
{
return $this->get($key);
}
/**
* Magic handler to catch isset calls to object properties.
*
* @param string $key Key to check on the object.
*
* @return bool
*/
public function __isset($key)
{
return $this->has($key);
}
/**
* Get current value for an object property.
*
* @param string $key Key to retrieve from the estimatedData array.
*
* @throws Exception
*
* @return mixed
*/
public function get($key)
{
if (!$this->_isDataAvailable($key)) {
throw new Exception(
'ParseObject has no data for this key. Call fetch() to get the data.'
);
}
if (isset($this->estimatedData[$key])) {
return $this->estimatedData[$key];
}
return null;
}
/**
* Get values for all keys of an object.
*
* @return array
*/
public function getAllKeys()
{
return $this->estimatedData;
}
/**
* Check if the object has a given key.
*
* @param string $key Key to check
*
* @return bool
*/
public function has($key)
{
return isset($this->estimatedData[$key]);
}
/**
* Check if the a value associated with a key has been
* added/updated/removed and not saved yet.
*
* @param string $key
*
* @return bool
*/
public function isKeyDirty($key)
{
return isset($this->operationSet[$key]);
}
/**
* Check if the object or any of its child objects have unsaved operations.
*
* @return bool
*/
public function isDirty()
{
return $this->_isDirty(true);
}
/**
* Detects if the object (and optionally the child objects) has unsaved
* changes.
*
* @param bool $considerChildren Whether to consider children when checking for dirty state
*
* @return bool
*/
protected function _isDirty($considerChildren)
{
return
(count($this->operationSet) || $this->objectId === null) ||
($considerChildren && $this->hasDirtyChildren());
}
/**
* Determines whether this object has child objects that are dirty
*
* @return bool
*/
private function hasDirtyChildren()
{
$result = false;
self::traverse(
true,
$this->estimatedData,
function ($object) use (&$result) {
if ($object instanceof ParseObject) {
if ($object->_isDirty(false)) {
$result = true;
}
}
}
);
return $result;
}
/**
* Returns true if this object exists on the Server
*
* @param bool $useMasterKey Whether to use the Master Key.
*
* @return bool
*/
public function exists($useMasterKey = false)
{
if (!$this->objectId) {
return false;
}
try {
$query = new ParseQuery($this->className);
$query->get($this->objectId, $useMasterKey);
return true;
} catch (Exception $e) {
if ($e->getCode() === 101) {
return false;
}
throw $e;
}
}
/**
* Validate and set a value for an object key.
*
* @param string $key Key to set a value for on the object.
* @param mixed $value Value to set on the key.
*
* @throws Exception
*/
public function set($key, $value)
{
if (!$key) {
throw new Exception('key may not be null.');
}
if (is_array($value)) {
throw new Exception(
'Must use setArray() or setAssociativeArray() for this value.'
);
}
$this->_performOperation($key, new SetOperation($value));
}
/**
* Set an array value for an object key.
*
* @param string $key Key to set the value for on the object.
* @param array $value Value to set on the key.
*
* @throws Exception
*/
public function setArray($key, $value)
{
if (!$key) {
throw new Exception('key may not be null.');
}
if (!is_array($value)) {
throw new Exception(
'Must use set() for non-array values.'
);
}
$this->_performOperation($key, new SetOperation(array_values($value)));
}
/**
* Set an associative array value for an object key.
*
* @param string $key Key to set the value for on the object.
* @param array $value Value to set on the key.
*
* @throws Exception
*/
public function setAssociativeArray($key, $value)
{
if (!$key) {
throw new Exception('key may not be null.');
}
if (!is_array($value)) {
throw new Exception(
'Must use set() for non-array values.'
);
}
$this->_performOperation($key, new SetOperation($value, true));
}
/**
* Remove a value from an array for an object key.
*
* @param string $key Key to remove the value from on the object.
* @param mixed $value Value to remove from the array.
*
* @throws Exception
*/
public function remove($key, $value)
{
if (!$key) {
throw new Exception('key may not be null.');
}
if (!is_array($value)) {
$value = [$value];
}
$this->_performOperation($key, new RemoveOperation($value));
}
/**
* Revert all unsaved operations.
*/
public function revert()
{
$this->operationSet = [];
$this->rebuildEstimatedData();
}
/**
* Clear all keys on this object by creating delete operations
* for each key.
*/
public function clear()
{
foreach ($this->estimatedData as $key => $value) {
$this->delete($key);
}
}
/**
* Perform an operation on an object property.
*
* @param string $key Key to perform an operation upon.
* @param FieldOperation $operation Operation to perform.
*/
public function _performOperation($key, FieldOperation $operation)
{
$oldValue = null;
if (isset($this->estimatedData[$key])) {
$oldValue = $this->estimatedData[$key];
}
$newValue = $operation->_apply($oldValue, $this, $key);
if ($newValue !== null) {
$this->estimatedData[$key] = $newValue;
} elseif (isset($this->estimatedData[$key])) {
unset($this->estimatedData[$key]);
}
if (isset($this->operationSet[$key])) {
$oldOperations = $this->operationSet[$key];
$newOperations = $operation->_mergeWithPrevious($oldOperations);
$this->operationSet[$key] = $newOperations;
} else {
$this->operationSet[$key] = $operation;
}
$this->dataAvailability[$key] = true;
}
/**
* Get the Parse Class Name for the object.
*
* @return string
*/
public function getClassName()
{
return $this->className;
}
/**
* Get the objectId for the object, or null if unsaved.
*
* @return string|null
*/
public function getObjectId()
{
return $this->objectId;
}
/**
* Get the createdAt for the object, or null if unsaved.
*
* @return \DateTime|null
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Returns true if the object has been fetched.
*
* @return bool
*/
public function isDataAvailable()
{
return $this->hasBeenFetched;
}
/**
* Returns whether or not data is available for a given key
*
* @param string $key Key to check availability of
* @return bool
*/
private function _isDataAvailable($key)
{
return $this->isDataAvailable() || isset($this->dataAvailability[$key]);
}
/**
* Get the updatedAt for the object, or null if unsaved.
*
* @return \DateTime|null
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Static method which returns a new Parse Object for a given class
* Optionally creates a pointer object if the objectId is provided.
*
* @param string $className Class Name for data on Parse.
* @param string $objectId Unique identifier for existing object.
* @param bool $isPointer If the object is a pointer.
*
* @return ParseObject
*/
public static function create($className, $objectId = null, $isPointer = false)
{
if (isset(self::$registeredSubclasses[$className])) {
return new self::$registeredSubclasses[$className](
$className,
$objectId,
$isPointer
);
} else {
return new self($className, $objectId, $isPointer);
}
}
/**
* Fetch the whole object from the server and update the local object.
*
* @param bool $useMasterKey Whether to use the master key and override ACLs
*
* @return ParseObject Returns self, so you can chain this call.
*/
public function fetch($useMasterKey = false)
{
$sessionToken = null;
if (ParseUser::getCurrentUser()) {
$sessionToken = ParseUser::getCurrentUser()->getSessionToken();
}
$response = ParseClient::_request(
'GET',
'classes/'.$this->className.'/'.$this->objectId,
$sessionToken,
null,
$useMasterKey
);
$this->_mergeAfterFetch($response);
return $this;
}
/**
* Fetch an array of Parse objects from the server.
*
* @param array $objects The ParseObjects to fetch
* @param array $includeKeys The nested ParseObjects to fetch
* @param bool $useMasterKey Whether to override ACLs
*
* @return ParseObject Returns self, so you can chain this call.
*/
public function fetchWithInclude(array $includeKeys, $useMasterKey = false)
{
$sessionToken = null;
if (ParseUser::getCurrentUser()) {
$sessionToken = ParseUser::getCurrentUser()->getSessionToken();
}
$response = ParseClient::_request(
'GET',
'classes/'.$this->className.'/'.$this->objectId.'?include='.implode(',', $includeKeys),
$sessionToken,
null,
$useMasterKey
);
$this->_mergeAfterFetch($response);
return $this;
}
/**
* Fetch an array of Parse objects from the server.
*
* @param array $objects The ParseObjects to fetch
* @param bool $useMasterKey Whether to override ACLs
*
* @return array
*/
public static function fetchAll(array $objects, $useMasterKey = false)
{
$objectIds = static::toObjectIdArray($objects);
if (!count($objectIds)) {
return $objects;
}
$className = $objects[0]->getClassName();
$query = new ParseQuery($className);
$query->containedIn('objectId', $objectIds);
$query->limit(count($objectIds));
$results = $query->find($useMasterKey);
return static::updateWithFetchedResults($objects, $results);
}
/**
* Fetch an array of Parse Objects from the server with nested Parse Objects.
*
* @param array $objects The ParseObjects to fetch
* @param mixed $includeKeys The nested ParseObjects to fetch
* @param bool $useMasterKey Whether to override ACLs
*
* @return array
*/
public static function fetchAllWithInclude(array $objects, $includeKeys, $useMasterKey = false)
{
$objectIds = static::toObjectIdArray($objects);
if (!count($objectIds)) {
return $objects;
}
$className = $objects[0]->getClassName();
$query = new ParseQuery($className);
$query->containedIn('objectId', $objectIds);
$query->limit(count($objectIds));
$query->includeKey($includeKeys);
$results = $query->find($useMasterKey);
return static::updateWithFetchedResults($objects, $results);
}
/**
* Creates an array of object ids from a given array of ParseObjects
*
* @param array $objects Objects to create id array from
* @return array
* @throws ParseException
*/
private static function toObjectIdArray(array $objects)
{
$objectIds = [];
$count = count($objects);
if (!$count) {
return $objectIds;
}
$className = $objects[0]->getClassName();
for ($i = 0; $i < $count; ++$i) {
$obj = $objects[$i];
if ($obj->getClassName() !== $className) {
throw new ParseException('All objects should be of the same class.', 103);
} elseif (!$obj->getObjectId()) {
throw new ParseException('All objects must have an ID.', 104);
}
array_push($objectIds, $obj->getObjectId());
}
return $objectIds;
}
/**
* Merges an existing array of objects with their fetched counterparts
*
* @param array $objects Original objects to update
* @param array $fetched Fetched object data to update with
* @return array
* @throws ParseException
*/
private static function updateWithFetchedResults(array $objects, array $fetched)
{
$fetchedObjectsById = [];
foreach ($fetched as $object) {
$fetchedObjectsById[$object->getObjectId()] = $object;
}
$count = count($objects);
for ($i = 0; $i < $count; ++$i) {
$obj = $objects[$i];
if (!isset($fetchedObjectsById[$obj->getObjectId()])) {
throw new ParseException('All objects must exist on the server.', 101);
}
$obj->mergeFromObject($fetchedObjectsById[$obj->getObjectId()]);
}
return $objects;
}
/**
* Merges data received from the server.
*
* @param array $result Data retrieved from the server.
* @param bool $completeData Fetch all data or not.
*/
public function _mergeAfterFetch($result, $completeData = true)
{
// This loop will clear operations for keys provided by the server
// It will not clear operations for new keys the server doesn't have.
foreach ($result as $key => $value) {
if (isset($this->operationSet[$key])) {
unset($this->operationSet[$key]);
}
}
$this->serverData = [];
$this->dataAvailability = [];
$this->mergeFromServer($result, $completeData);
$this->rebuildEstimatedData();
}
/**
* Merges data received from the server with a given selected keys.
*
* @param array $result Data retrieved from the server.
* @param array $selectedKeys Keys to be fetched. Null or empty means all
* data will be fetched.
*/
public function _mergeAfterFetchWithSelectedKeys($result, $selectedKeys)
{
$this->_mergeAfterFetch($result, $selectedKeys ? empty($selectedKeys) : true);
foreach ($selectedKeys as $key) {
$this->dataAvailability[$key] = true;
}
}
/**
* Merges data received from the server.
*
* @param array $data Data retrieved from server.
* @param bool $completeData Fetch all data or not.
*/
private function mergeFromServer($data, $completeData = true)
{
$this->hasBeenFetched = ($this->hasBeenFetched || $completeData) ? true : false;
$this->_mergeMagicFields($data);
foreach ($data as $key => $value) {
if ($key === '__type' && $value === 'className') {
continue;
}
$decodedValue = ParseClient::_decode($value);
if (is_array($decodedValue)) {
if (isset($decodedValue['__type'])) {
if ($decodedValue['__type'] === 'Relation') {
$className = $decodedValue['className'];
$decodedValue = new ParseRelation($this, $key, $className);
}
}
}
$this->serverData[$key] = $decodedValue;
$this->dataAvailability[$key] = true;
}
if (!$this->updatedAt && $this->createdAt) {
$this->updatedAt = $this->createdAt;
}
}
/**
* Merge data from other object.
*
* @param ParseObject $other Other object to merge data from
*/
private function mergeFromObject($other)
{
$this->objectId = $other->getObjectId();
$this->createdAt = $other->getCreatedAt();
$this->updatedAt = $other->getUpdatedAt();
$this->serverData = $other->serverData;
$this->operationSet = [];
$this->hasBeenFetched = true;
$this->rebuildEstimatedData();
}
/**
* Handle merging of special fields for the object.
*
* @param array &$data Data received from server.
*/
public function _mergeMagicFields(&$data)
{
if (isset($data['objectId'])) {
$this->objectId = $data['objectId'];
unset($data['objectId']);
}
if (isset($data['createdAt'])) {
$this->createdAt = new \DateTime($data['createdAt']);
unset($data['createdAt']);
}
if (isset($data['updatedAt'])) {
$this->updatedAt = new \DateTime($data['updatedAt']);
unset($data['updatedAt']);
}
if (isset($data['ACL'])) {
$acl = ParseACL::_createACLFromJSON($data['ACL']);
$this->serverData['ACL'] = $acl;
$this->dataAvailability['ACL'] = true;
unset($data['ACL']);
}
}
/**
* Start from serverData and process operations to generate the current
* value set for an object.
*/
protected function rebuildEstimatedData()
{
$this->estimatedData = [];
foreach ($this->serverData as $key => $value) {
$this->estimatedData[$key] = $value;
}
$this->applyOperations($this->operationSet, $this->estimatedData);
}
/**
* Apply operations to a target object.
*
* @param array $operations Operations set to apply.
* @param array &$target Target data to affect.
*/
private function applyOperations($operations, &$target)
{
foreach ($operations as $key => $operation) {
$oldValue = (isset($target[$key]) ? $target[$key] : null);
$newValue = $operation->_apply($oldValue, $this, $key);
if (empty($newValue) && !is_array($newValue)
&& $newValue !== null && !is_scalar($newValue)
) {
unset($target[$key]);
unset($this->dataAvailability[$key]);
} else {
$target[$key] = $newValue;
$this->dataAvailability[$key] = true;
}
}
}
/**
* Delete the object from Parse.
*
* @param bool $useMasterKey Whether to use the master key.
*/
public function destroy($useMasterKey = false)
{
if (!$this->objectId) {
return;
}
$sessionToken = null;
if (ParseUser::getCurrentUser()) {
$sessionToken = ParseUser::getCurrentUser()->getSessionToken();
}
ParseClient::_request(
'DELETE',
'classes/'.$this->className.'/'.$this->objectId,
$sessionToken,
null,
$useMasterKey
);
}
/**
* Delete an array of objects.
*
* @param array $objects Objects to destroy.
* @param bool $useMasterKey Whether to use the master key or not.
* @param int $batchSize Number of objects to process per request
*
* @throws ParseAggregateException
*/
public static function destroyAll(array $objects, $useMasterKey = false, $batchSize = 40)
{
$errors = [];
$objects = array_values($objects); // To support non-ordered arrays
$count = count($objects);
if ($count) {
$processed = 0;
$currentBatch = [];
$currentcount = 0;
while ($processed < $count) {
++$currentcount;
$currentBatch[] = $objects[$processed++];
if ($currentcount == $batchSize || $processed == $count) {
$results = static::destroyBatch($currentBatch, $useMasterKey);
$errors = array_merge($errors, $results);
$currentBatch = [];
$currentcount = 0;
}
}
if (count($errors)) {
throw new ParseAggregateException('Errors during batch destroy.', $errors);
}
}
return;
}
/**
* Destroy batch of objects.
*
* @param ParseObject[] $objects
* @param bool $useMasterKey
*
* @throws ParseException
*
* @return array
*/
private static function destroyBatch(array $objects, $useMasterKey = false)
{
$data = [];
$errors = [];
foreach ($objects as $object) {
$data[] = [
'method' => 'DELETE',
'path' => '/'.ParseClient::getMountPath().
'classes/'.$object->getClassName().
'/'.$object->getObjectId(),
];
}
$sessionToken = null;
if (ParseUser::getCurrentUser()) {
$sessionToken = ParseUser::getCurrentUser()->getSessionToken();
}
$result = ParseClient::_request(
'POST',
'batch',
$sessionToken,
json_encode(['requests' => $data]),
$useMasterKey
);
foreach ($objects as $key => $object) {
if (isset($result[$key]['error'])) {
$error = $result[$key]['error']['error'];
$code = isset($result[$key]['error']['code']) ?
$result[$key]['error']['code'] : -1;
$errors[] = [
'error' => $error,
'code' => $code,
];
}
}
return $errors;
}
/**
* Increment a numeric key by a certain value.
*
* @param string $key Key for numeric value on object to increment.
* @param int $value Value to increment by.
*/
public function increment($key, $value = 1)
{
$this->_performOperation($key, new IncrementOperation($value));
}
/**
* Add a value to an array property.
*
* @param string $key Key for array value on object to add a value to.
* @param mixed $value Value to add.
*/
public function add($key, $value)
{
$this->_performOperation($key, new AddOperation($value));
}
/**
* Add unique values to an array property.
*
* @param string $key Key for array value on object.
* @param mixed $value Value list to add uniquely.
*/
public function addUnique($key, $value)
{
$this->_performOperation($key, new AddUniqueOperation($value));
}
/**
* Delete a key from an object.
*
* @param string $key Key to remove from object.
*/
public function delete($key)
{
$this->_performOperation($key, new DeleteOperation());
}
/**