Skip to content

Commit

Permalink
Merge branch 'main' into connector-configurator
Browse files Browse the repository at this point in the history
  • Loading branch information
jenky committed Jun 19, 2023
2 parents 4a22b71 + 9f11d79 commit 254193f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/advanced/response-decoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The decoder should be configured as per-request basis. By default `Jenky\Atlas\R

### Creating Custom Decoder

To create a custom decoder, you need to implement [`DecoderInterface`](https://github.com/jenky/atlas/blob/main/src/Contracts/DecoderInterface.php) which defines the structure that a decoder must have. The contract contains two methods: `supports` and `decode` where you can implement your own logic to decode the response body. Then you can start using it in your request.
To create a custom decoder, you need to implement [`DecoderInterface`](https://github.com/jenky/atlas/blob/main/src/Contracts/DecoderInterface.php) which defines the structure that a decoder must have. The contract contains only one method: `decode` where you can implement your own logic to decode the response body. Then you can start using it in your request.

```php
use Jenky\Atlas\Contracts\DecoderInterface;
Expand Down
7 changes: 2 additions & 5 deletions src/Contracts/DecoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

interface DecoderInterface
{
/**
* Determine wether decoder is supported for given response.
*/
public function supports(ResponseInterface $response): bool;

/**
* Decode response body to native array type.
*
* Throw \Jenky\Atlas\Exception\NotDecodableException if decoder is unable to decode the response.
*/
public function decode(ResponseInterface $response): array;
}
13 changes: 4 additions & 9 deletions src/Decoder/ChainDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,19 @@ public function __construct(iterable $decoders)
$this->decoders = $decoders;
}

public function supports(ResponseInterface $request): bool
{
return iterator_count($this->decoders) > 0;
}

/**
* {@inheritdoc}
*
* @throws \Jenky\Atlas\Exception\NotDecodableException
*/
public function decode(ResponseInterface $response): array
{
foreach ($this->decoders as $decoder) {
if ($decoder->supports($response)) {
try {
return $decoder->decode($response);
} catch (NotDecodableException $e) {
continue;
}
}

throw new NotDecodableException('Unable to decode the response body.');
throw NotDecodableException::create();
}
}
16 changes: 12 additions & 4 deletions src/Decoder/JsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@
namespace Jenky\Atlas\Decoder;

use Jenky\Atlas\Contracts\DecoderInterface;
use Jenky\Atlas\Exception\NotDecodableException;
use Psr\Http\Message\ResponseInterface;

final class JsonDecoder implements DecoderInterface
{
public function supports(ResponseInterface $response): bool
/**
* @throws \Jenky\Atlas\Exception\NotDecodableException
*/
public function decode(ResponseInterface $response): array
{
return mb_strpos($response->getHeaderLine('Content-Type'), 'json') !== false;
if (! $this->supports($response)) {
throw NotDecodableException::create();
}

return json_decode((string) $response->getBody(), true) ?? [];
}

public function decode(ResponseInterface $response): array
private function supports(ResponseInterface $response): bool
{
return json_decode((string) $response->getBody(), true) ?? [];
return mb_strpos($response->getHeaderLine('Content-Type'), 'json') !== false;
}
}
18 changes: 13 additions & 5 deletions src/Decoder/XmlDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
namespace Jenky\Atlas\Decoder;

use Jenky\Atlas\Contracts\DecoderInterface;
use Jenky\Atlas\Exception\NotDecodableException;
use Psr\Http\Message\ResponseInterface;

final class XmlDecoder implements DecoderInterface
{
public function supports(ResponseInterface $response): bool
{
return mb_strpos($response->getHeaderLine('Content-Type'), 'xml') !== false;
}

/**
* @throws \Jenky\Atlas\Exception\NotDecodableException
*/
public function decode(ResponseInterface $response): array
{
if (! $this->supports($response)) {
throw NotDecodableException::create();
}

$xml = simplexml_load_string((string) $response->getBody());

if (! $xml) {
Expand All @@ -26,4 +29,9 @@ public function decode(ResponseInterface $response): array
json_encode($xml) ?: '[]', true
);
}

private function supports(ResponseInterface $response): bool
{
return mb_strpos($response->getHeaderLine('Content-Type'), 'xml') !== false;
}
}
4 changes: 4 additions & 0 deletions src/Exception/NotDecodableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

class NotDecodableException extends \LogicException
{
public static function create(string $message = 'Unable to decode the response body.'): self
{
return new self($message);
}
}
6 changes: 2 additions & 4 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,15 @@ public function data(): array
* Decode the response body.
*
* @return array<array-key, mixed>
*
* @throws \Jenky\Atlas\Exception\NotDecodableException
*/
private function decode(): array
{
if (! $this->decoder instanceof DecoderInterface) {
return [];
}

if (! $this->decoder->supports($this->response)) {
return [];
}

return $this->decoder->decode($this->response);
}

Expand Down

0 comments on commit 254193f

Please sign in to comment.