-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Event.php
202 lines (182 loc) · 5.01 KB
/
Event.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php declare(strict_types=1);
/*
* This file is part of the PHP Event Correlation package.
*
* (c) James Lucas <james@lucas.net.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EdgeTelemetrics\EventCorrelation;
use DateTimeImmutable;
use DateTimeZone;
use Exception;
use function ucfirst;
use function method_exists;
use function property_exists;
use function get_object_vars;
use function json_encode;
use function json_decode;
/**
* Class Event
* @package EdgeTelemetrics\EventCorrelation
* @property mixed $id
* @property string $event
* @property ?DateTimeImmutable $datetime
* @property ?DateTimeImmutable $receivedTime
*/
class Event implements IEvent {
/**
* @var mixed
*/
protected $id;
/**
* @var string
*/
protected string $event;
/**
* @var ?DateTimeImmutable
*/
protected ?DateTimeImmutable $datetime = null;
/**
* @var ?DateTimeImmutable
*/
protected ?DateTimeImmutable $receivedTime = null;
/**
* Event constructor.
* @param array $kvp Key Value pairs
* @throws Exception
*/
public function __construct(array $kvp)
{
//$this->datetime = DateTime::createFromFormat('U', $kvp['datetime'], new DateTimeZone('UTC'));
if (isset($kvp['datetime']))
{
//Parse the date string into a DateTimeImmutable (supports timezone encoded in string), then normalise the datetime to UTC timezone
$timezone = new DateTimeZone('UTC');
$this->datetime = (new DateTimeImmutable($kvp['datetime'], $timezone))->setTimezone($timezone);
unset($kvp['datetime']);
}
else
{
/** If the event data doesn't have a datetime then set it to the server time when received **/
$this->datetime = new DateTimeImmutable();
}
foreach ($kvp as $key => $value) {
$this->$key = $value;
}
}
/**
* @param string $event_name
* @return bool
*/
public function is_event(string $event_name) : bool {
return $this->event === $event_name;
}
/**
* Will return class variable, will first look for a function get<Name> and then check class properties
* @param string $name
* @return mixed|null
*/
public function __get(string $name)
{
$methodName = "get" . ucfirst($name);
if (method_exists($this, $methodName))
{
return $this->$methodName();
}
else if (property_exists($this, $name))
{
return $this->$name;
}
return null;
}
public function getId()
{
return $this->id;
}
public function getEventName(): string
{
return $this->event;
}
/**
* @param DateTimeImmutable $time
*/
public function setReceivedTime(DateTimeImmutable $time) : void
{
$this->receivedTime = $time;
}
/**
* Get the event datetime. If the receivedTime property is set this will be used
* as the server time when received was too different from the event timestamp
* @return ?DateTimeImmutable
*/
public function getDatetime() : ?DateTimeImmutable
{
return ($this->receivedTime ?? $this->datetime);
}
/**
* Convert this Event into a plain PHP Array
* @return array
*/
public function toArray() : array {
$object = get_object_vars($this);
$object['datetime'] = $this->datetime->format(IEvent::DATETIME_SERIALISATION_FORMAT);
if (null !== $this->receivedTime) {
$object['receivedTime'] = $this->receivedTime->format(IEvent::DATETIME_SERIALISATION_FORMAT);
}
else
{
unset($object['receivedTime']);
}
return $object;
}
/**
* @return array
*/
public function jsonSerialize() : array
{
return $this->toArray();
}
/**
* @return false|string
* @deprecated
*/
public function serialize()
{
return json_encode($this, JSON_PRESERVE_ZERO_FRACTION|JSON_UNESCAPED_SLASHES);
}
/**
* @return array
*/
public function __serialize(): array
{
return $this->toArray();
}
/**
* @param string $data
* @throws Exception
* @deprecated Left to support loading older state files
*/
public function unserialize($data)
{
$data = json_decode($data, true);
$this->__unserialize($data);
}
/**
* @param array $data
* @throws Exception
*/
public function __unserialize(array $data) {
$this->datetime = new DateTimeImmutable($data['datetime']);
unset($data['datetime']);
if (isset($data['receivedTime'])) {
$this->receivedTime = new DateTimeImmutable($data['receivedTime']);
unset($data['receivedTime']);
}
foreach($data as $key => $value)
{
$this->$key = $value;
}
}
}