| 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\Module\Checkout; |
| 14 |
|
| 15 |
/** |
| 16 |
* Options class. |
| 17 |
*/ |
| 18 |
class Options { |
| 19 |
|
| 20 |
/** |
| 21 |
* Options. |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
private $options; |
| 26 |
|
| 27 |
/** |
| 28 |
* Constructor. |
| 29 |
* |
| 30 |
* @param array $options Options. |
| 31 |
*/ |
| 32 |
public function __construct( array $options ) { |
| 33 |
$this->options = $options; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Carrier ID. |
| 38 |
* |
| 39 |
* @param string $carrierId Carrier ID. |
| 40 |
* |
| 41 |
* @return static |
| 42 |
*/ |
| 43 |
public static function createByCarrierId( string $carrierId ): self { |
| 44 |
$optionId = Checkout::CARRIER_PREFIX . $carrierId; |
| 45 |
$options = get_option( $optionId ); |
| 46 |
if ( empty( $options ) ) { |
| 47 |
$options = []; |
| 48 |
} |
| 49 |
|
| 50 |
return new self( $options ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Gets type of address validation. One of ['required', 'optional', 'none']. |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function getAddressValidation(): string { |
| 59 |
$none = 'none'; |
| 60 |
$value = $this->options['address_validation'] ?? $none; |
| 61 |
if ( $value ) { |
| 62 |
return $value; |
| 63 |
} |
| 64 |
|
| 65 |
return $none; |
| 66 |
} |
| 67 |
} |
| 68 |
|