1 <?php
2
3 namespace PayPal\Api;
4
5 use PayPal\Common\PayPalModel;
6
7 /**
8 * Class PatchRequest
9 *
10 * A JSON patch request.
11 *
12 * @package PayPal\Api
13 *
14 * @property \PayPal\Api\Patch[] patches
15 */
16 class PatchRequest extends PayPalModel
17 {
18 /**
19 * Placeholder for holding array of patch objects
20 *
21 * @param \PayPal\Api\Patch[] $patches
22 *
23 * @return $this
24 */
25 public function setPatches($patches)
26 {
27 $this->patches = $patches;
28 return $this;
29 }
30
31 /**
32 * Placeholder for holding array of patch objects
33 *
34 * @return \PayPal\Api\Patch[]
35 */
36 public function getPatches()
37 {
38 return $this->patches;
39 }
40
41 /**
42 * Append Patches to the list.
43 *
44 * @param \PayPal\Api\Patch $patch
45 * @return $this
46 */
47 public function addPatch($patch)
48 {
49 if (!$this->getPatches()) {
50 return $this->setPatches(array($patch));
51 } else {
52 return $this->setPatches(
53 array_merge($this->getPatches(), array($patch))
54 );
55 }
56 }
57
58 /**
59 * Remove Patches from the list.
60 *
61 * @param \PayPal\Api\Patch $patch
62 * @return $this
63 */
64 public function removePatch($patch)
65 {
66 return $this->setPatches(
67 array_diff($this->getPatches(), array($patch))
68 );
69 }
70
71 /**
72 * As PatchRequest holds the array of Patch object, we would override the json conversion to return
73 * a json representation of array of Patch objects.
74 *
75 * @param int $options
76 * @return mixed|string
77 */
78 public function toJSON($options = 0)
79 {
80 $json = array();
81 foreach ($this->getPatches() as $patch) {
82 $json[] = $patch->toArray();
83 }
84 return str_replace('\\/', '/', json_encode($json, $options));
85 }
86 }
87