-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/phanxipang/fansipan
- Loading branch information
Showing
21 changed files
with
486 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
|
@@ -6,7 +6,7 @@ parameters: | |
- src | ||
|
||
# The level 9 is the highest level | ||
level: 7 | ||
level: 8 | ||
|
||
tmpDir: build/phpstan | ||
|
||
|
This file contains 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 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 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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fansipan\Contracts; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* @template T of object | ||
*/ | ||
interface MapperInterface | ||
{ | ||
/** | ||
* Map the response to an object. | ||
* | ||
* @return ?T | ||
*/ | ||
public function map(ResponseInterface $response): ?object; | ||
} |
This file contains 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 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 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 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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fansipan\Mapper; | ||
|
||
use Fansipan\Contracts\DecoderInterface; | ||
use Fansipan\Contracts\MapperInterface; | ||
use Fansipan\Decoder\ChainDecoder; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* @template T of object | ||
* @implements MapperInterface<T> | ||
*/ | ||
final class GenericMapper implements DecoderInterface, MapperInterface | ||
{ | ||
/** | ||
* @var callable(iterable): ?T | ||
*/ | ||
private $onSuccess; | ||
|
||
/** | ||
* @var callable(iterable): ?T | ||
*/ | ||
private $onFailure; | ||
|
||
/** | ||
* @var DecoderInterface | ||
*/ | ||
private $decoder; | ||
|
||
/** | ||
* @param callable(iterable): ?T $onSuccess | ||
* @param callable(iterable): ?T $onFailure | ||
*/ | ||
public function __construct( | ||
callable $onSuccess, | ||
callable $onFailure, | ||
?DecoderInterface $decoder = null | ||
) { | ||
$this->onSuccess = $onSuccess; | ||
$this->onFailure = $onFailure; | ||
$this->decoder = $decoder ?? ChainDecoder::default(); | ||
} | ||
|
||
public function map(ResponseInterface $response): ?object | ||
{ | ||
$status = $response->getStatusCode(); | ||
$decoded = $this->decoder->decode($response); | ||
|
||
if ($status >= 200 && $status < 300) { | ||
return ($this->onSuccess)($decoded); | ||
} else { | ||
return ($this->onFailure)($decoded); | ||
} | ||
} | ||
|
||
public function decode(ResponseInterface $response): iterable | ||
{ | ||
return $this->decoder->decode($response); | ||
} | ||
} |
Oops, something went wrong.