Skip to content

Commit

Permalink
Support generic
Browse files Browse the repository at this point in the history
  • Loading branch information
jenky committed Jan 2, 2024
1 parent d8dec9a commit 1161401
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parameters:
- src

# The level 9 is the highest level
level: 7
level: 8

tmpDir: build/phpstan

Expand Down
4 changes: 4 additions & 0 deletions src/Contracts/ConnectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function middleware(): Middleware;
* Send the given request.
*
* The request and response should be processed through middleware.
*
* @template T of object
* @param Request<T> $request
* @return Response<T>
*/
public function send(Request $request): Response;
}
3 changes: 3 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Fansipan\Contracts\PayloadInterface;
use Fansipan\Decoder\ChainDecoder;

/**
* @template T of object
*/
abstract class Request
{
/**
Expand Down
11 changes: 10 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use LogicException;
use Psr\Http\Message\ResponseInterface;

/**
* @template T of object
*/
final class Response implements \ArrayAccess, \JsonSerializable, \Stringable
{
use Traits\Macroable;
Expand Down Expand Up @@ -62,14 +65,16 @@ public function data(): array

/**
* Get the decoded body of the response as an object.
*
* @return ?T
*/
public function object(): ?object
{
if (! $this->decoder instanceof MapperInterface) {
return null;
}

return $this->decoder->map($this->response);
return $this->decoder->map($this->response); // @phpstan-ignore-line
}

/**
Expand Down Expand Up @@ -232,6 +237,10 @@ public function throw(): self
if ($this->failed()) {
$exception = $this->toException();

if (! $exception) {
return $this;
}

if ($callback && \is_callable($callback)) {
$callback($this, $exception);
}
Expand Down
7 changes: 1 addition & 6 deletions src/Traits/ConnectorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,13 @@ public static function baseUri(): ?string
return null;
}

/**
* Send the given request.
*
* The request and response should be processed through middleware.
*/
public function send(Request $request): Response
{
$response = $this->sendRequest(
Util::request($request, static::baseUri())
);

return new Response($response, $request->decoder());
return new Response($response, $request->decoder()); // @phpstan-ignore-line
}

public function sendRequest(RequestInterface $request): ResponseInterface
Expand Down
3 changes: 3 additions & 0 deletions tests/Services/JsonPlaceholder/GetUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Fansipan\Request;
use Fansipan\Util;

/**
* @extends Request<User>
*/
final class GetUserRequest extends Request
{
private $id;
Expand Down

0 comments on commit 1161401

Please sign in to comment.