Completed
Push — master ( b28ce7...3bde8c )
by Bjørn
02:58
created

Options::setOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 4
cts 6
cp 0.6667
crap 2.1481
rs 10
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 35
    public function addOption($option)
28
    {
29 35
        $this->options[$option->getId()] = $option;
30 35
    }
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 35
     public function addOptions()
41
     {
42 35
         $options = func_get_args();
43 35
         foreach ($options as $option) {
44 35
             $this->addOption($option);
45
         }
46 35
     }
47
     /*
48
     In some years, we can use the splat instead (requires PHP 5.6):
49
    public function addOptions(...$options)
50
    {
51
        foreach ($options as $option) {
52
            $this->addOption($option);
53
        }
54
    }*/
55
56
    /**
57
     * Set the value of an option.
58
     *
59
     * @param  string  $id      Id of the option
60
     * @param  mixed   $value   Value of the option
61
     * @return void
62
     */
63 21
    public function setOption($id, $value)
64
    {
65 21
        if (!isset($this->options[$id])) {
66
            throw new OptionNotFoundException(
67
                'Could not set option. There is no option called "' . $id . '" in the collection.'
68
            );
69
        }
70 21
        $option = $this->options[$id];
71 21
        $option->setValue($value);
72 21
    }
73
74
    /**
75
     * Set option, or create a new, if no such option exists.
76
     *
77
     * @param  string  $id  Id of option to set/create
78
     * @param  mixed  $value  Value of option
79
     * @return void
80
     */
81 21
    public function setOrCreateOption($id, $value)
82
    {
83 21
        if (!isset($this->options[$id])) {
84 1
            $newOption = new GhostOption($id, null);
85 1
            $newOption->setValue($value);
86
            //$newOption = new Option($id, $value);
87 1
            $this->addOption($newOption);
88
        } else {
89 21
            $this->setOption($id, $value);
90
        }
91 21
    }
92
93
    /**
94
     * Get the value of an option in the collection - by id.
95
     *
96
     * @param  string  $id      Id of the option to get
97
     * @throws  OptionNotFoundException  if the option is not in the collection
98
     * @return mixed  The value of the option
99
     */
100
    public function getOption($id)
101
    {
102
        if (!isset($this->options[$id])) {
103
            throw new OptionNotFoundException(
104
                'There is no option called "' . $id . '" in the collection.'
105
            );
106
        }
107
        $option = $this->options[$id];
108
        return $option->getValue();
109
    }
110
111
    /**
112
     * Return map of option objects.
113
     *
114
     * @return array  map of option objects
115
     */
116 2
    public function getOptionsMap()
117
    {
118 2
        return $this->options;
119
    }
120
121
    /**
122
     * Return flat associative array of options.
123
     *
124
     * @return array  associative array of options
125
     */
126 35
    public function getOptions()
127
    {
128 35
        $values = [];
129 35
        foreach ($this->options as $id => $option) {
130 35
            $values[$id] = $option->getValue();
131
        }
132 35
        return $values;
133
    }
134
135
    /**
136
     * Check all options in the collection.
137
     */
138 17
    public function check()
139
    {
140 17
        foreach ($this->options as $id => $option) {
141 17
            $option->check();
142
        }
143 17
    }
144
}
145