-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvent.php
70 lines (56 loc) · 1.52 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
<?php
class Event
{
const END_OF_LINE = "/\r\n|\n|\r/";
private $data;
private $eventType;
private $id;
public function __construct($data = '', $eventType = 'message', $id = null)
{
$this->data = $data;
$this->eventType = $eventType;
$this->id = $id;
}
public static function parse($raw)
{
$event = new static();
$lines = preg_split(self::END_OF_LINE, $raw);
foreach ($lines as $line) {
$matched = preg_match('/(?P<name>[^:]*):?( ?(?P<value>.*))?/', $line, $matches);
if (!$matched) {
throw new InvalidArgumentException(sprintf('Invalid line %s', $line));
}
$name = $matches['name'];
$value = $matches['value'];
if ($name === '') {
continue;
}
switch ($name) {
case 'event':
$event->eventType = $value;
break;
case 'data':
$event->data = empty($event->data) ? $value : "$event->data\n$value";
break;
case 'id':
$event->id = $value;
break;
default:
break;
}
}
return $event;
}
public function getData()
{
return $this->data;
}
public function getEventType()
{
return $this->eventType;
}
public function getId()
{
return $this->id;
}
}