|
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
|
|
|
|