forked from php-enqueue/monitoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonSerializer.php
35 lines (27 loc) · 881 Bytes
/
JsonSerializer.php
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
<?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;
}
}