-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathLogItem.php
More file actions
124 lines (108 loc) · 3.18 KB
/
LogItem.php
File metadata and controls
124 lines (108 loc) · 3.18 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
<?php
declare(strict_types=1);
namespace Platformsh\Client\Model\ActivityLog;
class LogItem
{
private string $timestamp;
private string $message;
private string $id;
public function __construct(string $timestamp, string $message, string $id = '')
{
$this->timestamp = $timestamp;
$this->message = $message;
$this->id = $id;
}
/**
* @return string
*/
public function __toString()
{
return $this->message;
}
/**
* @return LogItem|false
* The log item, or FALSE if there is not enough information.
*@deprecated use LogItem::multipleFromJsonStreamWithSeal() instead
*/
public static function singleFromJson(string $str): self|false
{
$data = self::decode($str);
if (isset($data['data']['timestamp'], $data['data']['message'])) {
$id = isset($data['_id']) ? (string) $data['_id'] : '';
return new static($data['data']['timestamp'], $data['data']['message'], $id);
}
return false;
}
/**
* @return self[]
*@deprecated use LogItem::multipleFromJsonStreamWithSeal() instead
*/
public static function multipleFromJsonStream(string $str): array
{
$items = [];
foreach (explode("\n", trim($str, "\n")) as $line) {
if ($line === '') {
continue;
}
$item = static::singleFromJson($line);
if ($item !== false) {
$items[] = $item;
}
}
return $items;
}
/**
* Decodes the log stream into log items and whether the "seal" was reached.
*
* The seal 🦭 guarantees that the log has ended.
*
* @return array{'items': static[], 'seal': bool}
*/
public static function multipleFromJsonStreamWithSeal(string $str): array
{
$items = [];
$seal = false;
foreach (explode("\n", trim($str, "\n")) as $line) {
if ($line === '') {
continue;
}
$data = self::decode($line);
if (is_array($data)) {
if (! empty($data['seal'])) {
$seal = true;
}
if (isset($data['data']['timestamp'], $data['data']['message'])) {
$id = isset($data['_id']) ? (string) $data['_id'] : '';
$items[] = new static($data['data']['timestamp'], $data['data']['message'], $id);
}
}
}
return [
'items' => $items,
'seal' => $seal,
];
}
public function getMessage(): string
{
return $this->message;
}
/**
* @throws \Exception
*/
public function getTime(): \DateTimeImmutable
{
return new \DateTimeImmutable($this->timestamp);
}
public function getId(): string
{
return $this->id;
}
private static function decode(string $str): mixed
{
$data = json_decode($str, true);
if ($data === null) {
trigger_error(sprintf('Failed to decode JSON line with message: %s: %s', json_last_error_msg(), $str), E_USER_WARNING);
}
return $data;
}
}