8
8
/**
9
9
* Class AddOperation - FieldOperation for adding object(s) to array fields
10
10
*
11
- * @package Parse
12
- * @author Fosco Marotto <fjm@fb.com>
11
+ * @package Parse
12
+ * @author Fosco Marotto <fjm@fb.com>
13
13
*/
14
14
class AddOperation implements FieldOperation
15
15
{
16
16
17
- /**
18
- * @var - Array with objects to add.
19
- */
20
- private $ objects ;
17
+ /**
18
+ * @var - Array with objects to add.
19
+ */
20
+ private $ objects ;
21
21
22
- /**
23
- * Creates an AddOperation with the provided objects.
24
- *
25
- * @param array $objects Objects to add.
26
- *
27
- * @throws ParseException
28
- */
29
- public function __construct ($ objects )
30
- {
31
- if (!is_array ($ objects )) {
32
- throw new ParseException ("AddOperation requires an array. " );
22
+ /**
23
+ * Creates an AddOperation with the provided objects.
24
+ *
25
+ * @param array $objects Objects to add.
26
+ *
27
+ * @throws ParseException
28
+ */
29
+ public function __construct ($ objects )
30
+ {
31
+ if (!is_array ($ objects )) {
32
+ throw new ParseException ("AddOperation requires an array. " );
33
+ }
34
+ $ this ->objects = $ objects ;
33
35
}
34
- $ this ->objects = $ objects ;
35
- }
36
36
37
- /**
38
- * Gets the objects for this operation.
39
- *
40
- * @return mixed
41
- */
42
- public function getValue ()
43
- {
44
- return $ this ->objects ;
45
- }
46
-
47
- /**
48
- * Returns associative array representing encoded operation.
49
- *
50
- * @return array
51
- */
52
- public function _encode ()
53
- {
54
- return array ('__op ' => 'Add ' ,
55
- 'objects ' => ParseClient::_encode ($ this ->objects , true ));
56
- }
57
-
58
- /**
59
- * Takes a previous operation and returns a merged operation to replace it.
60
- *
61
- * @param FieldOperation $previous Previous operation.
62
- *
63
- * @return FieldOperation Merged operation.
64
- * @throws ParseException
65
- */
66
- public function _mergeWithPrevious ($ previous )
67
- {
68
- if (!$ previous ) {
69
- return $ this ;
70
- }
71
- if ($ previous instanceof DeleteOperation) {
72
- return new SetOperation ($ this ->objects );
37
+ /**
38
+ * Gets the objects for this operation.
39
+ *
40
+ * @return mixed
41
+ */
42
+ public function getValue ()
43
+ {
44
+ return $ this ->objects ;
73
45
}
74
- if ($ previous instanceof SetOperation) {
75
- $ oldList = $ previous ->getValue ();
76
- return new SetOperation (
77
- array_merge ((array )$ oldList , (array )$ this ->objects )
78
- );
46
+
47
+ /**
48
+ * Returns associative array representing encoded operation.
49
+ *
50
+ * @return array
51
+ */
52
+ public function _encode ()
53
+ {
54
+ return array ('__op ' => 'Add ' ,
55
+ 'objects ' => ParseClient::_encode ($ this ->objects , true ));
79
56
}
80
- if ($ previous instanceof AddOperation) {
81
- $ oldList = $ previous ->getValue ();
82
- return new SetOperation (
83
- array_merge ((array )$ oldList , (array )$ this ->objects )
84
- );
57
+
58
+ /**
59
+ * Takes a previous operation and returns a merged operation to replace it.
60
+ *
61
+ * @param FieldOperation $previous Previous operation.
62
+ *
63
+ * @return FieldOperation Merged operation.
64
+ * @throws ParseException
65
+ */
66
+ public function _mergeWithPrevious ($ previous )
67
+ {
68
+ if (!$ previous ) {
69
+ return $ this ;
70
+ }
71
+ if ($ previous instanceof DeleteOperation) {
72
+ return new SetOperation ($ this ->objects );
73
+ }
74
+ if ($ previous instanceof SetOperation) {
75
+ $ oldList = $ previous ->getValue ();
76
+ return new SetOperation (
77
+ array_merge ((array )$ oldList , (array )$ this ->objects )
78
+ );
79
+ }
80
+ if ($ previous instanceof AddOperation) {
81
+ $ oldList = $ previous ->getValue ();
82
+ return new SetOperation (
83
+ array_merge ((array )$ oldList , (array )$ this ->objects )
84
+ );
85
+ }
86
+ throw new ParseException (
87
+ 'Operation is invalid after previous operation. '
88
+ );
85
89
}
86
- throw new ParseException (
87
- 'Operation is invalid after previous operation. '
88
- );
89
- }
90
90
91
- /**
92
- * Applies current operation, returns resulting value.
93
- *
94
- * @param mixed $oldValue Value prior to this operation.
95
- * @param mixed $obj Value being applied.
96
- * @param string $key Key this operation affects.
97
- *
98
- * @return array
99
- */
100
- public function _apply ($ oldValue , $ obj , $ key )
101
- {
102
- if (!$ oldValue ) {
103
- return $ this ->objects ;
91
+ /**
92
+ * Applies current operation, returns resulting value.
93
+ *
94
+ * @param mixed $oldValue Value prior to this operation.
95
+ * @param mixed $obj Value being applied.
96
+ * @param string $key Key this operation affects.
97
+ *
98
+ * @return array
99
+ */
100
+ public function _apply ($ oldValue , $ obj , $ key )
101
+ {
102
+ if (!$ oldValue ) {
103
+ return $ this ->objects ;
104
+ }
105
+ return array_merge ((array )$ oldValue , (array )$ this ->objects );
104
106
}
105
- return array_merge ((array )$ oldValue , (array )$ this ->objects );
106
- }
107
107
108
- }
108
+ }
0 commit comments