-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor namespaces and add request handling classes for improved HTTP processing. #48
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 all commits
Commits
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
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
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 TinyBlocks\Http\Internal\Request; | ||
|
|
||
| final readonly class Attribute | ||
| { | ||
| private function __construct(private mixed $value) | ||
| { | ||
| } | ||
|
|
||
| public static function from(mixed $value): Attribute | ||
| { | ||
| return new Attribute(value: $value); | ||
| } | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return match (true) { | ||
| is_array($this->value) => $this->value, | ||
| default => [] | ||
| }; | ||
| } | ||
|
|
||
| public function toFloat(): float | ||
| { | ||
| return match (true) { | ||
| is_scalar($this->value) => (float)$this->value, | ||
| default => 0.00 | ||
| }; | ||
| } | ||
|
|
||
| public function toString(): string | ||
| { | ||
| return match (true) { | ||
| is_scalar($this->value) => (string)$this->value, | ||
| default => '' | ||
| }; | ||
| } | ||
|
|
||
| public function toInteger(): int | ||
| { | ||
| return match (true) { | ||
| is_scalar($this->value) => (int)$this->value, | ||
| default => 0 | ||
| }; | ||
| } | ||
|
|
||
| public function toBoolean(): bool | ||
| { | ||
| return match (true) { | ||
| is_scalar($this->value) => (bool)$this->value, | ||
| default => false | ||
| }; | ||
| } | ||
| } |
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,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace TinyBlocks\Http\Internal\Request; | ||
|
|
||
| use Psr\Http\Message\ServerRequestInterface; | ||
| use TinyBlocks\Http\Internal\Stream\StreamFactory; | ||
|
|
||
| final readonly class Body | ||
| { | ||
| private function __construct(private array $data) | ||
| { | ||
| } | ||
|
|
||
| public static function from(ServerRequestInterface $request): Body | ||
| { | ||
| $body = $request->getBody(); | ||
| $streamFactory = StreamFactory::fromStream(stream: $body); | ||
|
|
||
| if ($streamFactory->isEmptyContent()) { | ||
| return new Body(data: []); | ||
| } | ||
|
|
||
| return new Body(data: json_decode($streamFactory->content(), true)); | ||
gustavofreze marked this conversation as resolved.
Show resolved
Hide resolved
gustavofreze marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public function get(string $key): Attribute | ||
| { | ||
| $value = ($this->data[$key] ?? null); | ||
|
|
||
| return Attribute::from(value: $value); | ||
| } | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return $this->data; | ||
| } | ||
| } | ||
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,17 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace TinyBlocks\Http\Internal\Request; | ||
|
|
||
| final readonly class DecodedRequest | ||
| { | ||
| private function __construct(public Uri $uri, public Body $body) | ||
| { | ||
| } | ||
|
|
||
| public static function from(Uri $uri, Body $body): DecodedRequest | ||
| { | ||
| return new DecodedRequest(uri: $uri, body: $body); | ||
| } | ||
| } |
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,24 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace TinyBlocks\Http\Internal\Request; | ||
|
|
||
| use Psr\Http\Message\ServerRequestInterface; | ||
|
|
||
| final readonly class Decoder | ||
| { | ||
| private function __construct(private Uri $uri, private Body $body) | ||
| { | ||
| } | ||
|
|
||
| public static function from(ServerRequestInterface $request): Decoder | ||
| { | ||
| return new Decoder(uri: Uri::from(request: $request), body: Body::from(request: $request)); | ||
| } | ||
|
|
||
| public function decode(): DecodedRequest | ||
| { | ||
| return DecodedRequest::from(uri: $this->uri, body: $this->body); | ||
| } | ||
| } |
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,37 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace TinyBlocks\Http\Internal\Request; | ||
|
|
||
| use Psr\Http\Message\ServerRequestInterface; | ||
|
|
||
| final readonly class Uri | ||
| { | ||
| private const string ROUTE = '__route__'; | ||
|
|
||
| private function __construct(private ServerRequestInterface $request, private string $routeAttributeName) | ||
| { | ||
| } | ||
|
|
||
| public static function from(ServerRequestInterface $request): Uri | ||
| { | ||
| return new Uri(request: $request, routeAttributeName: self::ROUTE); | ||
| } | ||
|
|
||
| public function route(string $name = self::ROUTE): Uri | ||
| { | ||
| return new Uri(request: $this->request, routeAttributeName: $name); | ||
| } | ||
|
|
||
| public function get(string $key): Attribute | ||
| { | ||
| $attribute = $this->request->getAttribute($this->routeAttributeName); | ||
|
|
||
| if (is_array($attribute)) { | ||
| return Attribute::from(value: $attribute[$key] ?? null); | ||
| } | ||
|
|
||
| return Attribute::from(value: $attribute); | ||
| } | ||
| } |
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
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.