Completed
Pull Request — master (#738)
by
unknown
02:20
created

OptionsManagerAbstract::addOption()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.0416
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 238
    public function __construct()
22
    {
23 238
        $this->supportedOptions = $this->getSupportedOptions();
24 238
        $this->setDefaultOptions();
25 238
    }
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 238
    public function setOption($optionName, $optionValue)
48
    {
49 238
        if (\in_array($optionName, $this->supportedOptions)) {
50 238
            $this->options[$optionName] = $optionValue;
51
        }
52 238
    }
53
54
    /**
55
     * Add an option to the internal list of options
56
     * Used only for mergeCells() for now
57
     * @param mixed $optionName
58
     * @param mixed $optionValue
59
     * @return void
60
     */
61 3
    public function addOption($optionName, $optionValue)
62
    {
63 3
        if (\in_array($optionName, $this->supportedOptions)) {
64 2
            if (!isset($this->options[$optionName])) {
65
                $this->options[$optionName] = [];
66
            }
67 2
            $this->options[$optionName][] = $optionValue;
68
        }
69 3
    }
70
71
    /**
72
     * @param string $optionName
73
     * @return mixed|null The set option or NULL if no option with given name found
74
     */
75 196
    public function getOption($optionName)
76
    {
77 196
        $optionValue = null;
78
79 196
        if (isset($this->options[$optionName])) {
80 193
            $optionValue = $this->options[$optionName];
81
        }
82
83 196
        return $optionValue;
84
    }
85
}
86