1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace GusApi\Util; |
||
6 | |||
7 | use GusApi\Exception\InvalidServerResponseException; |
||
8 | use GusApi\Exception\NotFoundException; |
||
9 | use GusApi\Type\Response\SearchDataResponse; |
||
10 | use GusApi\Type\Response\SearchResponseCompanyData; |
||
11 | use GusApi\Type\Response\SearchResponseRaw; |
||
12 | use SimpleXMLElement; |
||
13 | |||
14 | class DataSearchDecoder |
||
15 | { |
||
16 | /** |
||
17 | * @throws InvalidServerResponseException |
||
18 | * @throws NotFoundException |
||
19 | */ |
||
20 | public static function decode(SearchResponseRaw $searchResponseRaw): SearchDataResponse |
||
21 | { |
||
22 | if ('' === $searchResponseRaw->getDaneSzukajPodmiotyResult()) { |
||
23 | return new SearchDataResponse(); |
||
24 | } |
||
25 | |||
26 | try { |
||
27 | $xmlElementsResponse = new SimpleXMLElement($searchResponseRaw->getDaneSzukajPodmiotyResult()); |
||
28 | } catch (\Exception $e) { |
||
29 | throw new InvalidServerResponseException('Invalid server response', 0, $e); |
||
30 | } |
||
31 | |||
32 | $elements = []; |
||
33 | |||
34 | foreach ($xmlElementsResponse->dane as $resultData) { |
||
35 | $elements[] = self::decodeSingleResult($resultData); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
36 | } |
||
37 | |||
38 | return new SearchDataResponse($elements); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @throws NotFoundException |
||
43 | */ |
||
44 | private static function decodeSingleResult(SimpleXMLElement $element): SearchResponseCompanyData |
||
45 | { |
||
46 | $result = new SearchResponseCompanyData(); |
||
47 | |||
48 | foreach ($element as $key => $item) { |
||
49 | if ('ErrorCode' === $key && 4 === (int) $item) { |
||
50 | throw new NotFoundException('No data found'); |
||
51 | } |
||
52 | |||
53 | $result->$key = (string) $item; |
||
54 | } |
||
55 | |||
56 | return $result; |
||
57 | } |
||
58 | } |
||
59 |