| @@ -6,9 +6,8 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | declare( strict_types=1 ); |
| 9 | 9 | |
| 10 | - | |
| 11 | 10 | namespace Packetery\Module\Carrier; |
| 12 | 11 | |
| 13 | 12 | use Packetery\Core\Rounder; |
| 14 | 13 | |
| @@ -16,8 +15,11 @@ | ||
| 16 | 15 | * Options class. |
| 17 | 16 | */ |
| 18 | 17 | class Options { |
| 19 | 18 | |
| 19 | + public const PRICING_TYPE_BY_WEIGHT = 'byWeight'; | |
| 20 | + public const PRICING_TYPE_BY_PRODUCT_VALUE = 'byProductValue'; | |
| 21 | + | |
| 20 | 22 | /** |
| 21 | 23 | * Option ID. |
| 22 | 24 | * |
| 23 | 25 | * @var string |
| @@ -26,9 +28,9 @@ | ||
| 26 | 28 | |
| 27 | 29 | /** |
| 28 | 30 | * Options. |
| 29 | 31 | * |
| 30 | - * @var array | |
| 32 | + * @var array<string, string|bool|array<string, string|bool>> | |
| 31 | 33 | */ |
| 32 | 34 | private $options; |
| 33 | 35 | |
| 34 | 36 | /** |
| @@ -42,24 +44,8 @@ | ||
| 42 | 44 | $this->options = $options; |
| 43 | 45 | } |
| 44 | 46 | |
| 45 | 47 | /** |
| 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 | 48 | * Option ID. |
| 63 | 49 | * |
| 64 | 50 | * @return string |
| 65 | 51 | */ |
| @@ -67,20 +53,8 @@ | ||
| 67 | 53 | return $this->optionId; |
| 68 | 54 | } |
| 69 | 55 | |
| 70 | 56 | /** |
| 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 | 57 | * Returns all options as assoc array. |
| 84 | 58 | * |
| 85 | 59 | * @return array |
| 86 | 60 | */ |
| @@ -107,15 +81,9 @@ | ||
| 107 | 81 | * |
| 108 | 82 | * @return string |
| 109 | 83 | */ |
| 110 | 84 | 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; | |
| 85 | + return $this->options['address_validation'] ?? 'none'; | |
| 118 | 86 | } |
| 119 | 87 | |
| 120 | 88 | /** |
| 121 | 89 | * Gets custom carrier name. |
| @@ -183,13 +151,13 @@ | ||
| 183 | 151 | * |
| 184 | 152 | * @return bool |
| 185 | 153 | */ |
| 186 | 154 | public function hasAnyCodSurchargeSetting(): bool { |
| 187 | - if ( null !== $this->getDefaultCODSurcharge() ) { | |
| 155 | + if ( $this->getDefaultCODSurcharge() !== null ) { | |
| 188 | 156 | return true; |
| 189 | 157 | } |
| 190 | 158 | |
| 191 | - return ! empty( $this->options['surcharge_limits'] ); | |
| 159 | + return isset( $this->options['surcharge_limits'] ); | |
| 192 | 160 | } |
| 193 | 161 | |
| 194 | 162 | /** |
| 195 | 163 | * Tells COD rounding type. |
| @@ -197,6 +165,35 @@ | ||
| 197 | 165 | * @return int |
| 198 | 166 | */ |
| 199 | 167 | public function getCodRoundingType(): int { |
| 200 | 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; | |
| 201 | 198 | } |
| 202 | 199 | } |