Test Failed
Push — master ( 975525...307699 )
by Bjørn
02:39
created

Options::setOrCreateOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebPConvert\Options;
4
5
use WebPConvert\Options\Option;
6
use WebPConvert\Options\Exceptions\OptionNotFoundException;
7
8
/**
9
 * Handles a collection of options.
10
 *
11
 * @package    WebPConvert
12
 * @author     Bjørn Rosell <[email protected]>
13
 * @since      Class available since Release 2.0.0
14
 */
15
class Options
16
{
17
18
    /** @var  array  A map of options, keyed by their id */
19
    private $options = [];
20
21
    /**
22
     * Add option.
23
     *
24
     * @param  Option  $option  The option object to add to collection.
25
     * @return void
26
     */
27
    public function addOption($option)
28
    {
29
        $this->options[$option->getId()] = $option;
30
    }
31
32
    /**
33
     * Add options.
34
     *
35
     * Conveniently add several options in one call.
36
     *
37
     * @param  Option[]  ...$options  Array of options objects to add
38
     * @return void
39
     */
40
    public function addOptions(...$options)
41
    {
42
        foreach ($options as $option) {
43
            $this->addOption($option);
44
        }
45
    }
46
47
    /**
48
     * Set the value of an option.
49
     *
50
     * @param  string  $id      Id of the option
51
     * @param  mixed   $value   Value of the option
52
     * @return void
53
     */
54
    public function setOption($id, $value)
55
    {
56
        $option = $this->options[$id];
57
        $option->setValue($value);
58
    }
59
60
    /**
61
     * Set option, or create a new, if no such option exists.
62
     *
63
     * @param  string  $id  Id of option to set/create
64
     * @param  mixed  $value  Value of option
65
     * @return void
66
     */
67
    public function setOrCreateOption($id, $value)
68
    {
69
        if (!isset($this->options[$id])) {
70
            $newOption = new Option($id, null);
71
            $newOption->setValue($value);
72
            $this->addOption($newOption);
73
        } else {
74
            $this->setOption($id, $value);
75
        }
76
    }
77
78
    /**
79
     * Get the value of an option in the collection - by id.
80
     *
81
     * @param  string  $id      Id of the option to get
82
     * @throws  OptionNotFoundException  if the option is not in the collection
83
     * @return mixed  The value of the option
84
     */
85
    public function getOption($id)
86
    {
87
        if (!isset($this->options[$id])) {
88
            throw new OptionNotFoundException(
89
                'There is no option called "' . $id . '" in the collection.'
90
            );
91
        }
92
        $option = $this->options[$id];
93
        return $option->getValue();
94
    }
95
96
    /**
97
     * Return flat associative array of options.
98
     *
99
     * @return array  associative array of options
100
     */
101
    public function getOptions()
102
    {
103
        $values = [];
104
        foreach ($this->options as $id => $option) {
105
            $values[$id] = $option->getValue();
106
        }
107
        return $values;
108
    }
109
110
    /**
111
     * Check all options in the collection.
112
     */
113
    public function check()
114
    {
115
        foreach ($this->options as $id => $option) {
116
            $option->check();
117
        }
118
    }
119
}
120