| 1 |
<?php |
| 2 |
/** |
| 3 |
* CarrierOptionsFactory class. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Carrier; |
| 11 |
|
| 12 |
use Packetery\Module\Framework\WpAdapter; |
| 13 |
|
| 14 |
/** |
| 15 |
* CarrierOptionsFactory class. |
| 16 |
*/ |
| 17 |
class CarrierOptionsFactory { |
| 18 |
|
| 19 |
/** |
| 20 |
* WP adapter. |
| 21 |
* |
| 22 |
* @var WpAdapter |
| 23 |
*/ |
| 24 |
private $wpAdapter; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
* |
| 29 |
* @param WpAdapter $wpAdapter WP adapter. |
| 30 |
*/ |
| 31 |
public function __construct( WpAdapter $wpAdapter ) { |
| 32 |
$this->wpAdapter = $wpAdapter; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Creates carrier options by option id. |
| 37 |
* |
| 38 |
* @param string $optionId Option id. |
| 39 |
* |
| 40 |
* @return Options |
| 41 |
*/ |
| 42 |
public function createByOptionId( string $optionId ): Options { |
| 43 |
$options = $this->wpAdapter->getOption( $optionId ); |
| 44 |
if ( ! isset( $options ) || $options === false ) { |
| 45 |
$options = []; |
| 46 |
} |
| 47 |
|
| 48 |
return new Options( $optionId, $options ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Creates instance by carrier ID. |
| 53 |
* |
| 54 |
* @param string $carrierId Carrier ID. |
| 55 |
* |
| 56 |
* @return Options |
| 57 |
*/ |
| 58 |
public function createByCarrierId( string $carrierId ): Options { |
| 59 |
$optionId = OptionPrefixer::getOptionId( $carrierId ); |
| 60 |
|
| 61 |
return $this->createByOptionId( $optionId ); |
| 62 |
} |
| 63 |
} |
| 64 |
|