forked from php-enqueue/enqueue-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonSerializer.php
More file actions
31 lines (23 loc) · 820 Bytes
/
JsonSerializer.php
File metadata and controls
31 lines (23 loc) · 820 Bytes
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
<?php
declare(strict_types=1);
namespace Enqueue\Monitoring;
class JsonSerializer implements Serializer
{
public function toString(Stats $stats): string
{
$rfClass = new \ReflectionClass($stats);
$data = [
'event' => $rfClass->getShortName(),
];
foreach ($rfClass->getProperties() as $rfProperty) {
$rfProperty->setAccessible(true);
$data[$rfProperty->getName()] = $rfProperty->getValue($stats);
$rfProperty->setAccessible(false);
}
$json = json_encode($data);
if (\JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(sprintf('The malformed json given. Error %s and message %s', json_last_error(), json_last_error_msg()));
}
return $json;
}
}