PluginProbe
Packeta / 1.2.2
Packeta v1.2.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Options / Provider.php

Provider.php in Packeta 1.2.2, at src/Packetery/Module/Options/Provider.php

249 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Provider
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Options;
11
12 /**
13 * Class Provider
14 *
15 * @package Packetery
16 */
17 class Provider {
18
19 const MAX_STATUS_SYNCING_PACKETS_DEFAULT = 100;
20
21 /**
22 * Options data.
23 *
24 * @var array
25 */
26 private $data;
27
28 /**
29 * Provider constructor.
30 */
31 public function __construct() {
32 $data = get_option( 'packetery' );
33 if ( ! $data ) {
34 $data = array();
35 }
36
37 $this->data = $data;
38 }
39
40 /**
41 * Casts data to array.
42 *
43 * @return array Data.
44 */
45 public function data_to_array(): array {
46 return $this->data;
47 }
48
49 /**
50 * Casts data to array.
51 *
52 * @return bool Has any data.
53 */
54 public function has_any(): bool {
55 return ! empty( $this->data );
56 }
57
58 /**
59 * Gets content from options array.
60 *
61 * @param string $key Options array key.
62 *
63 * @return mixed|null Content.
64 */
65 private function get( string $key ) {
66 return ( $this->data[ $key ] ?? null );
67 }
68
69 /**
70 * API key dynamically crafted from API password.
71 *
72 * @return string|null Content.
73 */
74 public function get_api_key(): ?string {
75 return $this->get( 'api_key' );
76 }
77
78 /**
79 * API password from client section.
80 *
81 * @return string|null Content.
82 */
83 public function get_api_password(): ?string {
84 return $this->get( 'api_password' );
85 }
86
87 /**
88 * Sender.
89 *
90 * @return string|null Content.
91 */
92 public function get_sender(): ?string {
93 return $this->get( 'sender' );
94 }
95
96 /**
97 * Carrier label format.
98 *
99 * @return string|null Content.
100 */
101 public function get_carrier_label_format(): ?string {
102 return $this->get( 'carrier_label_format' );
103 }
104
105 /**
106 * Packeta label format.
107 *
108 * @return string|null Content.
109 */
110 public function get_packeta_label_format(): ?string {
111 return $this->get( 'packeta_label_format' );
112 }
113
114 /**
115 * Does user allow label emailing?
116 *
117 * @return bool|null Content.
118 */
119 public function get_allow_label_emailing(): ?bool {
120 return (bool) $this->get( 'allow_label_emailing' );
121 }
122
123 /**
124 * Which payment rate id COD?
125 *
126 * @return string|null Content.
127 */
128 public function getCodPaymentMethod(): ?string {
129 $value = $this->get( 'cod_payment_method' );
130 if ( ! $value ) {
131 return null;
132 }
133
134 return $value;
135 }
136
137 /**
138 * Order packaging weight.
139 *
140 * @return float
141 */
142 public function getPackagingWeight(): float {
143 $value = $this->get( 'packaging_weight' );
144 if ( is_numeric( $value ) ) {
145 return (float) $value;
146 }
147
148 return 0.0;
149 }
150
151 /**
152 * Max syncing packets.
153 *
154 * @return int
155 */
156 public function getMaxStatusSyncingPackets(): int {
157 $value = $this->get( 'max_status_syncing_packets' );
158 if ( is_numeric( $value ) ) {
159 return (int) $value;
160 }
161
162 return self::MAX_STATUS_SYNCING_PACKETS_DEFAULT;
163 }
164
165 /**
166 * Provides available labels.
167 *
168 * @return array[]
169 */
170 public function getLabelFormats(): array {
171 return [
172 'A6 on A4' => [
173 'name' => __( 'labelNameA6onA4', 'packetery' ),
174 'directLabels' => true,
175 'maxOffset' => 3,
176 ],
177 'A6 on A6' => [
178 'name' => __( 'labelNameA6onA6', 'packetery' ),
179 'directLabels' => true,
180 'maxOffset' => 0,
181 ],
182 'A7 on A7' => [
183 'name' => __( 'labelNameA7onA7', 'packetery' ),
184 'directLabels' => false,
185 'maxOffset' => 0,
186 ],
187 'A7 on A4' => [
188 'name' => __( 'labelNameA7onA4', 'packetery' ),
189 'directLabels' => false,
190 'maxOffset' => 7,
191 ],
192 '105x35mm on A4' => [
193 'name' => __( 'labelName105x35onA4', 'packetery' ),
194 'directLabels' => false,
195 'maxOffset' => 15,
196 ],
197 'A8 on A8' => [
198 'name' => __( 'labelNameA8onA8', 'packetery' ),
199 'directLabels' => false,
200 'maxOffset' => 0,
201 ],
202 ];
203 }
204
205 /**
206 * Gets maximum offset for selected packeta labels format.
207 *
208 * @param string $format Selected format.
209 *
210 * @return int
211 */
212 public function getLabelMaxOffset( string $format ): int {
213 if ( '' === $format ) {
214 return 0;
215 }
216 $availableFormats = $this->getLabelFormats();
217
218 return $availableFormats[ $format ]['maxOffset'];
219 }
220
221 /**
222 * Gets list of packeta labels for select creation.
223 *
224 * @return array
225 */
226 public function getPacketaLabelFormats(): array {
227 $availableFormats = $this->getLabelFormats();
228
229 return array_filter( array_combine( array_keys( $availableFormats ), array_column( $availableFormats, 'name' ) ) );
230 }
231
232 /**
233 * Gets list of carrier labels for select creation.
234 *
235 * @return array
236 */
237 public function getCarrierLabelFormat(): array {
238 $availableFormats = $this->getLabelFormats();
239 $carrierLabelFormats = [];
240 foreach ( $availableFormats as $format => $formatData ) {
241 if ( true === $formatData['directLabels'] ) {
242 $carrierLabelFormats[ $format ] = $formatData['name'];
243 }
244 }
245
246 return $carrierLabelFormats;
247 }
248 }
249