-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Add ReadEvents with the associated dependencies and the test client file was split #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a766032
chore: Add .idea to .gitignore
ca127a4
chore: rectorphp was implemented and the following adjustments
cad1ad3
feat: Add ReadEvents with the associated dependencies and the test cl…
6c0326e
chore: remove .idea from .gitignore and clean up configuration files
28b7de2
chore: ractor config adjustement - skip SimplifyUselessVariableRector
484bdd4
feat: refactoring the ReadEventsOptions jsonSerialize method
5cce131
chore: replace iterator_to_array with iterator_count in write and rea…
2329365
feat: implement ReadEventLine and Utils for reading NDJSON streams
c1a6660
Merge branch 'main' into reading
3e9b132
Merge branch 'thenativeweb:main' into main
wundii ff364c4
Merge branch 'main' into reading
bafe2c5
chore: remove commented-out blueprint code from Utils.php
0f64f0c
chore: rename Utils.php to NdJson.php and update method references
b54e2f1
test: add unit tests for NdJson stream reading functionality
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Thenativeweb\Eventsourcingdb; | ||
|
|
||
| use JsonSerializable; | ||
|
|
||
| enum BoundType: string | ||
| { | ||
| case INCLUSIVE = 'inclusive'; | ||
| case EXCLUSIVE = 'exclusive'; | ||
| } | ||
|
|
||
| class Bound implements JsonSerializable | ||
| { | ||
| public function __construct( | ||
| public string $id, | ||
| public BoundType $type, | ||
| ) { | ||
| } | ||
|
|
||
| public function jsonSerialize(): array | ||
| { | ||
| return [ | ||
| 'id' => $this->id, | ||
| 'type' => $this->type->value, | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Thenativeweb\Eventsourcingdb; | ||
|
|
||
| use JsonSerializable; | ||
|
|
||
| enum Order: string | ||
| { | ||
| case CHRONOLOGICAL = 'chronological'; | ||
| case ANTICHRONOLOGICAL = 'antichronological'; | ||
| } | ||
|
|
||
| enum ReadIfEventIsMissing: string | ||
| { | ||
| case READ_NOTHING = 'read-nothing'; | ||
| case READ_EVERYTHING = 'read-everything'; | ||
| } | ||
|
|
||
| class ReadFromLatestEvent implements JsonSerializable | ||
| { | ||
| public function __construct( | ||
| public string $subject, | ||
| public string $type, | ||
| public ReadIfEventIsMissing $ifEventIsMissing, | ||
| ) { | ||
| } | ||
|
|
||
| public function jsonSerialize(): array | ||
| { | ||
| return [ | ||
| 'subject' => $this->subject, | ||
| 'type' => $this->type, | ||
| 'ifEventIsMissing' => $this->ifEventIsMissing->value, | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| class ReadEventsOptions implements JsonSerializable | ||
| { | ||
| public function __construct( | ||
| public bool $recursive = false, | ||
| public ?Order $order = null, | ||
| public ?Bound $lowerBound = null, | ||
| public ?Bound $upperBound = null, | ||
| public ?ReadFromLatestEvent $fromLatestEvent = null | ||
| ) { | ||
| } | ||
|
|
||
| public function jsonSerialize(): mixed | ||
| { | ||
| $result = [ | ||
| 'recursive' => $this->recursive, | ||
| ]; | ||
|
|
||
| if ($this->order instanceof Order) { | ||
| $result['order'] = $this->order->value; | ||
| } | ||
|
|
||
| if ($this->lowerBound instanceof Bound) { | ||
| $result['lowerBound'] = $this->lowerBound->jsonSerialize(); | ||
| } | ||
|
|
||
| if ($this->upperBound instanceof Bound) { | ||
| $result['upperBound'] = $this->upperBound->jsonSerialize(); | ||
| } | ||
|
|
||
| if ($this->fromLatestEvent instanceof ReadFromLatestEvent) { | ||
| $result['fromLatestEvent'] = $this->fromLatestEvent->jsonSerialize(); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Thenativeweb\Eventsourcingdb\Stream; | ||
|
|
||
| final readonly class ReadEventLine | ||
| { | ||
| public function __construct( | ||
| public string $type, | ||
| public array $payload, | ||
| ) { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Thenativeweb\Eventsourcingdb\Stream; | ||
|
|
||
| use Psr\Http\Message\StreamInterface; | ||
| use RuntimeException; | ||
|
|
||
| final readonly class Utils | ||
wundii marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| public static function readLine(StreamInterface $stream): string | ||
| { | ||
| $buffer = ''; | ||
|
|
||
| while (!$stream->eof()) { | ||
| if ('' === ($byte = $stream->read(1))) { | ||
| return $buffer; | ||
| } | ||
|
|
||
| $buffer .= $byte; | ||
| if ($byte === "\n") { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return $buffer; | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<ReadEventLine> | ||
| */ | ||
wundii marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public static function readNdJson(StreamInterface $stream): iterable | ||
| { | ||
| while (!$stream->eof()) { | ||
| $line = self::readLine($stream); | ||
| if ($line === '') { | ||
| continue; | ||
| } | ||
|
|
||
| if (!json_validate($line)) { | ||
| throw new RuntimeException('Failed to read events.'); | ||
| } | ||
|
|
||
| $item = json_decode($line, true); | ||
| if (!is_array($item)) { | ||
| throw new RuntimeException('Failed to read events, expected an array.'); | ||
| } | ||
|
|
||
| $eventLine = new ReadEventLine( | ||
| $item['type'] ?? 'unknown', | ||
| $item['payload'] ?? [], | ||
| ); | ||
| yield $eventLine; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.