Skip to content

Commit 62b9669

Browse files
author
Mike van den Hoek
committed
feat: supplement embedded data
1 parent 9fae22c commit 62b9669

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

src/PrefillGravityForms/Controllers/BaseController.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,26 @@ protected function createExpandArguments(string $expand): string
278278
return urldecode(http_build_query(['expand' => $imploded], '', ','));
279279
}
280280

281+
/**
282+
* Supplements the '_embedded' array with additional data retrieved via the '_links' array,
283+
* using the configured expand arguments to fetch and merge related resources.
284+
*/
285+
protected function supplementEmbeddedByLinks(array $apiResponse, string $embedType = '', string $doelBinding = ''): array
286+
{
287+
if (! isset($apiResponse['_links'][$embedType])) {
288+
return $apiResponse;
289+
}
290+
291+
foreach ($apiResponse['_links'][$embedType] as $key => $embeddedItem) {
292+
$response = $this->requestEmbedded($embeddedItem['href'], $doelBinding);
293+
$apiResponse['_embedded'][$embedType][$key] = array_merge($apiResponse['_embedded'][$embedType][$key], $response);
294+
}
295+
296+
return $apiResponse;
297+
}
298+
299+
abstract protected function requestEmbedded(string $url, string $doelBinding): array;
300+
281301
protected function getCurlHeaders(string $doelBinding = ''): array
282302
{
283303
$headers = [

src/PrefillGravityForms/Controllers/EnableUController.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ protected function fetchApiResponse(string $bsn, string $doelBinding = '', strin
6565
return [];
6666
}
6767

68+
foreach (array_filter(explode(',', $expand)) as $expandItem) {
69+
$apiResponse = $this->supplementEmbeddedByLinks($apiResponse, trim($expandItem), $doelBinding);
70+
}
71+
dd($apiResponse);
72+
6873
return $apiResponse;
6974
}
7075

@@ -77,4 +82,14 @@ protected function request(string $bsn = '', string $doelBinding = '', string $e
7782

7883
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
7984
}
85+
86+
protected function requestEmbedded(string $url, string $doelBinding): array
87+
{
88+
$curlArgs = [
89+
CURLOPT_URL => $url,
90+
CURLOPT_HTTPHEADER => $this->getCurlHeaders($doelBinding),
91+
];
92+
93+
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($url));
94+
}
8095
}

src/PrefillGravityForms/Controllers/PinkRoccadeController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ protected function fetchApiResponse(string $bsn, string $doelBinding = '', strin
6565
return [];
6666
}
6767

68+
foreach (array_filter(explode(',', $expand)) as $expandItem) {
69+
$apiResponse = $this->supplementEmbeddedByLinks($apiResponse, trim($expandItem), $doelBinding);
70+
}
71+
dd($apiResponse);
72+
6873
return $apiResponse;
6974
}
7075

@@ -79,4 +84,16 @@ protected function request(string $bsn = '', string $doelBinding = '', string $e
7984

8085
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
8186
}
87+
88+
protected function requestEmbedded(string $url, string $doelBinding): array
89+
{
90+
$curlArgs = [
91+
CURLOPT_URL => $url,
92+
CURLOPT_HTTPHEADER => $this->getCurlHeaders($doelBinding),
93+
CURLOPT_SSLCERT => $this->settings->getPublicCertificate(),
94+
CURLOPT_SSLKEY => $this->settings->getPrivateCertificate(),
95+
];
96+
97+
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($url));
98+
}
8299
}

src/PrefillGravityForms/Controllers/VrijBRPController.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public function handle(array $form): array
1515
}
1616

1717
$bsn = $this->getBSN();
18+
$bsn = '900231038';
19+
// $bsn = '311204557';
1820

1921
if ('' === $bsn) {
2022
return $form;
@@ -65,6 +67,11 @@ protected function fetchApiResponse(string $bsn, string $doelBinding = '', strin
6567
return [];
6668
}
6769

70+
foreach (array_filter(explode(',', $expand)) as $expandItem) {
71+
$apiResponse = $this->supplementEmbeddedByLinks($apiResponse, trim($expandItem), $doelBinding);
72+
}
73+
dd($apiResponse);
74+
6875
return $apiResponse;
6976
}
7077

@@ -79,4 +86,17 @@ protected function request(string $bsn = '', string $doelBinding = '', string $e
7986

8087
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
8188
}
89+
90+
protected function requestEmbedded(string $url, string $doelBinding): array
91+
{
92+
return ['overleden' => true];
93+
$curlArgs = [
94+
CURLOPT_URL => $url,
95+
CURLOPT_HTTPHEADER => $this->getCurlHeaders($doelBinding),
96+
CURLOPT_SSLCERT => $this->settings->getPublicCertificate(),
97+
CURLOPT_SSLKEY => $this->settings->getPrivateCertificate(),
98+
];
99+
100+
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($url));
101+
}
82102
}

src/PrefillGravityForms/Controllers/WeAreFrankController.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ protected function request(array $data = [], string $bsn = ''): array
156156
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
157157
}
158158

159+
/**
160+
* This one breaks the contract, fix later.
161+
*/
162+
protected function requestEmbedded(array $data = [], string $bsn = ''): array
163+
{
164+
$curlArgs = [
165+
CURLOPT_URL => $this->settings->getBaseURL(),
166+
CURLOPT_POSTFIELDS => json_encode($data),
167+
CURLOPT_HTTPHEADER => [
168+
'Content-Type: application/json',
169+
'Accept: application/json',
170+
sprintf('%s: %s', $this->settings->getAPITokenUsername(), $this->settings->getAPITokenPassword()),
171+
],
172+
];
173+
174+
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
175+
}
176+
159177
protected function getDefaultCurlArgs(): array
160178
{
161179
return [

0 commit comments

Comments
 (0)