Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 2.1.3 under development

- New #107: Add `DataStream` (@vjik)
- New #107, #110: Add `DataStream` (@vjik)
- New #107: Add `FormatterInterface` and implementations: `HtmlFormatter`, `JsonFormatter`, `PlainTextFormatter`,
`XmlFormatter` (@vjik)
- New #107: Add `DataResponseFactoryInterface` and implementations: `DataResponseFactory`, `FormattedResponseFactory`,
Expand Down
20 changes: 20 additions & 0 deletions src/DataStream/DataStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public function hasFormatter(): bool
return $this->formatter !== null;
}

/**
* Returns the formatter.
*
* @return FormatterInterface|null The formatter or `null` if not set.
*/
public function getFormatter(): ?FormatterInterface
{
return $this->formatter;
}

/**
* Changes the formatter.
*
Expand All @@ -57,6 +67,16 @@ public function changeFormatter(FormatterInterface $formatter): void
$this->resetState();
}

/**
* Returns the raw data.
*
* @return mixed The raw data.
*/
public function getData(): mixed
{
return $this->data;
}

/**
* Changes the data.
*
Expand Down
8 changes: 7 additions & 1 deletion tests/DataStream/DataStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function testBase(): void
$stream = new DataStream('test data');

$this->assertFalse($stream->hasFormatter());
$this->assertNull($stream->getFormatter());
$this->assertSame('test data', $stream->getData());
}

public function testGetFormattedWithoutFormatter(): void
Expand All @@ -37,9 +39,11 @@ public function testGetFormattedWithoutFormatter(): void

public function testFormatter(): void
{
$stream = new DataStream('test', new JsonFormatter());
$formatter = new JsonFormatter();
$stream = new DataStream('test', $formatter);

$this->assertTrue($stream->hasFormatter());
$this->assertSame($formatter, $stream->getFormatter());
$this->assertSame('"test"', (string) $stream);
}

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

$this->assertTrue($stream->hasFormatter());
$this->assertSame($formatter, $stream->getFormatter());
$this->assertSame('"test"', (string) $stream);
}

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

$this->assertSame('world', (string) $stream);
$this->assertSame('world', $stream->getData());
}

public function testCloseStreamOnChangeData(): void
Expand Down
Loading