Repository navigation
Expand file tree
/
Copy pathDebugSnapshot.php
More file actions
81 lines (67 loc) · 2.31 KB
/
Copy pathDebugSnapshot.php
File metadata and controls
81 lines (67 loc) · 2.31 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
<?php
declare(strict_types=1);
namespace PHPForge\Debug\Storage;
use JsonSerializable;
use function array_map;
/**
* Represents the versioned envelope persisted for one captured request.
*/
final readonly class DebugSnapshot implements JsonSerializable
{
public const int VERSION = 4;
/**
* Creates a versioned envelope for a captured request.
*
* @param RequestSummary $summary Captured request metadata.
* @param array<string, array<string, mixed>> $panels Serialized panel payloads indexed by panel ID.
* @param array<string, PanelFailure> $failures Panel failures indexed by panel ID.
*/
public function __construct(public RequestSummary $summary, public array $panels, public array $failures) {}
/**
* Hydrates a versioned request envelope from decoded JSON data.
*
* @param mixed $data Decoded snapshot envelope.
*
* @return self Hydrated request snapshot.
*/
public static function fromArray(mixed $data): self
{
$payload = Payload::object($data)->shape(['version', 'summary', 'panels', 'failures']);
if ($payload->int('version') !== self::VERSION) {
throw HydrationException::at(
'$.version',
'storage version ' . self::VERSION,
);
}
$panels = [];
foreach ($payload->map('panels') as $id => $panel) {
$panels[$id] = Payload::object($panel, "$.panels.{$id}")->all();
}
$failures = [];
foreach ($payload->map('failures') as $id => $failure) {
$failures[$id] = PanelFailure::fromArray($failure, "$.failures.{$id}");
}
return new self(
RequestSummary::fromArray($payload->raw('summary')),
$panels,
$failures,
);
}
/**
* Returns the request envelope for JSON serialization.
*
* @return array<string, mixed> Versioned request envelope.
*/
public function jsonSerialize(): array
{
return [
'version' => self::VERSION,
'summary' => $this->summary->jsonSerialize(),
'panels' => $this->panels,
'failures' => array_map(
static fn(PanelFailure $failure): array => $failure->jsonSerialize(),
$this->failures,
),
];
}
}