forked from parse-community/parse-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddOperation.php
More file actions
executable file
·111 lines (100 loc) · 2.62 KB
/
Copy pathAddOperation.php
File metadata and controls
executable file
·111 lines (100 loc) · 2.62 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
<?php
namespace Parse\Internal;
use Parse\ParseClient;
use Parse\ParseException;
/**
* Class AddOperation - FieldOperation for adding object(s) to array fields.
*
* @author Fosco Marotto <fjm@fb.com>
*/
class AddOperation implements FieldOperation
{
/**
* Array with objects to add.
*
* @var array
*/
private $objects;
/**
* Creates an AddOperation with the provided objects.
*
* @param array $objects Objects to add.
*
* @throws ParseException
*/
public function __construct($objects)
{
if (!is_array($objects)) {
throw new ParseException('AddOperation 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' => 'Add',
'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 new SetOperation($this->objects);
}
if ($previous instanceof SetOperation) {
$oldList = $previous->getValue();
return new SetOperation(
array_merge((array) $oldList, (array) $this->objects)
);
}
if ($previous instanceof self) {
$oldList = $previous->getValue();
return new SetOperation(
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 (!$oldValue) {
return $this->objects;
}
return array_merge((array) $oldValue, (array) $this->objects);
}
}