forked from parse-community/parse-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveOperation.php
More file actions
130 lines (120 loc) · 3.28 KB
/
Copy pathRemoveOperation.php
File metadata and controls
130 lines (120 loc) · 3.28 KB
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
<?php
namespace Parse\Internal;
use Parse\ParseClient;
use Parse\ParseException;
use Parse\ParseObject;
/**
* Class RemoveOperation - FieldOperation for removing object(s) from array
* fields.
*
* @author Fosco Marotto <fjm@fb.com>
*/
class RemoveOperation implements FieldOperation
{
/**
* Array with objects to remove.
*
* @var array
*/
private $objects;
/**
* Creates an RemoveOperation with the provided objects.
*
* @param array $objects Objects to remove.
*
* @throws ParseException
*/
public function __construct($objects)
{
if (!is_array($objects)) {
throw new ParseException('RemoveOperation requires an array.');
}
$this->objects = $objects;
}
/**
* Gets the objects for this operation.
*
* @return mixed
*/
public function getValue()
{
return $this->objects;
}
/**
* Returns associative array representing encoded operation.
*
* @return array
*/
public function _encode()
{
return ['__op' => 'Remove',
'objects' => ParseClient::_encode($this->objects, true), ];
}
/**
* Takes a previous operation and returns a merged operation to replace it.
*
* @param FieldOperation $previous Previous operation.
*
* @throws ParseException
*
* @return FieldOperation Merged operation.
*/
public function _mergeWithPrevious($previous)
{
if (!$previous) {
return $this;
}
if ($previous instanceof DeleteOperation) {
return $previous;
}
if ($previous instanceof SetOperation) {
return new SetOperation(
$this->_apply($previous->getValue(), $this->objects, null)
);
}
if ($previous instanceof self) {
$oldList = $previous->getValue();
return new self(
array_merge((array) $oldList, (array) $this->objects)
);
}
throw new ParseException(
'Operation is invalid after previous operation.'
);
}
/**
* Applies current operation, returns resulting value.
*
* @param mixed $oldValue Value prior to this operation.
* @param mixed $obj Value being applied.
* @param string $key Key this operation affects.
*
* @return array
*/
public function _apply($oldValue, $obj, $key)
{
if (empty($oldValue)) {
return [];
}
$newValue = [];
foreach ($oldValue as $oldObject) {
foreach ($this->objects as $newObject) {
if ($oldObject instanceof ParseObject) {
if ($newObject instanceof ParseObject
&& !$oldObject->isDirty()
&& $oldObject->getObjectId() == $newObject->getObjectId()
) {
// found the object, won't add it.
} else {
$newValue[] = $oldObject;
}
} else {
if ($oldObject !== $newObject) {
$newValue[] = $oldObject;
}
}
}
}
return $newValue;
}
}