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