Repository navigation
Expand file tree
/
Copy pathArrayPayloadSnapshot.php
More file actions
79 lines (70 loc) · 2.1 KB
/
Copy pathArrayPayloadSnapshot.php
File metadata and controls
79 lines (70 loc) · 2.1 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
<?php
declare(strict_types=1);
namespace PHPForge\Debug\Storage;
/**
* Provides the capture/hydrate/serialize cycle for panels whose payload stays genuinely dynamic (application
* configuration, dumped values, and user-configured request globals).
*/
trait ArrayPayloadSnapshot
{
/**
* Creates a snapshot from a tagged dynamic payload.
*
* @param DebugArray $payload Tagged dynamic payload.
*/
final public function __construct(private readonly DebugArray $payload) {}
/**
* Captures arbitrary array values in a JSON-safe snapshot.
*
* @param array<array-key, mixed> $values Raw payload captured for the request.
*
* @return self Snapshot containing tagged debug values.
*/
public static function capture(array $values): self
{
return new self(DebugArray::capture($values));
}
/**
* Hydrates a dynamic payload from decoded JSON data.
*
* @param mixed $data Decoded JSON payload.
* @param string $path Payload path used in hydration errors.
*
* @return self Hydrated dynamic payload snapshot.
*/
public static function fromArray(mixed $data, string $path): self
{
$key = self::payloadKey();
return new self(
Payload::object($data, $path)
->shape([$key])
->debugArray($key),
);
}
/**
* Returns the tagged payload for JSON serialization.
*
* @return array<string, mixed> Tagged payload indexed by its persistence key.
*/
public function jsonSerialize(): array
{
return [
self::payloadKey() => $this->payload->jsonSerialize(),
];
}
/**
* Returns the JSON key used to persist the dynamic payload.
*
* @return string Persisted payload key.
*/
abstract protected static function payloadKey(): string;
/**
* Returns the payload restored to plain PHP values.
*
* @return array<array-key, mixed> Payload restored to plain PHP values.
*/
private function values(): array
{
return $this->payload->values();
}
}