| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient\Chunk; |
| 13 |
|
| 14 |
use Symfony\Contracts\HttpClient\ChunkInterface; |
| 15 |
|
| 16 |
/** |
| 17 |
* @author Antoine Bluchet <[email protected]> |
| 18 |
* @author Nicolas Grekas <[email protected]> |
| 19 |
*/ |
| 20 |
final class ServerSentEvent extends DataChunk implements ChunkInterface |
| 21 |
{ |
| 22 |
private $data = ''; |
| 23 |
private $id = ''; |
| 24 |
private $type = 'message'; |
| 25 |
private $retry = 0; |
| 26 |
|
| 27 |
public function __construct(string $content) |
| 28 |
{ |
| 29 |
parent::__construct(-1, $content); |
| 30 |
|
| 31 |
// remove BOM |
| 32 |
if (0 === strpos($content, "\xEF\xBB\xBF")) { |
| 33 |
$content = substr($content, 3); |
| 34 |
} |
| 35 |
|
| 36 |
foreach (preg_split("/(?:\r\n|[\r\n])/", $content) as $line) { |
| 37 |
if (0 === $i = strpos($line, ':')) { |
| 38 |
continue; |
| 39 |
} |
| 40 |
|
| 41 |
$i = false === $i ? \strlen($line) : $i; |
| 42 |
$field = substr($line, 0, $i); |
| 43 |
$i += 1 + (' ' === ($line[1 + $i] ?? '')); |
| 44 |
|
| 45 |
switch ($field) { |
| 46 |
case 'id': $this->id = substr($line, $i); break; |
| 47 |
case 'event': $this->type = substr($line, $i); break; |
| 48 |
case 'data': $this->data .= ('' === $this->data ? '' : "\n").substr($line, $i); break; |
| 49 |
case 'retry': |
| 50 |
$retry = substr($line, $i); |
| 51 |
|
| 52 |
if ('' !== $retry && \strlen($retry) === strspn($retry, '0123456789')) { |
| 53 |
$this->retry = $retry / 1000.0; |
| 54 |
} |
| 55 |
break; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public function getId(): string |
| 61 |
{ |
| 62 |
return $this->id; |
| 63 |
} |
| 64 |
|
| 65 |
public function getType(): string |
| 66 |
{ |
| 67 |
return $this->type; |
| 68 |
} |
| 69 |
|
| 70 |
public function getData(): string |
| 71 |
{ |
| 72 |
return $this->data; |
| 73 |
} |
| 74 |
|
| 75 |
public function getRetry(): float |
| 76 |
{ |
| 77 |
return $this->retry; |
| 78 |
} |
| 79 |
} |
| 80 |
|