Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/ServerEvent.php

raw symbols SCIP
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\HttpFoundation;
13
14 /**
15 * An event generated on the server intended for streaming to the client
16 * as part of the SSE streaming technique.
17 *
18 * @implements \IteratorAggregate<string>
19 *
20 * @author Yonel Ceruto <[email protected]>
21 */
22 class ServerEvent implements \IteratorAggregate
23 {
24 /**
25 * @param string|iterable<string> $data The event data field for the message
26 * @param string|null $type The event type
27 * @param int|null $retry The number of milliseconds the client should wait
28 * before reconnecting in case of network failure
29 * @param string|null $id The event ID to set the EventSource object's last event ID value
30 * @param string|null $comment The event comment
31 */
32 public function __construct(
33 private string|iterable $data,
34 private ?string $type = null,
35 private ?int $retry = null,
36 private ?string $id = null,
37 private ?string $comment = null,
38 ) {
39 }
40
41 public function getData(): iterable|string
42 {
43 return $this->data;
44 }
45
46 /**
47 * @return $this
48 */
49 public function setData(iterable|string $data): static
50 {
51 $this->data = $data;
52
53 return $this;
54 }
55
56 public function getType(): ?string
57 {
58 return $this->type;
59 }
60
61 /**
62 * @return $this
63 */
64 public function setType(string $type): static
65 {
66 $this->type = $type;
67
68 return $this;
69 }
70
71 public function getRetry(): ?int
72 {
73 return $this->retry;
74 }
75
76 /**
77 * @return $this
78 */
79 public function setRetry(?int $retry): static
80 {
81 $this->retry = $retry;
82
83 return $this;
84 }
85
86 public function getId(): ?string
87 {
88 return $this->id;
89 }
90
91 /**
92 * @return $this
93 */
94 public function setId(string $id): static
95 {
96 $this->id = $id;
97
98 return $this;
99 }
100
101 public function getComment(): ?string
102 {
103 return $this->comment;
104 }
105
106 public function setComment(string $comment): static
107 {
108 $this->comment = $comment;
109
110 return $this;
111 }
112
113 /**
114 * @return \Traversable<string>
115 */
116 public function getIterator(): \Traversable
117 {
118 static $lastRetry = null;
119
120 $head = '';
121 if ($this->comment) {
122 $head .= \sprintf(': %s', $this->comment)."\n";
123 }
124 if ($this->id) {
125 $head .= \sprintf('id: %s', $this->id)."\n";
126 }
127 if ($this->retry > 0 && $this->retry !== $lastRetry) {
128 $head .= \sprintf('retry: %s', $lastRetry = $this->retry)."\n";
129 }
130 if ($this->type) {
131 $head .= \sprintf('event: %s', $this->type)."\n";
132 }
133 yield $head;
134
135 if (is_iterable($this->data)) {
136 foreach ($this->data as $data) {
137 yield \sprintf('data: %s', $data)."\n";
138 }
139 } elseif ('' !== $this->data) {
140 yield \sprintf('data: %s', $this->data)."\n";
141 }
142
143 yield "\n";
144 }
145 }