Skip to content

Commit 60549b4

Browse files
committed
Support multi-line comments
Comments are now stored as a single (optionally multi-line) string, instead of an array of strings, to make it more consistent with the data field.
1 parent 311eb57 commit 60549b4

File tree

5 files changed

+58
-23
lines changed

5 files changed

+58
-23
lines changed

src/BufferedEventStream.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public function disconnect(WritableStreamInterface $stream): void
6262
*/
6363
public function send(Event $event): void
6464
{
65-
// Skip buffering events that only consist of comments, such as the
65+
// Skip buffering events that only consist of a comment, such as the
6666
// keep-alive event.
67-
if (! $event->consistsOnlyOfComments()) {
67+
if (! $event->consistsOnlyOfComment()) {
6868
$this->lastEventId++;
6969

7070
$event = $event->id($this->lastEventId);

src/Encoder.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ class Encoder
1010
/**
1111
* Encode given fields into an event.
1212
*
13-
* @param array<string> $comments
13+
* @param string $comment
1414
* @param string|null $event
1515
* @param string $data
1616
* @param string $id
1717
* @param int|null $retry
1818
* @return string
1919
*/
2020
public function __invoke(
21-
array $comments,
21+
string $comment,
2222
?string $event,
2323
string $data,
2424
string $id,
2525
?int $retry
2626
): string {
2727
$encoded = '';
2828

29-
foreach ($comments as $comment) {
30-
$encoded .= ":{$comment}\n";
29+
foreach ($this->explodeLineBreaks($comment) as $line) {
30+
$encoded .= ":{$line}\n";
3131
}
3232

3333
if (! is_null($event)) {
@@ -50,11 +50,11 @@ public function __invoke(
5050
}
5151

5252
/**
53-
* Line breaks within a data field are not permitted, because they would indicate the end of a field (or even a
54-
* block). Therefore every line break should result in an additional data field.
53+
* Line breaks within comments and data fields are not permitted, because they would indicate the end of a field
54+
* (or even a block). Therefore every line break should result in an additional comment or data field.
5555
*
5656
* @param string $data
57-
* @return array
57+
* @return array<string>
5858
*/
5959
protected function explodeLineBreaks(string $data): array
6060
{

src/Event.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ final class Event
1010
/** @var string|null */
1111
private $event;
1212

13-
/** @var array<string> */
14-
private $comments = [];
13+
/** @var string */
14+
private $comment = '';
1515

1616
/** @var string */
1717
private $data = '';
@@ -28,23 +28,23 @@ public function __construct(?string $event = null)
2828
}
2929

3030
/**
31-
* Append one or more comments.
31+
* Set the event comment, supports line breaks.
3232
* Do not prefix comments with a colon – this is done by the encoder.
3333
*
34-
* @param string ...$comments
34+
* @param string $comment
3535
* @return self
3636
*/
37-
public function comment(string ...$comments): self
37+
public function comment(string $comment): self
3838
{
3939
$clone = clone $this;
4040

41-
array_push($clone->comments, ...$comments);
41+
$clone->comment = $comment;
4242

4343
return $clone;
4444
}
4545

4646
/**
47-
* Set the event data.
47+
* Set the event data, supports line breaks.
4848
*
4949
* @param string $data
5050
* @return $this
@@ -96,7 +96,7 @@ public function retry(?int $retry): self
9696
public function toString(Encoder $encoder): string
9797
{
9898
return $encoder(
99-
$this->comments,
99+
$this->comment,
100100
$this->event,
101101
$this->data,
102102
$this->id,
@@ -105,16 +105,16 @@ public function toString(Encoder $encoder): string
105105
}
106106

107107
/**
108-
* Determine if this event consists only of comments.
108+
* Determine if this event consists only of a comment.
109109
*
110110
* @return bool
111111
*/
112-
public function consistsOnlyOfComments(): bool
112+
public function consistsOnlyOfComment(): bool
113113
{
114114
return is_null($this->event)
115115
&& is_null($this->retry)
116116
&& $this->data === ''
117-
&& $this->comments !== [];
117+
&& $this->comment !== '';
118118
}
119119
}
120120

tests/EncoderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ public function single_comment()
8383
}
8484

8585
/** @test */
86-
public function multiple_comments()
86+
public function multi_line_comments()
8787
{
8888
$event = (new Event())->comment(
89-
'This is a comment',
90-
'And this too.'
89+
"This is a comment\n" .
90+
"And this too."
9191
);
9292

9393
self::assertEquals(

tests/EventTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Devfrey\FrameworkX\EventSource\Event;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class EventTest extends TestCase
9+
{
10+
/** @test */
11+
public function consistsOnlyOfComment_returns_true_for_an_event_with_only_a_comment()
12+
{
13+
$event = (new Event())->comment('Test');
14+
15+
self::assertTrue($event->consistsOnlyOfComment());
16+
}
17+
18+
/** @test */
19+
public function consistsOnlyOfComment_returns_false_for_an_event_with_data()
20+
{
21+
$event = (new Event())
22+
->data('foo')
23+
->comment('bar');
24+
25+
self::assertFalse($event->consistsOnlyOfComment());
26+
}
27+
28+
/** @test */
29+
public function consistsOnlyOfComment_returns_false_for_an_empty_event()
30+
{
31+
$event = new Event();
32+
33+
self::assertFalse($event->consistsOnlyOfComment());
34+
}
35+
}

0 commit comments

Comments
 (0)