Skip to content

Commit 6474161

Browse files
authored
Add getFormatter() and getData() methods to DataStream (#110)
1 parent 3a4caff commit 6474161

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 2.1.3 under development
44

5-
- New #107: Add `DataStream` (@vjik)
5+
- New #107, #110: Add `DataStream` (@vjik)
66
- New #107: Add `FormatterInterface` and implementations: `HtmlFormatter`, `JsonFormatter`, `PlainTextFormatter`,
77
`XmlFormatter` (@vjik)
88
- New #107: Add `DataResponseFactoryInterface` and implementations: `DataResponseFactory`, `FormattedResponseFactory`,

src/DataStream/DataStream.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ public function hasFormatter(): bool
4646
return $this->formatter !== null;
4747
}
4848

49+
/**
50+
* Returns the formatter.
51+
*
52+
* @return FormatterInterface|null The formatter or `null` if not set.
53+
*/
54+
public function getFormatter(): ?FormatterInterface
55+
{
56+
return $this->formatter;
57+
}
58+
4959
/**
5060
* Changes the formatter.
5161
*
@@ -57,6 +67,16 @@ public function changeFormatter(FormatterInterface $formatter): void
5767
$this->resetState();
5868
}
5969

70+
/**
71+
* Returns the raw data.
72+
*
73+
* @return mixed The raw data.
74+
*/
75+
public function getData(): mixed
76+
{
77+
return $this->data;
78+
}
79+
6080
/**
6181
* Changes the data.
6282
*

tests/DataStream/DataStreamTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public function testBase(): void
2424
$stream = new DataStream('test data');
2525

2626
$this->assertFalse($stream->hasFormatter());
27+
$this->assertNull($stream->getFormatter());
28+
$this->assertSame('test data', $stream->getData());
2729
}
2830

2931
public function testGetFormattedWithoutFormatter(): void
@@ -37,9 +39,11 @@ public function testGetFormattedWithoutFormatter(): void
3739

3840
public function testFormatter(): void
3941
{
40-
$stream = new DataStream('test', new JsonFormatter());
42+
$formatter = new JsonFormatter();
43+
$stream = new DataStream('test', $formatter);
4144

4245
$this->assertTrue($stream->hasFormatter());
46+
$this->assertSame($formatter, $stream->getFormatter());
4347
$this->assertSame('"test"', (string) $stream);
4448
}
4549

@@ -51,6 +55,7 @@ public function testChangeFormatter(): void
5155
$stream->changeFormatter($formatter);
5256

5357
$this->assertTrue($stream->hasFormatter());
58+
$this->assertSame($formatter, $stream->getFormatter());
5459
$this->assertSame('"test"', (string) $stream);
5560
}
5661

@@ -73,6 +78,7 @@ public function testChangeData(): void
7378
$stream->changeData('world');
7479

7580
$this->assertSame('world', (string) $stream);
81+
$this->assertSame('world', $stream->getData());
7682
}
7783

7884
public function testCloseStreamOnChangeData(): void

0 commit comments

Comments
 (0)