ParameterHolder::hasParameter()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of the Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2017 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation;
11
12
use PommProject\Foundation\Exception\FoundationException;
13
14
/**
15
 * ParameterHolder
16
 *
17
 * @package   Foundation
18
 * @copyright 2014 - 2017 Grégoire HUBERT
19
 * @author    Grégoire HUBERT <[email protected]>
20
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
21
 */
22
class ParameterHolder implements \ArrayAccess, \IteratorAggregate, \Countable
23
{
24
    protected $parameters;
25
26
    /**
27
     * __construct()
28
     *
29
     * @param  array $parameters (optional)
30
     */
31
    public function __construct(array $parameters = [])
32
    {
33
        $this->parameters = $parameters;
34
    }
35
36
    /**
37
     * setParameter
38
     *
39
     * Set a parameter.
40
     *
41
     * @param  string          $name
42
     * @param  string|array    $value
43
     * @return ParameterHolder $this
44
     */
45
    public function setParameter($name, $value)
46
    {
47
        $this->parameters[$name] = $value;
48
49
        return $this;
50
    }
51
52
    /**
53
     * hasParameter
54
     *
55
     * check if the given parameter exists.
56
     *
57
     * @param  string $name
58
     * @return bool
59
     */
60
    public function hasParameter($name)
61
    {
62
        return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters);
63
    }
64
65
    /**
66
     * getParameter
67
     *
68
     * Returns the parameter "name" or "default" if not set.
69
     *
70
     * @param  string       $name
71
     * @param  string       $default Optional default value if name not set.
72
     * @return string|array Parameter's value or default.
73
     */
74
    public function getParameter($name, $default = null)
75
    {
76
        return $this->hasParameter($name) ? $this->parameters[$name] : $default;
77
    }
78
79
    /**
80
     * mustHave()
81
     *
82
     * Throw an exception if a param is not set
83
     *
84
     * @throws  FoundationException
85
     * @param  string          $name the parameter's name
86
     * @return ParameterHolder $this
87
     */
88
    public function mustHave($name)
89
    {
90
        if (!$this->hasParameter($name)) {
91
            throw new FoundationException(sprintf('The parameter "%s" is mandatory.', $name));
92
        }
93
94
        return $this;
95
    }
96
97
    /**
98
     * setDefaultValue()
99
     *
100
     * Sets a default value if the param $name is not set
101
     *
102
     * @param  string          $name  the parameter's name
103
     * @param  mixed           $value the default value
104
     * @return ParameterHolder $this
105
     */
106
    public function setDefaultValue($name, $value)
107
    {
108
        if (!$this->hasParameter($name)) {
109
            $this->setParameter($name, $value);
110
        }
111
112
        return $this;
113
    }
114
115
    /**
116
     * mustBeOneOf()
117
     *
118
     * Check if the given parameter is one of the values passed as argument. If
119
     * not, an exception is thrown.
120
     *
121
     * @throws  FoundationException
122
     * @param  string          $name   the parameter's name
123
     * @param  array           $values
124
     * @return ParameterHolder $this
125
     */
126
    public function mustBeOneOf($name, array $values)
127
    {
128
        if (!in_array($this[$name], $values)) {
129
            throw new FoundationException(
130
                sprintf('The parameters "%s" must be one of [%s].', $name, implode(', ', $values))
131
            );
132
        }
133
134
        return $this;
135
    }
136
137
    /**
138
     * unsetParameter()
139
     *
140
     * @param  string          $name
141
     * @return ParameterHolder $this
142
     */
143
    public function unsetParameter($name)
144
    {
145
        unset($this->parameters[$name]);
146
147
        return $this;
148
    }
149
150
    /**
151
     * offsetExists()
152
     *
153
     * @see ArrayAccess
154
     */
155
    public function offsetExists($name)
156
    {
157
        return $this->hasParameter($name);
158
    }
159
160
    /**
161
     * offsetGet()
162
     *
163
     * @see ArrayAccess
164
     */
165
    public function offsetGet($name)
166
    {
167
        return $this->getParameter($name);
168
    }
169
170
    /**
171
     * offsetSet()
172
     *
173
     * @see ArrayAccess
174
     */
175
    public function offsetSet($name, $value)
176
    {
177
        $this->setParameter($name, $value);
178
    }
179
180
    /**
181
     * offsetUnset()
182
     *
183
     * @see ArrayAccess
184
     */
185
    public function offsetUnset($name)
186
    {
187
        $this->unsetParameter($name);
188
    }
189
190
    /**
191
     *
192
     * @see \Countable
193
     */
194
    public function count()
195
    {
196
        return count($this->parameters);
197
    }
198
199
    /**
200
     * getIterator()
201
     *
202
     * @see \IteratorAggregate
203
     */
204
    public function getIterator()
205
    {
206
        return new \ArrayIterator($this->parameters);
207
    }
208
}
209