Repository navigation
Expand file tree
/
Copy pathQueryInput.php
More file actions
107 lines (89 loc) · 2.92 KB
/
Copy pathQueryInput.php
File metadata and controls
107 lines (89 loc) · 2.92 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
declare(strict_types=1);
namespace PHPForge\Debug\Data;
use function is_array;
use function is_finite;
use function is_float;
use function is_int;
use function is_numeric;
use function is_string;
/**
* Reads `Prefix[attribute]` filter groups and scalar values from a parsed query-parameter array.
*/
final class QueryInput
{
/**
* Returns the active `Prefix[attribute]` filter map from the query parameters.
*
* @param array<array-key, mixed> $query Parsed query parameters.
* @param string $prefix Filter-group prefix (for example, `Debug` matches `Debug[statusCode]`).
*
* @return array<string, string> Attribute-to-value map with empty and non-scalar entries removed.
*/
public static function group(array $query, string $prefix): array
{
$group = $query[$prefix] ?? null;
if (!is_array($group)) {
return [];
}
$filters = [];
foreach ($group as $attribute => $value) {
if (!is_string($attribute)) {
continue;
}
$normalized = self::stringValue($value);
if ($normalized === null || $normalized === '') {
continue;
}
$filters[$attribute] = $normalized;
}
return $filters;
}
/**
* Returns a submitted lower bound as a finite, non-negative number, or `null` when the value is unusable.
*
* Empty, non-numeric, negative, and overflowing values are rejected, so callers can clear the stored filter
* whenever `null` comes back.
*
* @param string $value Raw submitted bound.
*
* @return float|null Finite, non-negative bound, or `null` when the value is unusable.
*/
public static function minimumBound(string $value): float|null
{
if (!is_numeric($value)) {
return null;
}
$bound = (float) $value;
return is_finite($bound) && $bound >= 0.0 ? $bound : null;
}
/**
* Returns a top-level query parameter as a string, or `null` when absent or non-scalar.
*
* @param array<array-key, mixed> $query Parsed query parameters.
* @param string $name Parameter name to read.
*
* @return string|null Parameter value as a string, or `null` when absent or non-scalar.
*/
public static function scalar(array $query, string $name): string|null
{
return self::stringValue($query[$name] ?? null);
}
/**
* Normalizes a scalar query value to its string form.
*
* @param mixed $value Raw query value.
*
* @return string|null String form of a string, `int`, or `float` value; `null` for any other type.
*/
private static function stringValue(mixed $value): string|null
{
if (is_string($value)) {
return $value;
}
if (is_int($value) || is_float($value)) {
return (string) $value;
}
return null;
}
}