ParameterBag   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 93
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 13 3
A all() 0 4 1
A keys() 0 4 1
A replace() 0 4 1
A add() 0 8 2
A get() 0 8 2
A set() 0 4 1
A has() 0 4 1
A remove() 0 4 1
A getIterator() 0 4 1
A count() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Easy;
15
16
use ArrayIterator;
17
use Countable;
18
use InvalidArgumentException;
19
use IteratorAggregate;
20
21
class ParameterBag implements IteratorAggregate, Countable
22
{
23
    /**
24
     * @var array
25
     */
26
    private $parameters = [];
27
28
    /**
29
     * @return mixed
30
     */
31
    public function __call(string $name, array $arguments)
32
    {
33
        if (method_exists($this, $name)) {
34
            return \call_user_func_array([$this, $name], $arguments);
35
        }
36
37
        if (0 === \count($arguments)) {
38
            return $this->get($name);
39
        }
40
        array_unshift($arguments, $name);
41
42
        return \call_user_func_array([$this, 'set'], $arguments);
43
    }
44
45
    public function all(): array
46
    {
47
        return $this->parameters;
48
    }
49
50
    public function keys(): array
51
    {
52
        return array_keys($this->parameters);
53
    }
54
55
    public function replace(array $parameters): void
56
    {
57
        $this->parameters = $parameters;
58
    }
59
60
    /**
61
     * @throws InvalidArgumentException if the parameters are invalid
62
     */
63
    public function add(array $parameters): void
64
    {
65
        $replaced = array_replace($this->parameters, $parameters);
66
        if (null === $replaced) {
67
            throw new InvalidArgumentException('Invalid parameters');
68
        }
69
        $this->parameters = $replaced;
70
    }
71
72
    /**
73
     * @throws InvalidArgumentException if the selected parameter is missing
74
     *
75
     * @return mixed
76
     */
77
    public function get(string $key)
78
    {
79
        if (!\array_key_exists($key, $this->parameters)) {
80
            throw new InvalidArgumentException(sprintf('Parameter "%s" is missing', $key));
81
        }
82
83
        return $this->parameters[$key];
84
    }
85
86
    /**
87
     * @param mixed $value The value
88
     */
89
    public function set(string $key, $value): void
90
    {
91
        $this->parameters[$key] = $value;
92
    }
93
94
    public function has(string $key): bool
95
    {
96
        return \array_key_exists($key, $this->parameters);
97
    }
98
99
    public function remove(string $key): void
100
    {
101
        unset($this->parameters[$key]);
102
    }
103
104
    public function getIterator(): ArrayIterator
105
    {
106
        return new ArrayIterator($this->parameters);
107
    }
108
109
    public function count(): int
110
    {
111
        return \count($this->parameters);
112
    }
113
}
114