PluginProbe
PostNL for WooCommerce / 4.0.0
PostNL for WooCommerce v4.0.0
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / includes / vendor / myparcelnl / sdk / src / Adapter / ConsignmentAdapter.php

ConsignmentAdapter.php in PostNL for WooCommerce 4.0.0, at includes/vendor/myparcelnl/sdk/src/Adapter/ConsignmentAdapter.php

235 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2 /**
3 * If you want to add improvements, please create a fork in our GitHub:
4 * https://github.com/myparcelnl
5 *
6 * @author Reindert Vetter <[email protected]>
7 * @copyright 2010-2020 MyParcel
8 * @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL
9 * @link https://github.com/myparcelnl/sdk
10 * @since File available since Release v2.0.0
11 */
12
13 namespace MyParcelNL\Sdk\src\Adapter;
14
15 use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
16 use MyParcelNL\Sdk\src\Support\Arr;
17
18 class ConsignmentAdapter
19 {
20 private $data;
21
22 /**
23 * @var AbstractConsignment
24 */
25 private $consignment;
26
27 /**
28 * ConsignmentDecode constructor.
29 *
30 * @param array $data
31 * @param \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment $consignment
32 *
33 * @throws \Exception
34 */
35 public function __construct(array $data, AbstractConsignment $consignment)
36 {
37 $this->data = $data;
38 $this->consignment = $consignment;
39
40 $this
41 ->baseOptions()
42 ->extraOptions()
43 ->recipient()
44 ->pickup();
45 }
46
47 /**
48 * @return AbstractConsignment
49 */
50 public function getConsignment()
51 {
52 return $this->consignment;
53 }
54
55 /**
56 * @return $this
57 */
58 private function baseOptions()
59 {
60 $recipient = $this->data['recipient'];
61 $options = $this->data['options'];
62
63 /** @noinspection PhpInternalEntityUsedInspection */
64 $this->consignment
65 ->setConsignmentId($this->data['id'])
66 ->setReferenceId($this->data['reference_identifier'])
67 ->setBarcode($this->data['barcode'])
68 ->setStatus($this->data['status'])
69 ->setCountry($recipient['cc'])
70 ->setPerson($recipient['person'])
71 ->setPostalCode($recipient['postal_code'])
72 ->setStreet($recipient['street'])
73 ->setCity($recipient['city'])
74 ->setEmail(isset($recipient['email']) ? $recipient['email'] : '')
75 ->setPhone(isset($recipient['phone']) ? $recipient['phone'] : '')
76 ->setPackageType($options['package_type'])
77 ->setLabelDescription(isset($options['label_description']) ? $options['label_description'] : '');
78
79 if (Arr::get($this->data, 'physical_properties.weight')) {
80 $this->consignment->setTotalWeight($this->data['physical_properties']['weight']);
81 }
82
83 return $this;
84 }
85
86 /**
87 * @return $this
88 * @throws \Exception
89 */
90 private function extraOptions()
91 {
92 $options = $this->data['options'];
93 $fields = [
94 'only_recipient' => false,
95 'large_format' => false,
96 'age_check' => false,
97 'signature' => false,
98 'return' => false,
99 'delivery_date' => null,
100 'delivery_type' => AbstractConsignment::DEFAULT_DELIVERY_TYPE,
101 ];
102 /** @noinspection PhpInternalEntityUsedInspection */
103 $this->clearFields($fields);
104
105 if (! empty($options['only_recipient'])) {
106 $this->consignment->setOnlyRecipient((bool) $options['only_recipient']);
107 }
108
109 if (! empty($options['large_format'])) {
110 $this->consignment->setLargeFormat((bool) $options['large_format']);
111 }
112
113 if (! empty($options['age_check'])) {
114 $this->consignment->setAgeCheck((bool) $options['age_check']);
115 }
116
117 if (! empty($options['signature'])) {
118 $this->consignment->setSignature((bool) $options['signature']);
119 }
120
121 if (! empty($options['return'])) {
122 $this->consignment->setReturn((bool) $options['return']);
123 }
124
125 if (! empty($options['delivery_date'])) {
126 $this->consignment->setDeliveryDate($options['delivery_date']);
127 }
128
129 if (key_exists('insurance', $options)) {
130 $insuranceAmount = $options['insurance']['amount'];
131 $this->consignment->setInsurance($insuranceAmount / 100);
132 }
133
134 if (isset($options['delivery_type'])) {
135 $this->consignment->setDeliveryType($options['delivery_type'], false);
136 }
137
138 return $this;
139 }
140
141 /**
142 * @return $this
143 */
144 private function recipient()
145 {
146 $fields = [
147 'company' => '',
148 'number' => null,
149 'number_suffix' => '',
150
151 ];
152 /** @noinspection PhpInternalEntityUsedInspection */
153 $this->clearFields($fields);
154
155 $methods = [
156 'Company' => 'company',
157 'Number' => 'number',
158 'NumberSuffix' => 'number_suffix',
159 ];
160 /** @noinspection PhpInternalEntityUsedInspection */
161 $this->setByMethods($this->data['recipient'], $methods);
162
163 return $this;
164 }
165
166 /**
167 * @return $this
168 */
169 private function pickup()
170 {
171 // Set pickup
172 if (key_exists('pickup', $this->data) && $this->data['pickup'] !== null) {
173
174 $methods = [
175 'PickupPostalCode' => 'postal_code',
176 'PickupStreet' => 'street',
177 'PickupCity' => 'city',
178 'PickupNumber' => 'number',
179 'PickupLocationName' => 'location_name',
180 'PickupLocationCode' => 'location_code',
181 'PickupNetworkId' => 'network_id',
182 ];
183 /** @noinspection PhpInternalEntityUsedInspection */
184 $this->setByMethods($this->data['pickup'], $methods);
185 } else {
186
187 $fields = [
188 'pickup_postal_code' => null,
189 'pickup_street' => null,
190 'pickup_city' => null,
191 'pickup_number' => null,
192 'pickup_location_name' => null,
193 'pickup_location_code' => '',
194 'retail_network_id' => '',
195
196 ];
197 /** @noinspection PhpInternalEntityUsedInspection */
198 $this->clearFields($fields);
199 }
200
201 return $this;
202 }
203
204 /**
205 * @param array $data
206 * @param array $methods
207 *
208 * @return $this
209 */
210 private function setByMethods($data, $methods)
211 {
212 foreach ($methods as $method => $value) {
213 if (! empty($data[$value])) {
214 $this->consignment->{'set' . $method}($data[$value]);
215 }
216 }
217
218 return $this;
219 }
220
221 /**
222 * @param $fields
223 *
224 * @return $this
225 */
226 private function clearFields($fields)
227 {
228 foreach ($fields as $field => $default) {
229 $this->consignment->{$field} = $default;
230 }
231
232 return $this;
233 }
234 }
235