forked from clue/reactphp-eventsource
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageEvent.php
More file actions
68 lines (59 loc) · 1.44 KB
/
MessageEvent.php
File metadata and controls
68 lines (59 loc) · 1.44 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
<?php
namespace Clue\React\EventSource;
class MessageEvent
{
/**
* @param string $data
* @return self
* @internal
*/
public static function parse($data)
{
$message = new self();
$lines = preg_split(
'/\r\n|\r(?!\n)|\n/S',
$data
);
foreach ($lines as $line) {
$name = strstr($line, ':', true);
$value = substr(strstr($line, ':'), 1);
if (isset($value[0]) && $value[0] === ' ') {
$value = substr($value, 1);
}
if ($name === 'data') {
$message->data .= $value . "\n";
} elseif ($name === 'id') {
$message->lastEventId .= $value;
} elseif ($name === 'event') {
$message->type = $value;
} elseif ($name === 'retry' && $value === (string)(int)$value && $value >= 0) {
$message->retry = (int)$value;
}
}
if (substr($message->data, -1) === "\n") {
$message->data = substr($message->data, 0, -1);
}
return $message;
}
/**
* @var string
* @readonly
*/
public $data = '';
/**
* @var ?string
* @readonly
*/
public $lastEventId = null;
/**
* @var string
* @readonly
*/
public $type = 'message';
/**
* @internal
* @var ?int
* @readonly
*/
public $retry;
}