|
2 | 2 |
|
3 | 3 | namespace Vormkracht10\KvKApi;
|
4 | 4 |
|
| 5 | +use GuzzleHttp\ClientInterface; |
| 6 | +use GuzzleHttp\Psr7\Response; |
5 | 7 | use Illuminate\Support\Collection;
|
| 8 | +use Psr\Http\Message\ResponseInterface; |
| 9 | +use stdClass; |
6 | 10 | use Vormkracht10\KvKApi\Company\Company;
|
7 | 11 |
|
8 | 12 | class Client
|
9 | 13 | {
|
10 |
| - private $httpClient; |
11 |
| - private $baseUrl; |
12 |
| - private array $results; |
13 |
| - private int $page; |
14 |
| - private int $resultsPerPage; |
15 |
| - |
16 |
| - public function __construct($httpClient) |
| 14 | + private ClientInterface $httpClient; |
| 15 | + private string $baseUrl; |
| 16 | + /** @var array<Company> */ |
| 17 | + private array $results = []; |
| 18 | + private int $page = 1; |
| 19 | + private int $resultsPerPage = 10; |
| 20 | + |
| 21 | + public function __construct(ClientInterface $httpClient) |
17 | 22 | {
|
18 | 23 | $this->httpClient = $httpClient;
|
19 | 24 | $this->baseUrl = 'https://api.kvk.nl/api/v2/';
|
20 | 25 | }
|
21 | 26 |
|
22 |
| - public function search(string $search, array $params = []) |
| 27 | + /** |
| 28 | + * @param array<string, mixed> $params |
| 29 | + * @return array<Company> |
| 30 | + */ |
| 31 | + public function search(string $search, array $params = []): array |
23 | 32 | {
|
24 | 33 | $queryParams = array_merge([
|
25 | 34 | 'naam' => $search,
|
26 |
| - 'pagina' => $this->page ?? 1, |
27 |
| - 'resultatenPerPagina' => $this->resultsPerPage ?? 10, |
| 35 | + 'pagina' => $this->page, |
| 36 | + 'resultatenPerPagina' => $this->resultsPerPage, |
28 | 37 | ], $params);
|
29 | 38 | $data = $this->getData($queryParams);
|
30 | 39 |
|
31 | 40 | $parsedData = $this->parseData($this->decodeJson($data));
|
32 | 41 |
|
33 |
| - $parsedData->each(function ($item) { |
34 |
| - $data = json_decode($this->getRelatedData($item)); |
| 42 | + foreach ($parsedData as $item) { |
| 43 | + $data = $this->decodeJson($this->getRelatedData($item)); |
35 | 44 |
|
36 | 45 | $this->results[] = new Company(
|
37 |
| - $data->kvkNummer ?? null, |
| 46 | + $data->kvkNummer ?? '', |
38 | 47 | $data->vestigingsnummer ?? null,
|
39 | 48 | $data->naam ?? null,
|
40 | 49 | $data->adres ?? null,
|
41 | 50 | $data->websites ?? null
|
42 | 51 | );
|
43 |
| - }); |
| 52 | + }; |
44 | 53 |
|
45 | 54 | return $this->results;
|
46 | 55 | }
|
47 | 56 |
|
48 |
| - private function getData(array $params) |
| 57 | + /** |
| 58 | + * @param array<string, mixed> $params |
| 59 | + */ |
| 60 | + private function getData(array $params): string |
49 | 61 | {
|
50 | 62 | $url = $this->baseUrl . 'zoeken?' . http_build_query($params);
|
51 | 63 |
|
52 |
| - $response = $this->httpClient->get($url); |
| 64 | + $response = $this->httpClient->request('GET', $url); |
53 | 65 |
|
54 | 66 | return $this->getJson($response);
|
55 | 67 | }
|
56 | 68 |
|
57 |
| - private function getJson(object $response) |
| 69 | + private function getJson(ResponseInterface $response): string |
58 | 70 | {
|
59 |
| - return $response->getBody()->getContents(); |
| 71 | + return (string) $response->getBody()->getContents(); |
60 | 72 | }
|
61 | 73 |
|
62 |
| - private function decodeJson(string $json) |
| 74 | + /** |
| 75 | + * @return stdClass |
| 76 | + */ |
| 77 | + private function decodeJson(string $json): stdClass |
63 | 78 | {
|
64 |
| - return json_decode($json); |
| 79 | + return json_decode($json, false) ?: new stdClass(); |
65 | 80 | }
|
66 | 81 |
|
67 |
| - public function setPage(int $page) |
| 82 | + public function setPage(int $page): self |
68 | 83 | {
|
69 | 84 | $this->page = $page;
|
70 | 85 |
|
71 | 86 | return $this;
|
72 | 87 | }
|
73 | 88 |
|
74 |
| - public function setResultsPerPage(int $resultsPerPage) |
| 89 | + public function setResultsPerPage(int $resultsPerPage): self |
75 | 90 | {
|
76 | 91 | $this->resultsPerPage = $resultsPerPage;
|
77 | 92 |
|
78 | 93 | return $this;
|
79 | 94 | }
|
80 | 95 |
|
81 |
| - public function searchByKvkNumber(string $kvkNumber, array $params = []) |
| 96 | + /** |
| 97 | + * @param array<string, mixed> $params |
| 98 | + * @return array<Company> |
| 99 | + */ |
| 100 | + public function searchByKvkNumber(string $kvkNumber, array $params = []): array |
82 | 101 | {
|
83 | 102 | return $this->search('', array_merge(['kvkNummer' => $kvkNumber], $params));
|
84 | 103 | }
|
85 | 104 |
|
86 |
| - public function searchByRsin(string $rsin, array $params = []) |
| 105 | + /** |
| 106 | + * @param array<string, mixed> $params |
| 107 | + * @return array<Company> |
| 108 | + */ |
| 109 | + public function searchByRsin(string $rsin, array $params = []): array |
87 | 110 | {
|
88 | 111 | return $this->search('', array_merge(['rsin' => $rsin], $params));
|
89 | 112 | }
|
90 | 113 |
|
91 |
| - public function searchByVestigingsnummer(string $vestigingsnummer, array $params = []) |
| 114 | + /** |
| 115 | + * @param array<string, mixed> $params |
| 116 | + * @return array<Company> |
| 117 | + */ |
| 118 | + public function searchByVestigingsnummer(string $vestigingsnummer, array $params = []): array |
92 | 119 | {
|
93 | 120 | return $this->search('', array_merge(['vestigingsnummer' => $vestigingsnummer], $params));
|
94 | 121 | }
|
95 | 122 |
|
96 |
| - private function parseData(object $data) |
| 123 | + /** |
| 124 | + * @return array<int, stdClass> |
| 125 | + */ |
| 126 | + private function parseData(stdClass $data): array |
97 | 127 | {
|
98 |
| - $data = collect($data->resultaten); |
| 128 | + $resultaten = $data->resultaten ?? []; |
| 129 | + /** @var array<int, stdClass> $resultatenArray */ |
| 130 | + $resultatenArray = is_array($resultaten) ? $resultaten : []; |
99 | 131 |
|
100 |
| - $data = $data->map(function ($value) { |
| 132 | + return array_map(function ($value) { |
101 | 133 | $value = (object) $value;
|
102 |
| - $value->attributes = collect((array) $value)->except(['type', 'links']); |
| 134 | + /** @var array<string, mixed> $attributes */ |
| 135 | + $attributes = array_diff_key((array) $value, array_flip(['type', 'links'])); |
| 136 | + $value->attributes = $attributes; |
103 | 137 | $value->id = uniqid();
|
104 | 138 |
|
105 | 139 | if (isset($value->links)) {
|
106 |
| - $links = collect($value->links); |
107 |
| - $links = $links->mapWithKeys(function ($linkObj) { |
108 |
| - return [$linkObj->rel => $linkObj->href]; |
109 |
| - }); |
110 |
| - $value->links = $links; |
| 140 | + /** @var array<stdClass> $links */ |
| 141 | + $links = $value->links; |
| 142 | + /** @var array<string, string> $mappedLinks */ |
| 143 | + $mappedLinks = array_column($links, 'href', 'rel'); |
| 144 | + $value->links = $mappedLinks; |
111 | 145 | } else {
|
112 |
| - $value->links = collect(); |
| 146 | + /** @var array<string, string> $emptyLinks */ |
| 147 | + $emptyLinks = []; |
| 148 | + $value->links = $emptyLinks; |
113 | 149 | }
|
114 | 150 |
|
115 | 151 | $value->actief = $value->actief ?? null;
|
116 | 152 | $value->vervallenNaam = $value->vervallenNaam ?? null;
|
117 | 153 |
|
118 | 154 | return $value;
|
119 |
| - }); |
120 |
| - |
121 |
| - return $data; |
| 155 | + }, $resultatenArray); |
122 | 156 | }
|
123 | 157 |
|
124 |
| - private function getRelatedData($parsedData): Collection |
| 158 | + private function getRelatedData(stdClass $parsedData): string |
125 | 159 | {
|
126 |
| - $relatedData = collect(); |
| 160 | + $relatedData = []; |
| 161 | + |
| 162 | + /** @var Collection<string, string> $links */ |
| 163 | + $links = collect((array)($parsedData->links ?? [])); |
127 | 164 |
|
128 |
| - collect($parsedData->links)->each(function ($link, $key) use (&$relatedData) { |
129 |
| - $response = $this->httpClient->get($link); |
| 165 | + $links->each(function (string $link) use (&$relatedData) { |
| 166 | + $response = $this->httpClient->request('GET', $link); |
130 | 167 |
|
131 | 168 | $data = $this->decodeJson($this->getJson($response));
|
132 | 169 |
|
133 |
| - $relatedData = $relatedData->merge($data); |
| 170 | + $relatedData = array_merge($relatedData, (array) $data); |
134 | 171 | });
|
135 | 172 |
|
136 |
| - return $relatedData; |
| 173 | + return json_encode($relatedData) ?: '{}'; |
137 | 174 | }
|
138 | 175 | }
|
0 commit comments