ParameterBag::add()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 2
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Buzz\Configuration;
6
7
/**
8
 * A ParameterBag is a container for key/value pairs. This implementation is immutable.
9
 *
10
 * @author Fabien Potencier <[email protected]>
11
 * @author Tobias Nyholm <[email protected]>
12
 */
13
final class ParameterBag implements \IteratorAggregate, \Countable
14
{
15
    /**
16
     * Parameter storage.
17
     */
18
    private $parameters;
19
20
    /**
21
     * @param array $parameters An array of parameters
22
     */
23 245
    public function __construct(array $parameters = [])
24
    {
25 245
        $this->parameters = $parameters;
26 245
    }
27
28
    /**
29
     * Returns the parameters.
30
     *
31
     * @return array An array of parameters
32
     */
33 228
    public function all(): array
34
    {
35 228
        return $this->parameters;
36
    }
37
38
    /**
39
     * Returns the parameter keys.
40
     *
41
     * @return array An array of parameter keys
42
     */
43 1
    public function keys(): array
44
    {
45 1
        return array_keys($this->parameters);
46
    }
47
48
    /**
49
     * Adds parameters to the existing ones.
50
     *
51
     * @param array $parameters An array of parameters
52
     */
53 237
    public function add(array $parameters = []): self
54
    {
55
        // Make sure to merge Curl parameters
56 237
        if (isset($this->parameters['curl'])
57 237
            && isset($parameters['curl'])
58 237
            && \is_array($this->parameters['curl'])
59 237
            && \is_array($parameters['curl'])) {
60 4
            $parameters['curl'] = array_replace($this->parameters['curl'], $parameters['curl']);
61
        }
62
63
        /** @var array $newParameters */
64 237
        $newParameters = array_replace($this->parameters, $parameters);
65
66 237
        return new self($newParameters);
67
    }
68
69
    /**
70
     * Returns a parameter by name.
71
     *
72
     * @param string|int $key     The key
73
     * @param mixed      $default The default value if the parameter key does not exist
74
     */
75 249
    public function get($key, $default = null)
76
    {
77 249
        return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
78
    }
79
80
    /**
81
     * Returns true if the parameter is defined.
82
     *
83
     * @param string|int $key The key
84
     *
85
     * @return bool true if the parameter exists, false otherwise
86
     */
87 1
    public function has($key): bool
88
    {
89 1
        return \array_key_exists($key, $this->parameters);
90
    }
91
92
    /**
93
     * Returns an iterator for parameters.
94
     *
95
     * @return \ArrayIterator An \ArrayIterator instance
96
     */
97 1
    public function getIterator(): \ArrayIterator
98
    {
99 1
        return new \ArrayIterator($this->parameters);
100
    }
101
102
    /**
103
     * Returns the number of parameters.
104
     *
105
     * @return int The number of parameters
106
     */
107 1
    public function count(): int
108
    {
109 1
        return \count($this->parameters);
110
    }
111
}
112