| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient\Chunk; |
| 13 |
|
| 14 |
use Symfony\Component\HttpClient\Exception\JsonException; |
| 15 |
use Symfony\Contracts\HttpClient\ChunkInterface; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
final class ServerSentEvent extends DataChunk implements ChunkInterface |
| 22 |
{ |
| 23 |
private string $data = ''; |
| 24 |
private string $id = ''; |
| 25 |
private string $type = 'message'; |
| 26 |
private float $retry = 0; |
| 27 |
private ?array $jsonData = null; |
| 28 |
|
| 29 |
public function __construct(string $content) |
| 30 |
{ |
| 31 |
parent::__construct(-1, $content); |
| 32 |
|
| 33 |
|
| 34 |
if (str_starts_with($content, "\xEF\xBB\xBF")) { |
| 35 |
$content = substr($content, 3); |
| 36 |
} |
| 37 |
|
| 38 |
foreach (preg_split("/(?:\r\n|[\r\n])/", $content) as $line) { |
| 39 |
if (0 === $i = strpos($line, ':')) { |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
$i = false === $i ? \strlen($line) : $i; |
| 44 |
$field = substr($line, 0, $i); |
| 45 |
$i += 1 + (' ' === ($line[1 + $i] ?? '')); |
| 46 |
|
| 47 |
switch ($field) { |
| 48 |
case 'id': |
| 49 |
$this->id = substr($line, $i); |
| 50 |
break; |
| 51 |
case 'event': |
| 52 |
$this->type = substr($line, $i); |
| 53 |
break; |
| 54 |
case 'data': |
| 55 |
$this->data .= ('' === $this->data ? '' : "\n").substr($line, $i); |
| 56 |
break; |
| 57 |
case 'retry': |
| 58 |
$retry = substr($line, $i); |
| 59 |
|
| 60 |
if ('' !== $retry && \strlen($retry) === strspn($retry, '0123456789')) { |
| 61 |
$this->retry = $retry / 1000.0; |
| 62 |
} |
| 63 |
|
| 64 |
break; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public function getId(): string |
| 70 |
{ |
| 71 |
return $this->id; |
| 72 |
} |
| 73 |
|
| 74 |
public function getType(): string |
| 75 |
{ |
| 76 |
return $this->type; |
| 77 |
} |
| 78 |
|
| 79 |
public function getData(): string |
| 80 |
{ |
| 81 |
return $this->data; |
| 82 |
} |
| 83 |
|
| 84 |
public function getRetry(): float |
| 85 |
{ |
| 86 |
return $this->retry; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function getArrayData(): array |
| 93 |
{ |
| 94 |
if (null !== $this->jsonData) { |
| 95 |
return $this->jsonData; |
| 96 |
} |
| 97 |
|
| 98 |
if ('' === $this->data) { |
| 99 |
throw new JsonException(\sprintf('Server-Sent Event%s data is empty.', '' !== $this->id ? \sprintf(' "%s"', $this->id) : '')); |
| 100 |
} |
| 101 |
|
| 102 |
try { |
| 103 |
$jsonData = json_decode($this->data, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR); |
| 104 |
} catch (\JsonException $e) { |
| 105 |
throw new JsonException(\sprintf('Decoding Server-Sent Event%s failed: ', '' !== $this->id ? \sprintf(' "%s"', $this->id) : '').$e->getMessage(), $e->getCode()); |
| 106 |
} |
| 107 |
|
| 108 |
if (!\is_array($jsonData)) { |
| 109 |
throw new JsonException(\sprintf('JSON content was expected to decode to an array, "%s" returned in Server-Sent Event%s.', get_debug_type($jsonData), '' !== $this->id ? \sprintf(' "%s"', $this->id) : '')); |
| 110 |
} |
| 111 |
|
| 112 |
return $this->jsonData = $jsonData; |
| 113 |
} |
| 114 |
} |