Parameter::__set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2.0185
1
<?php
2
3
/*
4
 * This file is part of the GestPayWS library.
5
 *
6
 * (c) Manuel Dalla Lana <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EndelWar\GestPayWS\Parameter;
13
14
use InvalidArgumentException;
15
16
/**
17
 * Class Parameter
18
 * @package EndelWar\GestPayWS\Parameter
19
 */
20
abstract class Parameter implements \ArrayAccess
21
{
22
    protected $parameters = array();
23
    protected $parametersName = array();
24
    protected $mandatoryParameters = array();
25
26
    /**
27
     * @param array $parameters
28
     */
29 38
    public function __construct(array $parameters = array())
30
    {
31 38
        foreach ($this->parametersName as $parameterName) {
32 38
            $this->set($parameterName, null);
33 38
        }
34 38
        if (!empty($parameters)) {
35 9
            $this->fromArray($parameters);
36 9
        }
37 38
    }
38
39
    /**
40
     * @param string $key
41
     * @param mixed $value
42
     */
43 38 View Code Duplication
    public function set($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45 38
        if (!in_array($key, $this->parametersName, true)) {
46 1
            throw new InvalidArgumentException(sprintf('%s is not a valid parameter name.', $key));
47
        }
48 38
        $this->parameters[$key] = $value;
49 38
    }
50
51
    /**
52
     * @param string $key
53
     *
54
     * @return mixed|null The value at the specified index or null.
55
     */
56 16
    public function get($key)
57
    {
58 16
        if (array_key_exists($key, $this->parameters)) {
59 13
            return $this->parameters[$key];
60
        }
61
62 3
        return;
63
    }
64
65
    /**
66
     * @param array $array
67
     */
68 11
    public function fromArray($array)
69
    {
70 11
        foreach ($array as $key => $value) {
71 11
            $this->set($key, $value);
72 11
        }
73 11
    }
74
75
    /**
76
     * @return array
77
     */
78 3
    public function toArray()
79
    {
80 3
        return $this->parameters;
81
    }
82
83
    /**
84
     * @return bool
85
     */
86 6
    public function areAllMandatoryParametersSet()
87
    {
88 6
        foreach ($this->mandatoryParameters as $param) {
89 6
            if (!isset($this->$param)) {
90 3
                return false;
91
            }
92 3
        }
93
94 3
        return true;
95
    }
96
97
    /**
98
     * Returns whether the requested index exists
99
     *
100
     * @param string $key
101
     *
102
     * @return bool
103
     */
104 1
    public function offsetExists($key)
105
    {
106 1
        return array_key_exists($key, $this->parameters);
107
    }
108
109
    /**
110
     * Returns the value at the specified index
111
     *
112
     * @param string $key The index with the value.
113
     *
114
     * @return mixed|null The value at the specified index or null.
115
     */
116 4
    public function offsetGet($key)
117
    {
118 4
        return $this->get($key);
119
    }
120
121
    /**
122
     * Sets the value at the specified index to $value
123
     *
124
     * @param string $key The index being set.
125
     * @param mixed $value The new value for the index
126
     */
127 2
    public function offsetSet($key, $value)
128
    {
129 2
        $this->set($key, $value);
130 2
    }
131
132
    /**
133
     * Unsets the value at the specified index
134
     *
135
     * @param string $key
136
     */
137 1
    public function offsetUnset($key)
138
    {
139 1
        unset($this->parameters[$key]);
140 1
    }
141
142
    /**
143
     * Magic getter, calls getXXX if exists.
144
     *
145
     * @param string $key
146
     *
147
     * @return mixed
148
     */
149 6 View Code Duplication
    public function __get($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151 6
        $getter = 'get' . $this->classify($key);
152 6
        if (method_exists($this, $getter)) {
153 1
            return call_user_func(array($this, $getter));
154
        }
155
156 6
        return $this->get($key);
157
    }
158
159
    /**
160
     * Magic setter, calls getXXX if exists.
161
     * @param $key
162
     * @param $value
163
     *
164
     * @return mixed
165
     */
166 1 View Code Duplication
    public function __set($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168 1
        $setter = 'set' . $this->classify($key);
169 1
        if (method_exists($this, $setter)) {
170
            return call_user_func_array(array($this, $setter), array($value));
171
        }
172 1
        $this->set($key, $value);
173 1
    }
174
175
    /**
176
     * Converts a string into a CamelCase word.
177
     *
178
     * @param string $string The string to classify.
179
     *
180
     * @return string The classified word.
181
     */
182 6
    private function classify($string)
183
    {
184 6
        return str_replace(' ', '', ucwords(strtr($string, '_-', ' ')));
185
    }
186
187
    /**
188
     * Returns whether the requested index exists
189
     *
190
     * @param string $key
191
     *
192
     * @return bool
193
     */
194 6
    public function __isset($key)
195
    {
196 6
        return isset($this->parameters[$key]);
197
    }
198
199
    /**
200
     * Unsets the value at the specified index
201
     *
202
     * @param string $key
203
     */
204 1
    public function __unset($key)
205
    {
206 1
        unset($this->parameters[$key]);
207 1
    }
208
}
209