Passed
Push — master ( f070bb...4ac1e0 )
by Marcel
10:45
created

ParameterBag::__serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[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 LightSaml\Meta;
13
14
use Traversable;
15
16
class ParameterBag implements \IteratorAggregate, \Countable
17
{
18
    /**
19
     * Parameter storage.
20
     *
21
     * @var array
22
     */
23
    protected $parameters;
24
25
    /**
26
     * @param array $parameters An array of parameters
27
     */
28
    public function __construct(array $parameters = [])
29
    {
30
        $this->parameters = $parameters;
31
    }
32
33
    /**
34
     * Returns the parameters.
35
     *
36
     * @return array An array of parameters
37
     */
38
    public function all()
39
    {
40
        return $this->parameters;
41
    }
42
43
    /**
44
     * Returns the parameter keys.
45
     *
46
     * @return array An array of parameter keys
47
     */
48
    public function keys()
49
    {
50
        return array_keys($this->parameters);
51
    }
52
53
    /**
54
     * Replaces the current parameters by a new set.
55
     *
56
     * @param array $parameters An array of parameters
57
     */
58
    public function replace(array $parameters = [])
59
    {
60
        $this->parameters = $parameters;
61
    }
62
63
    /**
64
     * Adds parameters.
65
     */
66
    public function add(array $parameters = [])
67
    {
68
        $this->parameters = array_replace($this->parameters, $parameters);
69
    }
70
71
    /**
72
     * Returns a parameter by name.
73
     *
74
     * @param string $key
75
     * @param mixed  $default
76
     *
77
     * @return mixed
78
     */
79
    public function get($key, $default = null)
80
    {
81
        return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
82
    }
83
84
    /**
85
     * Sets a parameter by name.
86
     *
87
     * @param string $key
88
     * @param mixed  $value
89
     */
90
    public function set($key, $value)
91
    {
92
        $this->parameters[$key] = $value;
93
    }
94
95
    /**
96
     * Returns true if the parameter is defined.
97
     *
98
     * @param string $key
99
     *
100
     * @return bool true if the parameter exists, false otherwise
101
     */
102
    public function has($key)
103
    {
104
        return array_key_exists($key, $this->parameters);
105
    }
106
107
    /**
108
     * Removes a parameter.
109
     *
110
     * @param string $key
111
     */
112
    public function remove($key)
113
    {
114
        unset($this->parameters[$key]);
115
    }
116
117
    /**
118
     * @return \ArrayIterator
119
     */
120
    public function getIterator(): Traversable
121
    {
122
        return new \ArrayIterator($this->parameters);
123
    }
124
125
    /**
126
     * @return int
127
     */
128
    public function count(): int
129
    {
130
        return count($this->parameters);
131
    }
132
133
    public function __serialize(): array {
134
        return $this->parameters;
135
    }
136
137
    public function __unserialize(array $data): void {
138
        $this->parameters = $data;
139
    }
140
}
141