|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PHPForge\Debug\Helper; |
| 6 | + |
| 7 | +use Stringable; |
| 8 | + |
| 9 | +use function is_array; |
| 10 | +use function is_numeric; |
| 11 | +use function is_scalar; |
| 12 | +use function is_string; |
| 13 | + |
| 14 | +/** |
| 15 | + * Narrows arbitrary mixed payloads into the typed scalars debug-panel renderers expect. |
| 16 | + * |
| 17 | + * Framework callbacks, logger entries, request parameters, and renderer rows remain mixed even though persisted |
| 18 | + * snapshots use strict JSON DTOs. This helper is limited to those external runtime boundaries; snapshot hydration is |
| 19 | + * handled by {@see \PHPForge\Debug\Storage\Payload} without scalar coercion. |
| 20 | + */ |
| 21 | +final class Coerce |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Returns a numeric value as a float or the supplied default. |
| 25 | + * |
| 26 | + * Usage example: |
| 27 | + * ```php |
| 28 | + * $duration = \PHPForge\Debug\Helper\Coerce::float($payload['duration'] ?? null, 0.0); |
| 29 | + * ``` |
| 30 | + * |
| 31 | + * @param mixed $value Value to narrow. |
| 32 | + * @param float $default Value returned for non-numeric input. |
| 33 | + * |
| 34 | + * @return float Numeric value or the configured default. |
| 35 | + */ |
| 36 | + public static function float(mixed $value, float $default = 0.0): float |
| 37 | + { |
| 38 | + return self::floatOrNull($value) ?? $default; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Returns the value as a float when it is numeric, `null` otherwise. |
| 43 | + * |
| 44 | + * Usage example: |
| 45 | + * ```php |
| 46 | + * $duration = \PHPForge\Debug\Helper\Coerce::floatOrNull($payload['duration'] ?? null); |
| 47 | + * ``` |
| 48 | + * |
| 49 | + * @param mixed $value Value to narrow. |
| 50 | + * |
| 51 | + * @return float|null Numeric value or `null`. |
| 52 | + */ |
| 53 | + public static function floatOrNull(mixed $value): float|null |
| 54 | + { |
| 55 | + return is_numeric($value) ? (float) $value : null; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns a numeric value as an int or the supplied default. |
| 60 | + * |
| 61 | + * Usage example: |
| 62 | + * ```php |
| 63 | + * $count = \PHPForge\Debug\Helper\Coerce::int($payload['count'] ?? null); |
| 64 | + * ``` |
| 65 | + * |
| 66 | + * @param mixed $value Value to narrow. |
| 67 | + * @param int $default Value returned for non-numeric input. |
| 68 | + * |
| 69 | + * @return int Numeric value or the configured default. |
| 70 | + */ |
| 71 | + public static function int(mixed $value, int $default = 0): int |
| 72 | + { |
| 73 | + return self::intOrNull($value) ?? $default; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns the value as an int when it is an integer or numeric, `null` otherwise. |
| 78 | + * |
| 79 | + * Usage example: |
| 80 | + * ```php |
| 81 | + * $statusCode = \PHPForge\Debug\Helper\Coerce::intOrNull($payload['statusCode'] ?? null); |
| 82 | + * ``` |
| 83 | + * |
| 84 | + * @param mixed $value Value to narrow. |
| 85 | + * |
| 86 | + * @return int|null Numeric value or `null`. |
| 87 | + */ |
| 88 | + public static function intOrNull(mixed $value): int|null |
| 89 | + { |
| 90 | + return is_numeric($value) ? (int) $value : null; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns a string value or the supplied default. |
| 95 | + * |
| 96 | + * Usage example: |
| 97 | + * ```php |
| 98 | + * $category = \PHPForge\Debug\Helper\Coerce::string($payload['category'] ?? null, 'application'); |
| 99 | + * ``` |
| 100 | + * |
| 101 | + * @param mixed $value Value to narrow. |
| 102 | + * @param string $default Value returned for non-string input. |
| 103 | + * |
| 104 | + * @return string String value or the configured default. |
| 105 | + */ |
| 106 | + public static function string(mixed $value, string $default = ''): string |
| 107 | + { |
| 108 | + return is_string($value) ? $value : $default; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns only the entries of `$data` whose key is a string, preserving order. |
| 113 | + * |
| 114 | + * Narrows a mixed/`array<array-key, mixed>` snapshot down to the `array<string, mixed>` shape downstream view-model |
| 115 | + * normalizers expect. |
| 116 | + * |
| 117 | + * Usage example: |
| 118 | + * ```php |
| 119 | + * $data = \PHPForge\Debug\Helper\Coerce::stringKeyedArray(['name' => 'debug', 0 => 'ignored']); |
| 120 | + * ``` |
| 121 | + * |
| 122 | + * @param array<array-key, mixed> $data Source array with arbitrary keys. |
| 123 | + * |
| 124 | + * @return array<string, mixed> Entries whose key was already a string, in original order. |
| 125 | + */ |
| 126 | + public static function stringKeyedArray(array $data): array |
| 127 | + { |
| 128 | + $normalized = []; |
| 129 | + |
| 130 | + foreach ($data as $key => $value) { |
| 131 | + if (is_string($key)) { |
| 132 | + $normalized[$key] = $value; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return $normalized; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Returns only the string entries of a raw list, preserving order. |
| 141 | + * |
| 142 | + * Usage example: |
| 143 | + * ```php |
| 144 | + * $categories = \PHPForge\Debug\Helper\Coerce::stringList(['application', 42, 'database']); |
| 145 | + * ``` |
| 146 | + * |
| 147 | + * @param mixed $values Raw list, typically a user-configured category list. |
| 148 | + * |
| 149 | + * @return list<string> String entries in original order, possibly empty. |
| 150 | + */ |
| 151 | + public static function stringList(mixed $values): array |
| 152 | + { |
| 153 | + if (!is_array($values)) { |
| 154 | + return []; |
| 155 | + } |
| 156 | + |
| 157 | + $strings = []; |
| 158 | + |
| 159 | + foreach ($values as $value) { |
| 160 | + if (is_string($value)) { |
| 161 | + $strings[] = $value; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return $strings; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Returns the value as a string when it is scalar or {@see Stringable}, `null` otherwise. |
| 170 | + * |
| 171 | + * Usage example: |
| 172 | + * ```php |
| 173 | + * $label = \PHPForge\Debug\Helper\Coerce::stringOrNull($payload['label'] ?? null); |
| 174 | + * ``` |
| 175 | + * |
| 176 | + * @param mixed $value Value to narrow. |
| 177 | + * |
| 178 | + * @return string|null String representation or `null`. |
| 179 | + */ |
| 180 | + public static function stringOrNull(mixed $value): string|null |
| 181 | + { |
| 182 | + if (is_scalar($value) || $value instanceof Stringable) { |
| 183 | + return (string) $value; |
| 184 | + } |
| 185 | + |
| 186 | + return null; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Narrows a raw trace value (as captured by Yii's logger) into the `list<array<string, mixed>>` shape every panel |
| 191 | + * renderer consumes. |
| 192 | + * |
| 193 | + * Each frame keeps only its string-keyed entries; non-array frames are dropped. |
| 194 | + * |
| 195 | + * Usage example: |
| 196 | + * ```php |
| 197 | + * $frames = \PHPForge\Debug\Helper\Coerce::traceFrames($payload['trace'] ?? null); |
| 198 | + * ``` |
| 199 | + * |
| 200 | + * @param mixed $value Raw trace payload. |
| 201 | + * |
| 202 | + * @return list<array<string, mixed>> Trace frames normalized to string-keyed maps. |
| 203 | + */ |
| 204 | + public static function traceFrames(mixed $value): array |
| 205 | + { |
| 206 | + if (!is_array($value)) { |
| 207 | + return []; |
| 208 | + } |
| 209 | + |
| 210 | + $frames = []; |
| 211 | + |
| 212 | + foreach ($value as $frame) { |
| 213 | + if (!is_array($frame)) { |
| 214 | + continue; |
| 215 | + } |
| 216 | + |
| 217 | + $frames[] = self::stringKeyedArray($frame); |
| 218 | + } |
| 219 | + |
| 220 | + return $frames; |
| 221 | + } |
| 222 | +} |
0 commit comments