| 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 |
* Transform shipping address to contain pickup point address? |
| 167 |
* |
| 168 |
* @return bool |
| 169 |
*/ |
| 170 |
public function replaceShippingAddressWithPickupPointAddress(): bool { |
| 171 |
return (bool) $this->get( 'replace_shipping_address_with_pickup_point_address' ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Provides available labels. |
| 176 |
* |
| 177 |
* @return array[] |
| 178 |
*/ |
| 179 |
public function getLabelFormats(): array { |
| 180 |
return [ |
| 181 |
'A6 on A4' => [ |
| 182 |
'name' => __( '1/4 A4, print on A4, 4pcs/page', 'packetery' ), |
| 183 |
'directLabels' => true, |
| 184 |
'maxOffset' => 3, |
| 185 |
], |
| 186 |
'A6 on A6' => [ |
| 187 |
'name' => __( '1/4 A4, direct print, 1pc/page', 'packetery' ), |
| 188 |
'directLabels' => true, |
| 189 |
'maxOffset' => 0, |
| 190 |
], |
| 191 |
'A7 on A7' => [ |
| 192 |
'name' => __( '1/8 A4, direct print, 1pc/page', 'packetery' ), |
| 193 |
'directLabels' => false, |
| 194 |
'maxOffset' => 0, |
| 195 |
], |
| 196 |
'A7 on A4' => [ |
| 197 |
'name' => __( '1/8 A4, print on A4, 8pcs/page', 'packetery' ), |
| 198 |
'directLabels' => false, |
| 199 |
'maxOffset' => 7, |
| 200 |
], |
| 201 |
'105x35mm on A4' => [ |
| 202 |
'name' => __( '105x35mm, print on A4, 16 pcs/page', 'packetery' ), |
| 203 |
'directLabels' => false, |
| 204 |
'maxOffset' => 15, |
| 205 |
], |
| 206 |
'A8 on A8' => [ |
| 207 |
'name' => __( '1/16 A4, direct print, 1pc/page', 'packetery' ), |
| 208 |
'directLabels' => false, |
| 209 |
'maxOffset' => 0, |
| 210 |
], |
| 211 |
]; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Gets maximum offset for selected packeta labels format. |
| 216 |
* |
| 217 |
* @param string $format Selected format. |
| 218 |
* |
| 219 |
* @return int |
| 220 |
*/ |
| 221 |
public function getLabelMaxOffset( string $format ): int { |
| 222 |
if ( '' === $format ) { |
| 223 |
return 0; |
| 224 |
} |
| 225 |
$availableFormats = $this->getLabelFormats(); |
| 226 |
|
| 227 |
return $availableFormats[ $format ]['maxOffset']; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Gets list of packeta labels for select creation. |
| 232 |
* |
| 233 |
* @return array |
| 234 |
*/ |
| 235 |
public function getPacketaLabelFormats(): array { |
| 236 |
$availableFormats = $this->getLabelFormats(); |
| 237 |
|
| 238 |
return array_filter( array_combine( array_keys( $availableFormats ), array_column( $availableFormats, 'name' ) ) ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Gets list of carrier labels for select creation. |
| 243 |
* |
| 244 |
* @return array |
| 245 |
*/ |
| 246 |
public function getCarrierLabelFormat(): array { |
| 247 |
$availableFormats = $this->getLabelFormats(); |
| 248 |
$carrierLabelFormats = []; |
| 249 |
foreach ( $availableFormats as $format => $formatData ) { |
| 250 |
if ( true === $formatData['directLabels'] ) { |
| 251 |
$carrierLabelFormats[ $format ] = $formatData['name']; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
return $carrierLabelFormats; |
| 256 |
} |
| 257 |
} |
| 258 |
|