swisnl /
json-api-client
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Swis\JsonApi\Client\Parsers; |
||
| 6 | |||
| 7 | use Swis\JsonApi\Client\ErrorCollection; |
||
| 8 | use Swis\JsonApi\Client\Exceptions\ValidationException; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @internal |
||
| 12 | */ |
||
| 13 | class ErrorCollectionParser |
||
| 14 | { |
||
| 15 | private ErrorParser $errorParser; |
||
| 16 | |||
| 17 | 184 | public function __construct(ErrorParser $errorParser) |
|
| 18 | { |
||
| 19 | 184 | $this->errorParser = $errorParser; |
|
| 20 | 92 | } |
|
| 21 | |||
| 22 | /** |
||
| 23 | * @param mixed $data |
||
| 24 | */ |
||
| 25 | 36 | public function parse($data): ErrorCollection |
|
| 26 | { |
||
| 27 | 36 | if (! is_array($data)) { |
|
| 28 | 24 | throw new ValidationException(sprintf('ErrorCollection MUST be an array, "%s" given.', gettype($data))); |
|
| 29 | } |
||
| 30 | 12 | if (count($data) === 0) { |
|
| 31 | 4 | throw new ValidationException('ErrorCollection cannot be empty and MUST have at least one Error object.'); |
|
| 32 | } |
||
| 33 | |||
| 34 | 8 | return new ErrorCollection( |
|
| 35 | 8 | array_map( |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 36 | 8 | fn ($error) => $this->errorParser->parse($error), |
|
| 37 | 4 | $data |
|
| 38 | 4 | ) |
|
| 39 | 4 | ); |
|
| 40 | } |
||
| 41 | } |
||
| 42 |