Skip to content

Commit

Permalink
Merge pull request #7 from farzai/fix-build-error
Browse files Browse the repository at this point in the history
Use support package
  • Loading branch information
parsilver authored Dec 8, 2023
2 parents e2d7666 + 5054d6e commit a100898
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.2, 8.1, 8.0]
php: [8.3, 8.2, 8.1]
stability: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A HTTP client for Farzai Package.
You can install the package via composer:

```bash
composer require farzai/transport:dev-main
composer require farzai/transport
```

## Testing
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"farzai/support": "^1.1",
"guzzlehttp/guzzle": "^7.7",
"guzzlehttp/psr7": "^2.5",
"psr/http-client": "^1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function isSuccessfull(): bool;
/**
* Return the json decoded response.
*/
public function json(string $key = null): mixed;
public function json(?string $key = null): mixed;

/**
* Throw an exception if the response is not successfull.
Expand All @@ -39,7 +39,7 @@ public function json(string $key = null): mixed;
*
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
public function throw(callable $callback = null);
public function throw(?callable $callback = null);

/**
* Return the psr request.
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ResponseExceptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ResponseExceptionFactory
/**
* Create a new exception instance.
*/
public static function create(ResponseInterface $response, Throwable $previous = null)
public static function create(ResponseInterface $response, ?Throwable $previous = null)
{
$statusCode = $response->statusCode();

Expand Down
7 changes: 4 additions & 3 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Farzai\Transport;

use Farzai\Support\Arr;
use Farzai\Transport\Contracts\ResponseInterface;
use Farzai\Transport\Exceptions\ResponseExceptionFactory;
use Farzai\Transport\Traits\PsrResponseTrait;
Expand Down Expand Up @@ -73,7 +74,7 @@ public function isSuccessfull(): bool
/**
* Return the json decoded response.
*/
public function json(string $key = null): mixed
public function json(?string $key = null): mixed
{
if (is_null($this->jsonDecoded)) {
$this->jsonDecoded = @json_decode($this->body(), true) ?: false;
Expand All @@ -87,7 +88,7 @@ public function json(string $key = null): mixed
return $this->jsonDecoded;
}

return $this->jsonDecoded[$key] ?? null;
return Arr::get($this->jsonDecoded, $key);
}

/**
Expand All @@ -98,7 +99,7 @@ public function json(string $key = null): mixed
*
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
public function throw(callable $callback = null)
public function throw(?callable $callback = null)
{
$callback = $callback ?? function (ResponseInterface $response, ?\Exception $e) {
if (! $this->isSuccessfull()) {
Expand Down
16 changes: 15 additions & 1 deletion tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@
expect($response->json())->toBe(['foo' => 'bar']);
});

it('can get json body with dot notation', function () {
$stream = $this->createMock(StreamInterface::class);
$stream->method('getContents')->willReturn('{"foo":{"bar":"baz"}}');

$baseResponse = $this->createMock(ResponseInterface::class);
$baseResponse->method('getBody')->willReturn($stream);

$response = new Response(
$this->createMock(RequestInterface::class),
$baseResponse,
);

expect($response->json('foo.bar'))->toBe('baz');
});

it('cannot get json when invalid json format', function () {
$stream = $this->createMock(StreamInterface::class);
$stream->method('getContents')->willReturn('{"foo":"bar"');
Expand Down Expand Up @@ -116,7 +131,6 @@
$response = new Response(
$this->createMock(RequestInterface::class),
$baseResponse,

);

$response->throw();
Expand Down

0 comments on commit a100898

Please sign in to comment.