| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\OpenSpout\Common\Manager; |
| 4 |
|
| 5 |
abstract class OptionsManagerAbstract implements OptionsManagerInterface |
| 6 |
{ |
| 7 |
public const PREFIX_OPTION = 'OPTION_'; |
| 8 |
/** @var string[] List of all supported option names */ |
| 9 |
private $supportedOptions = []; |
| 10 |
/** @var array Associative array [OPTION_NAME => OPTION_VALUE] */ |
| 11 |
private $options = []; |
| 12 |
/** |
| 13 |
* OptionsManagerAbstract constructor. |
| 14 |
*/ |
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
$this->supportedOptions = $this->getSupportedOptions(); |
| 18 |
$this->setDefaultOptions(); |
| 19 |
} |
| 20 |
/** |
| 21 |
* Sets the given option, if this option is supported. |
| 22 |
* |
| 23 |
* @param string $optionName |
| 24 |
* @param mixed $optionValue |
| 25 |
*/ |
| 26 |
public function setOption($optionName, $optionValue) |
| 27 |
{ |
| 28 |
if (\in_array($optionName, $this->supportedOptions, \true)) { |
| 29 |
$this->options[$optionName] = $optionValue; |
| 30 |
} |
| 31 |
} |
| 32 |
/** |
| 33 |
* Add an option to the internal list of options |
| 34 |
* Used only for mergeCells() for now. |
| 35 |
* |
| 36 |
* @param mixed $optionName |
| 37 |
* @param mixed $optionValue |
| 38 |
*/ |
| 39 |
public function addOption($optionName, $optionValue) |
| 40 |
{ |
| 41 |
if (\in_array($optionName, $this->supportedOptions, \true)) { |
| 42 |
if (!isset($this->options[$optionName])) { |
| 43 |
$this->options[$optionName] = []; |
| 44 |
} elseif (!\is_array($this->options[$optionName])) { |
| 45 |
$this->options[$optionName] = [$this->options[$optionName]]; |
| 46 |
} |
| 47 |
$this->options[$optionName][] = $optionValue; |
| 48 |
} |
| 49 |
} |
| 50 |
/** |
| 51 |
* @param string $optionName |
| 52 |
* |
| 53 |
* @return null|mixed The set option or NULL if no option with given name found |
| 54 |
*/ |
| 55 |
public function getOption($optionName) |
| 56 |
{ |
| 57 |
$optionValue = null; |
| 58 |
if (isset($this->options[$optionName])) { |
| 59 |
$optionValue = $this->options[$optionName]; |
| 60 |
} |
| 61 |
return $optionValue; |
| 62 |
} |
| 63 |
/** |
| 64 |
* @return array List of supported options |
| 65 |
*/ |
| 66 |
protected abstract function getSupportedOptions(); |
| 67 |
/** |
| 68 |
* Sets the default options. |
| 69 |
* To be overriden by child classes. |
| 70 |
*/ |
| 71 |
protected abstract function setDefaultOptions(); |
| 72 |
} |
| 73 |
|