Passed
Branch master (7599e2)
by Sébastien
12:18 queued 06:01
created

ConnectionConfig::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Bdf\Prime\Connection;
4
5
use ArrayAccess;
6
use IteratorAggregate;
7
8
/**
9
 * Config
10
 * 
11
 * Manage multidimensional array. Config allows developer to easy get a value or a node
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
12
 * 
13
 * <code>
14
 * $config = new Config([
15
 *    'connection_name' => ['item1' => 'value1']
16
 * ]);
17
 * $config->get('connection_name'); //['item1' => 'value1']
18
 * </code>
19
 *
20
 * @deprecated Since 1.1
0 ignored issues
show
introduced by
The text '@deprecated Since 1.1' does not match the standard format: @deprecated in deprecation-version and is removed from removal-version. extra-info.
Loading history...
introduced by
Each @deprecated tag must have a @see tag immediately following it.
Loading history...
21
 */
22
class ConnectionConfig implements IteratorAggregate, ArrayAccess
23
{
24
    /**
25
     * @var array
26
     */
27
    protected $config;
28
29
    /**
30
     * Set the config content and the read only mode
31
     * 
32
     * @param array $config    Data
33
     */
34
    public function __construct(array $config = [])
35
    {
36
        $this->config = $config;
37
    }
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $default should have a doc-comment as per coding-style.
Loading history...
40
     * {@inheritdoc}
41
     */
42
    public function get($key, $default = null)
43
    {
44
        return $this->raw($key, $default);
45
    }
46
    
47
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
48
     * {@inheritdoc}
49
     */
50
    public function has($key)
51
    {
52
        return isset($this->config[$key]);
53
    }
54
    
55
    /**
56
     * Set datum
57
     * 
58
     * @param string $key
59
     * @param mixed  $value
60
     * 
61
     * @return $this
62
     */
63
    public function set($key, $value)
64
    {
65
        if ($value instanceof self) {
66
            $value = $value->config;
67
        }
68
        
69
        if ($key === null) {
0 ignored issues
show
introduced by
The condition $key === null is always false.
Loading history...
70
            $this->config[] = $value;
71
        } else {
72
            $this->config[$key] = $value;
73
        }
74
75
        return $this;
76
    }
77
    
78
    /**
79
     * Remove a datum
80
     * 
81
     * @param string $key
82
     * 
83
     * @return $this
84
     */
85
    public function remove($key)
86
    {
87
        unset($this->config[$key]);
88
89
        return $this;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function all()
96
    {
97
        return $this->config;
98
    }
99
100
    /**
101
     * SPL - IteratorAggregate
102
     *
103
     * {@inheritdoc}
104
     */
105
    public function getIterator()
106
    {
107
        foreach ($this->config as $key => $value) {
108
            yield $key => $this->get($key);
109
        }
110
    }
111
112
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $offset should have a doc-comment as per coding-style.
Loading history...
113
     * SPL - ArrayAccess
114
     * 
115
     * {@inheritdoc}
116
     */
117
    public function offsetExists($offset)
118
    {
119
        return $this->has($offset);
120
    }
121
122
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $offset should have a doc-comment as per coding-style.
Loading history...
123
     * SPL - ArrayAccess
124
     * 
125
     * {@inheritdoc}
126
     */
127
    public function offsetGet($offset)
128
    {
129
        return $this->get($offset);
130
    }
131
132
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $offset should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
133
     * SPL - ArrayAccess
134
     * 
135
     * {@inheritdoc}
136
     */
137
    public function offsetSet($offset, $value)
138
    {
139
        $this->set($offset, $value);
140
    }
141
142
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $offset should have a doc-comment as per coding-style.
Loading history...
143
     * SPL - ArrayAccess
144
     * 
145
     * {@inheritdoc}
146
     */
147
    public function offsetUnset($offset)
148
    {
149
        $this->remove($offset);
150
    }
151
152
    /**
153
     * Get the raw value
154
     *
155
     * @param string $key
156
     * @param mixed  $default
157
     *
158
     * @return mixed
159
     */
160
    private function raw($key, $default = null)
0 ignored issues
show
Coding Style introduced by
Private method name "ConnectionConfig::raw" must be prefixed with an underscore
Loading history...
161
    {
162
        return $this->config[$key] ?? $default;
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
163
    }
164
}
165