forked from parse-community/parse-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddUniqueOperation.php
More file actions
executable file
·140 lines (127 loc) · 3.6 KB
/
Copy pathAddUniqueOperation.php
File metadata and controls
executable file
·140 lines (127 loc) · 3.6 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
131
132
133
134
135
136
137
138
139
140
<?php
namespace Parse\Internal;
use Parse\ParseClient;
use Parse\ParseException;
/**
* Class AddUniqueOperation - Operation to add unique objects to an array key.
*
* @author Fosco Marotto <fjm@fb.com>
*/
class AddUniqueOperation implements FieldOperation
{
/**
* Array containing objects to add.
*
* @var array
*/
private $objects;
/**
* Creates an operation for adding unique values to an array key.
*
* @param array $objects Objects to add.
*
* @throws ParseException
*/
public function __construct($objects)
{
if (!is_array($objects)) {
throw new ParseException('AddUniqueOperation requires an array.');
}
$this->objects = $objects;
}
/**
* Returns the values for this operation.
*
* @return mixed
*/
public function getValue()
{
return $this->objects;
}
/**
* Returns an associative array encoding of this operation.
*
* @return array
*/
public function _encode()
{
return ['__op' => 'AddUnique',
'objects' => ParseClient::_encode($this->objects, true), ];
}
/**
* Merge this operation with the previous operation and return the result.
*
* @param FieldOperation $previous Previous Operation.
*
* @throws ParseException
*
* @return FieldOperation Merged Operation.
*/
public function _mergeWithPrevious($previous)
{
if (!$previous) {
return $this;
}
if ($previous instanceof DeleteOperation) {
return new SetOperation($this->objects);
}
if ($previous instanceof SetOperation) {
$oldValue = $previous->getValue();
$result = $this->_apply($oldValue, null, null);
return new SetOperation($result);
}
if ($previous instanceof self) {
$oldList = $previous->getValue();
$result = $this->_apply($oldList, null, null);
return new self($result);
}
throw new ParseException(
'Operation is invalid after previous operation.'
);
}
/**
* Apply the current operation and return the result.
*
* @param mixed $oldValue Value prior to this operation.
* @param array $obj Value being applied.
* @param string $key Key this operation affects.
*
* @return array
*/
public function _apply($oldValue, $obj, $key)
{
if (!$oldValue) {
return $this->objects;
}
if (!is_array($oldValue)) {
$oldValue = (array) $oldValue;
}
foreach ($this->objects as $object) {
if ($object instanceof ParseObject && $object->getObjectId()) {
if (!$this->isParseObjectInArray($object, $oldValue)) {
$oldValue[] = $object;
}
} elseif (is_object($object)) {
if (!in_array($object, $oldValue, true)) {
$oldValue[] = $object;
}
} else {
if (!in_array($object, $oldValue, true)) {
$oldValue[] = $object;
}
}
}
return $oldValue;
}
private function isParseObjectInArray($parseObject, $oldValue)
{
foreach ($oldValue as $object) {
if ($object instanceof ParseObject && $object->getObjectId() != null) {
if ($object->getObjectId() == $parseObject->getObjectId()) {
return true;
}
}
}
return false;
}
}