| 1 |
<?php |
| 2 |
/** |
| 3 |
* Options class. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Carrier; |
| 11 |
|
| 12 |
use Packetery\Core\Rounder; |
| 13 |
|
| 14 |
/** |
| 15 |
* Options class. |
| 16 |
*/ |
| 17 |
class Options { |
| 18 |
|
| 19 |
public const PRICING_TYPE_BY_WEIGHT = 'byWeight'; |
| 20 |
public const PRICING_TYPE_BY_PRODUCT_VALUE = 'byProductValue'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Option ID. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
private $optionId; |
| 28 |
|
| 29 |
/** |
| 30 |
* Options. |
| 31 |
* |
| 32 |
* @var array<string, string|bool|array<string, string|bool>> |
| 33 |
*/ |
| 34 |
private $options; |
| 35 |
|
| 36 |
/** |
| 37 |
* Constructor. |
| 38 |
* |
| 39 |
* @param string $optionId Option ID. |
| 40 |
* @param array $options Options. |
| 41 |
*/ |
| 42 |
public function __construct( string $optionId, array $options ) { |
| 43 |
$this->optionId = $optionId; |
| 44 |
$this->options = $options; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Option ID. |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function getOptionId(): string { |
| 53 |
return $this->optionId; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns all options as assoc array. |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public function toArray(): array { |
| 62 |
return $this->options; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Age verification fee. |
| 67 |
* |
| 68 |
* @return float|null |
| 69 |
*/ |
| 70 |
public function getAgeVerificationFee(): ?float { |
| 71 |
$value = $this->options['age_verification_fee'] ?? null; |
| 72 |
if ( is_numeric( $value ) ) { |
| 73 |
return (float) $value; |
| 74 |
} |
| 75 |
|
| 76 |
return null; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Gets type of address validation. One of ['required', 'optional', 'none']. |
| 81 |
* |
| 82 |
* @return string |
| 83 |
*/ |
| 84 |
public function getAddressValidation(): string { |
| 85 |
return $this->options['address_validation'] ?? 'none'; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Gets custom carrier name. |
| 90 |
* |
| 91 |
* @return string|null |
| 92 |
*/ |
| 93 |
public function getName(): ?string { |
| 94 |
return ( $this->options['name'] ?? null ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Tells if carrier is active. |
| 99 |
* |
| 100 |
* @return bool |
| 101 |
*/ |
| 102 |
public function isActive(): bool { |
| 103 |
return $this->options['active'] ?? false; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Tells if carrier has coupon free shipping active. |
| 108 |
* |
| 109 |
* @return bool |
| 110 |
*/ |
| 111 |
public function hasCouponFreeShippingActive(): bool { |
| 112 |
return $this->options['coupon_free_shipping']['active'] ?? false; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Tells if carrier has coupon free shipping for fees allowed. |
| 117 |
* |
| 118 |
* @return bool |
| 119 |
*/ |
| 120 |
public function hasCouponFreeShippingForFeesAllowed(): bool { |
| 121 |
return $this->hasCouponFreeShippingActive() && ( $this->options['coupon_free_shipping']['allow_for_fees'] ?? false ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Tells if carrier has payment method disallowed. |
| 126 |
* |
| 127 |
* @param string $method Payment method. |
| 128 |
* |
| 129 |
* @return bool |
| 130 |
*/ |
| 131 |
public function hasCheckoutPaymentMethodDisallowed( string $method ): bool { |
| 132 |
return in_array( $method, $this->options['disallowed_checkout_payment_methods'] ?? [], true ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Gets default COD surcharge. |
| 137 |
* |
| 138 |
* @return float|null |
| 139 |
*/ |
| 140 |
public function getDefaultCODSurcharge(): ?float { |
| 141 |
$value = $this->options['default_COD_surcharge'] ?? null; |
| 142 |
if ( is_numeric( $value ) ) { |
| 143 |
return (float) $value; |
| 144 |
} |
| 145 |
|
| 146 |
return null; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Tells if any COD surcharge was configured. |
| 151 |
* |
| 152 |
* @return bool |
| 153 |
*/ |
| 154 |
public function hasAnyCodSurchargeSetting(): bool { |
| 155 |
if ( $this->getDefaultCODSurcharge() !== null ) { |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
return isset( $this->options['surcharge_limits'] ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Tells COD rounding type. |
| 164 |
* |
| 165 |
* @return int |
| 166 |
*/ |
| 167 |
public function getCodRoundingType(): int { |
| 168 |
return $this->options['cod_rounding'] ?? Rounder::DONT_ROUND; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Gets pricing type. |
| 173 |
* |
| 174 |
* @return string |
| 175 |
*/ |
| 176 |
public function getPricingType(): string { |
| 177 |
return $this->options[ OptionsPage::FORM_FIELD_PRICING_TYPE ] ?? self::PRICING_TYPE_BY_WEIGHT; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Checks if carrier has saved options. |
| 182 |
* |
| 183 |
* @return bool |
| 184 |
*/ |
| 185 |
public function hasOptions(): bool { |
| 186 |
return count( $this->options ) > 0; |
| 187 |
} |
| 188 |
|
| 189 |
public function getSizeRestrictions(): ?array { |
| 190 |
if ( |
| 191 |
isset( $this->options['dimensions_restrictions']['active'] ) && |
| 192 |
$this->options['dimensions_restrictions']['active'] === true |
| 193 |
) { |
| 194 |
return $this->options['dimensions_restrictions']; |
| 195 |
} |
| 196 |
|
| 197 |
return null; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* @return string[]|null |
| 202 |
*/ |
| 203 |
public function getVendorGroups(): ?array { |
| 204 |
return ( isset( $this->options['vendor_groups'] ) && is_array( $this->options['vendor_groups'] ) ) ? |
| 205 |
$this->options['vendor_groups'] : null; |
| 206 |
} |
| 207 |
} |
| 208 |
|