Failed Conditions
Pull Request — master (#738)
by
unknown
02:18
created

OptionsManagerAbstract::setOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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