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 / class-wcpn-data.php

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

202 lines 5.0 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 public const DEFAULT_COUNTRY_CODE = "NL";
28 public const DEFAULT_CARRIER = PostNLConsignment::CARRIER_NAME;
29
30 /**
31 * @var array
32 */
33 private static $packageTypes;
34
35 /**
36 * @var array
37 */
38 private static $packageTypesHuman;
39
40 /**
41 * @var array
42 */
43 private static $deliveryTypesHuman;
44
45 public function __construct()
46 {
47 self::$packageTypes = [
48 AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME,
49 AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME,
50 AbstractConsignment::PACKAGE_TYPE_LETTER_NAME,
51 ];
52
53 self::$packageTypesHuman = [
54 AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME => __("Package", "woocommerce-postnl"),
55 AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME => __("Mailbox", "woocommerce-postnl"),
56 AbstractConsignment::PACKAGE_TYPE_LETTER_NAME => __("Unpaid letter", "woocommerce-postnl"),
57 ];
58
59 self::$deliveryTypesHuman = [
60 AbstractConsignment::DELIVERY_TYPE_MORNING_NAME => __("Morning delivery", "woocommerce-postnl"),
61 AbstractConsignment::DELIVERY_TYPE_STANDARD_NAME => __("Standard delivery", "woocommerce-postnl"),
62 AbstractConsignment::DELIVERY_TYPE_EVENING_NAME => __("Evening delivery", "woocommerce-postnl"),
63 AbstractConsignment::DELIVERY_TYPE_PICKUP_NAME => __("Pickup", "woocommerce-postnl"),
64 ];
65 }
66
67 /**
68 * @return array
69 */
70 public static function getPackageTypes(): array
71 {
72 return self::$packageTypes;
73 }
74
75 /**
76 * @return array
77 */
78 public static function getPackageTypesHuman(): array
79 {
80 return self::$packageTypesHuman;
81 }
82
83 /**
84 * @return array
85 */
86 public static function getDeliveryTypesHuman(): array
87 {
88 return self::$deliveryTypesHuman;
89 }
90
91 /**
92 * @param int|string $packageType
93 *
94 * @return string|null
95 */
96 public static function getPackageTypeHuman($packageType): ?string
97 {
98 return self::getHuman(
99 $packageType,
100 AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP,
101 self::$packageTypesHuman
102 );
103 }
104
105 /**
106 * @param string $packageType
107 *
108 * @return int|null
109 */
110 public static function getPackageTypeId(string $packageType): ?int
111 {
112 return Arr::get(AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP, $packageType, null);
113 }
114
115 /**
116 * @param int $packageType
117 *
118 * @return string|null
119 */
120 public static function getPackageTypeName(int $packageType): ?string
121 {
122 return Arr::get(array_flip(AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP), (string) $packageType, null);
123 }
124
125 /**
126 * @param string $deliveryType
127 *
128 * @return int|null
129 */
130 public static function getDeliveryTypeId(string $deliveryType): ?int
131 {
132 return Arr::get(AbstractConsignment::DELIVERY_TYPES_NAMES_IDS_MAP, $deliveryType, null);
133 }
134
135 /**
136 * @param string|int $key
137 * @param array $map
138 * @param array $humanMap
139 *
140 * @return string|null
141 */
142 private static function getHuman($key, array $map, array $humanMap): ?string
143 {
144 if (is_numeric($key)) {
145 $integerMap = array_flip($map);
146 $key = (int) $key;
147
148 if (! array_key_exists($key, $integerMap)) {
149 return null;
150 }
151
152 $key = $integerMap[$key];
153 }
154
155 return $humanMap[$key] ?? null;
156 }
157
158 /**
159 * @return array
160 */
161 public static function getInsuranceAmounts(): array
162 {
163 $amounts = [];
164
165 /**
166 * @type PostNLConsignment
167 */
168 $carrier = ConsignmentFactory::createByCarrierName(WCPOST_Settings::SETTINGS_POSTNL);
169 $amountPossibilities = $carrier::INSURANCE_POSSIBILITIES_LOCAL;
170
171 foreach ($amountPossibilities as $key => $value) {
172 $amounts[$value] = $value;
173 }
174
175 return $amounts;
176 }
177
178 /**
179 * @return array
180 */
181 public static function getCarriersHuman(): array
182 {
183 return [
184 PostNLConsignment::CARRIER_NAME => __("PostNL", "woocommerce-postnl"),
185 ];
186 }
187
188 /**
189 * Check if a given cc matches the default country code.
190 *
191 * @param string $country
192 *
193 * @return bool
194 */
195 public static function isHomeCountry(string $country): bool
196 {
197 return self::DEFAULT_COUNTRY_CODE === $country;
198 }
199 }
200
201 return new WCPN_Data();
202