PluginProbe
PostNL for WooCommerce / 4.4.1
PostNL for WooCommerce v4.4.1
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 / class-wcpn-data.php

class-wcpn-data.php in PostNL for WooCommerce 4.4.1, at includes/class-wcpn-data.php

245 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use MyParcelNL\Sdk\src\Factory\ConsignmentFactory;
4 use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
5 use MyParcelNL\Sdk\src\Model\Consignment\PostNLConsignment;
6 use MyParcelNL\Sdk\src\Support\Arr;
7
8 if (! defined('ABSPATH')) {
9 exit;
10 } // Exit if accessed directly
11
12 if (class_exists('WCPN_Data')) {
13 return new WCPN_Data();
14 }
15
16 class WCPN_Data
17 {
18 public const API_URL = "https://api.myparcel.nl/";
19
20 /**
21 * @var array
22 */
23 public const CARRIERS_HUMAN = [
24 PostNLConsignment::CARRIER_NAME => 'PostNL',
25 ];
26
27 /**
28 * @var array
29 */
30 public const DIGITAL_STAMP_RANGES = [
31 [
32 'min' => 0,
33 'max' => 20,
34 'average' => 15
35 ],
36 [
37 'min' => 20,
38 'max' => 50,
39 'average' => 35
40 ],
41 [
42 'min' => 50,
43 'max' => 100,
44 'average' => 75
45 ],
46 [
47 'min' => 100,
48 'max' => 350,
49 'average' => 225
50 ],
51 [
52 'min' => 350,
53 'max' => 2000,
54 'average' => 1175
55 ],
56 ];
57
58 public const DEFAULT_COUNTRY_CODE = "NL";
59 public const DEFAULT_CARRIER = PostNLConsignment::CARRIER_NAME;
60
61 /**
62 * @var array
63 */
64 private static $packageTypes;
65
66 /**
67 * @var array
68 */
69 private static $packageTypesHuman;
70
71 /**
72 * @var array
73 */
74 private static $deliveryTypesHuman;
75
76 public function __construct()
77 {
78 self::$packageTypes = [
79 AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME,
80 AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME,
81 AbstractConsignment::PACKAGE_TYPE_LETTER_NAME,
82 ];
83
84 self::$packageTypesHuman = [
85 AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME => __("Package", "woocommerce-postnl"),
86 AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME => __("Mailbox", "woocommerce-postnl"),
87 AbstractConsignment::PACKAGE_TYPE_LETTER_NAME => __("Unpaid letter", "woocommerce-postnl"),
88 ];
89
90 self::$deliveryTypesHuman = [
91 AbstractConsignment::DELIVERY_TYPE_MORNING_NAME => __("Morning delivery", "woocommerce-postnl"),
92 AbstractConsignment::DELIVERY_TYPE_STANDARD_NAME => __("Standard delivery", "woocommerce-postnl"),
93 AbstractConsignment::DELIVERY_TYPE_EVENING_NAME => __("Evening delivery", "woocommerce-postnl"),
94 AbstractConsignment::DELIVERY_TYPE_PICKUP_NAME => __("Pickup", "woocommerce-postnl"),
95 ];
96 }
97
98 /**
99 * @return array
100 */
101 public static function getPackageTypes(): array
102 {
103 return self::$packageTypes;
104 }
105
106 /**
107 * @return array
108 */
109 public static function getPackageTypesHuman(): array
110 {
111 return self::$packageTypesHuman;
112 }
113
114 /**
115 * @return array
116 */
117 public static function getDeliveryTypesHuman(): array
118 {
119 return self::$deliveryTypesHuman;
120 }
121
122 /**
123 * @return array
124 */
125 public static function getDigitalStampRanges(): array
126 {
127 return self::DIGITAL_STAMP_RANGES;
128 }
129
130 /**
131 * @param int|string $packageType
132 *
133 * @return string|null
134 */
135 public static function getPackageTypeHuman($packageType): ?string
136 {
137 return self::getHuman(
138 $packageType,
139 AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP,
140 self::$packageTypesHuman
141 );
142 }
143
144 /**
145 * @param string $packageType
146 *
147 * @return int|null
148 */
149 public static function getPackageTypeId(string $packageType): ?int
150 {
151 return Arr::get(AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP, $packageType, null);
152 }
153
154 /**
155 * @param int $packageType
156 *
157 * @return string|null
158 */
159 public static function getPackageTypeName(int $packageType): ?string
160 {
161 return Arr::get(array_flip(AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP), (string) $packageType, null);
162 }
163
164 /**
165 * @param string|null $deliveryType
166 *
167 * @return int|null
168 */
169 public static function getDeliveryTypeId(?string $deliveryType): ?int
170 {
171 if (! $deliveryType) {
172 $deliveryType = AbstractConsignment::DELIVERY_TYPE_STANDARD;
173 }
174
175 return Arr::get(AbstractConsignment::DELIVERY_TYPES_NAMES_IDS_MAP, $deliveryType, null);
176 }
177
178 /**
179 * @param string|int $key
180 * @param array $map
181 * @param array $humanMap
182 *
183 * @return string|null
184 */
185 private static function getHuman($key, array $map, array $humanMap): ?string
186 {
187 if (is_numeric($key)) {
188 $integerMap = array_flip($map);
189 $key = (int) $key;
190
191 if (! array_key_exists($key, $integerMap)) {
192 return null;
193 }
194
195 $key = $integerMap[$key];
196 }
197
198 return $humanMap[$key] ?? null;
199 }
200
201 /**
202 * @return array
203 */
204 public static function getInsuranceAmounts(): array
205 {
206 $amounts = [];
207
208 /**
209 * @type PostNLConsignment
210 */
211 $carrier = ConsignmentFactory::createByCarrierName(WCPOST_Settings::SETTINGS_POSTNL);
212 $amountPossibilities = $carrier::INSURANCE_POSSIBILITIES_LOCAL;
213
214 foreach ($amountPossibilities as $key => $value) {
215 $amounts[$value] = $value;
216 }
217
218 return $amounts;
219 }
220
221 /**
222 * @return array
223 */
224 public static function getCarriersHuman(): array
225 {
226 return [
227 PostNLConsignment::CARRIER_NAME => __("PostNL", "woocommerce-postnl"),
228 ];
229 }
230
231 /**
232 * Check if a given cc matches the default country code.
233 *
234 * @param string $country
235 *
236 * @return bool
237 */
238 public static function isHomeCountry(string $country): bool
239 {
240 return self::DEFAULT_COUNTRY_CODE === $country;
241 }
242 }
243
244 return new WCPN_Data();
245