AbstractParameterBag   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 157
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A replace() 0 6 1
A add() 0 10 3
A all() 0 4 1
A values() 0 4 1
A keys() 0 4 1
A get() 0 8 2
A has() 0 4 1
A filter() 0 6 1
A getIterator() 0 4 1
A count() 0 4 1
A remove() 0 6 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-Http package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Http;
12
13
use ArrayIterator;
14
use Countable;
15
use IteratorAggregate;
16
17
/**
18
 * @author      Iqbal Maulana <[email protected]>
19
 * @created     8/6/15
20
 */
21
abstract class AbstractParameterBag implements IteratorAggregate, Countable
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $parameters = array();
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param array $parameters
32
     */
33
    public function __construct(array $parameters = array())
34
    {
35
        if (!empty($parameters)) {
36
            $this->replace($parameters);
37
        }
38
    }
39
40
    /**
41
     * Reset parameters and replace with new one.
42
     *
43
     * @param array $parameters
44
     *
45
     * @return $this
46
     */
47
    public function replace(array $parameters)
48
    {
49
        $this->parameters = array();
50
51
        return $this->add($parameters);
52
    }
53
54
    /**
55
     * Add parameters.
56
     *
57
     * @param array $parameters
58
     *
59
     * @return $this
60
     */
61
    public function add(array $parameters)
62
    {
63
        foreach($parameters as $key => $value) {
64
            if (method_exists($this, 'set')) {
65
                $this->{'set'}($key, $value);
66
            }
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * Get all parameters.
74
     *
75
     * @return array
76
     */
77
    public function all()
78
    {
79
        return $this->parameters;
80
    }
81
82
    /**
83
     * Get all parameter values.
84
     *
85
     * @return array
86
     */
87
    public function values()
88
    {
89
        return array_values($this->parameters);
90
    }
91
92
    /**
93
     * Get all parameter keys.
94
     *
95
     * @return array
96
     */
97
    public function keys()
98
    {
99
        return array_keys($this->parameters);
100
    }
101
102
    /**
103
     * Get parameter by key and return default value if not exists.
104
     *
105
     * @param string     $key
106
     * @param mixed|null $default
107
     *
108
     * @return mixed|null
109
     */
110
    public function get($key, $default = null)
111
    {
112
        if (isset($this->parameters[$key])) {
113
            return $this->parameters[$key];
114
        }
115
116
        return $default;
117
    }
118
119
    /**
120
     * Check if parameter exist by key.
121
     *
122
     * @param string $key
123
     *
124
     * @return bool Return true if parameter exist, false otherwise
125
     */
126
    public function has($key)
127
    {
128
        return array_key_exists($key, $this->parameters);
129
    }
130
131
    /**
132
     * Get parameter with key and filter the value.
133
     *
134
     * @param string     $key
135
     * @param int        $filter
136
     * @param mixed|null $default
137
     * @param array|null $options
138
     *
139
     * @return mixed
140
     */
141
    public function filter($key, $filter = FILTER_DEFAULT, $default = null, array $options = null)
142
    {
143
        $variable = $this->get($key, $default);
144
145
        return filter_var($variable, $filter, $options);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function getIterator()
152
    {
153
        return new ArrayIterator($this->parameters);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function count()
160
    {
161
        return count($this->parameters);
162
    }
163
164
    /**
165
     * Remove parameter by key.
166
     *
167
     * @param string $key
168
     *
169
     * @return $this
170
     */
171
    public function remove($key)
172
    {
173
        unset($this->parameters[$key]);
174
175
        return $this;
176
    }
177
}
178