-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathJavaScriptSerializer.php
More file actions
166 lines (139 loc) · 4.28 KB
/
JavaScriptSerializer.php
File metadata and controls
166 lines (139 loc) · 4.28 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
namespace Pest\Browser\Support;
use DateTimeImmutable;
/**
* Handles serialization and deserialization of JavaScript values.
*
* @internal
*/
final class JavaScriptSerializer
{
/**
* Serialize arguments for JavaScript evaluation according to Playwright protocol.
*
* @return array<string, mixed>
*/
public static function serializeArgument(mixed $value): array
{
return [
'value' => self::serializeValue($value),
'handles' => [],
];
}
/**
* Serialize a value according to Playwright's serialization format.
*
* @return array<string, mixed>
*/
public static function serializeValue(mixed $value): array
{
if ($value === null) {
return ['v' => 'null'];
}
if (is_bool($value)) {
return ['b' => $value];
}
if (is_int($value) || is_float($value)) {
if (is_float($value) && is_nan($value)) {
return ['v' => 'NaN'];
}
if (is_float($value) && is_infinite($value)) {
return ['v' => $value > 0 ? 'Infinity' : '-Infinity'];
}
if (is_int($value) && ($value < -9007199254740992 || $value > 9007199254740991)) {
return ['bi' => (string) $value];
}
return ['n' => $value];
}
if (is_string($value)) {
return ['s' => $value];
}
if (is_array($value)) {
$isAssoc = array_keys($value) !== range(0, count($value) - 1);
if ($isAssoc) {
$result = [];
foreach ($value as $key => $val) {
$result[] = ['k' => $key, 'v' => self::serializeValue($val)];
}
return ['o' => $result];
}
$result = [];
foreach ($value as $item) {
$result[] = self::serializeValue($item);
}
return ['a' => $result];
}
if (is_object($value)) {
if ($value instanceof DateTimeImmutable) {
return ['d' => $value->format('c')];
}
$result = [];
foreach (get_object_vars($value) as $key => $val) {
$result[] = ['k' => $key, 'v' => self::serializeValue($val)];
}
return ['o' => $result];
}
return ['s' => (string) $value]; // @phpstan-ignore-line
}
/**
* Parse a value from JavaScript according to Playwright protocol.
*/
public static function parseValue(mixed $value): mixed
{
if (! is_array($value)) {
return $value;
}
/**
* @var array{
* v?: string, // null, undefined, NaN, Infinity, -Infinity
* b?: bool, // boolean
* n?: int|float, // number
* s?: string, // string
* bi?: string, // bigint
* d?: string, // date in ISO 8601 format
* a?: array<mixed>, // array
* o?: array<array{ k: string, v: mixed }>, // object
* } $value
*/
// Handle primitive values
if (isset($value['v'])) {
return match ($value['v']) {
'null' => null,
'undefined' => null,
'NaN' => NAN,
'Infinity' => INF,
'-Infinity' => -INF,
default => $value['v'],
};
}
if (isset($value['b'])) {
return $value['b'];
}
if (isset($value['n'])) {
return $value['n'];
}
if (isset($value['s'])) {
return $value['s'];
}
if (isset($value['bi'])) {
return $value['bi'];
}
if (isset($value['d'])) {
return new DateTimeImmutable($value['d']);
}
// Handle arrays
if (isset($value['a'])) {
return array_map(self::parseValue(...), $value['a']);
}
// Handle objects
if (isset($value['o'])) {
$result = [];
foreach ($value['o'] as $item) {
$result[$item['k']] = self::parseValue($item['v']);
}
return $result;
}
return $value;
}
}