-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathInputParameters.php
64 lines (51 loc) · 1.95 KB
/
InputParameters.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
<?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 CodeIgniter\Exceptions\InvalidArgumentException;
use CodeIgniter\Exceptions\RuntimeException;
/**
* @template TKey of string
* @template TValue of scalar|array<(int|string), mixed>
*
* @extends Parameters<TKey, TValue>
*
* @see \CodeIgniter\HTTP\Parameters\InputParametersTest
*/
class InputParameters extends Parameters
{
public function override(array $parameters = []): void
{
$this->parameters = [];
foreach ($parameters as $key => $value) {
$this->set($key, $value);
}
}
public function get(string $key, mixed $default = null): mixed
{
if ($default !== null && ! is_scalar($default)) {
throw new InvalidArgumentException(sprintf('The default value for the InputParameters must be a scalar type, "%s" given.', gettype($default)));
}
// TODO: We need to check that the default value is set. Let's check the unique string
$tempDefault = bin2hex(random_bytes(8));
$value = parent::get($key, $tempDefault);
if ($value !== null && $value !== $tempDefault && ! is_scalar($value)) {
throw new RuntimeException(sprintf('The value of the key "%s" InputParameters does not contain a scalar value, "%s" given.', $key, gettype($value)));
}
return $value === $tempDefault ? $default : $value;
}
public function set(string $key, mixed $value): void
{
if (! is_scalar($value) && ! is_array($value)) {
throw new InvalidArgumentException(sprintf('The value for the InputParameters must be a scalar type, "%s" given.', gettype($value)));
}
$this->parameters[$key] = $value;
}
}