-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathParameters.php
89 lines (73 loc) · 2.02 KB
/
Parameters.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\HTTP\Parameters;
use ArrayIterator;
use CodeIgniter\Exceptions\RuntimeException;
/**
* @template TKey of string
* @template TValue
*
* @implements ParametersInterface<TKey, TValue>
*
* @see \CodeIgniter\HTTP\Parameters\ParametersTest
*/
class Parameters implements ParametersInterface
{
/**
* @param array<TKey, TValue> $parameters
*/
public function __construct(
protected array $parameters = [],
) {
}
public function override(array $parameters = []): void
{
$this->parameters = $parameters;
}
public function has(string $key): bool
{
return array_key_exists($key, $this->parameters);
}
public function get(string $key, mixed $default = null): mixed
{
return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
}
public function set(string $key, mixed $value): void
{
$this->parameters[$key] = $value;
}
public function remove(string $key): void
{
unset($this->parameters[$key]);
}
public function all(?string $key = null): array
{
if ($key === null) {
return $this->parameters;
}
if (! isset($this->parameters[$key]) || ! is_array($this->parameters[$key])) {
throw new RuntimeException(sprintf('The key "%s" value for Parameters is not an array or was not found.', $key));
}
return $this->parameters[$key];
}
public function keys(): array
{
return array_keys($this->parameters);
}
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->parameters);
}
public function count(): int
{
return count($this->parameters);
}
}