Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/PrefillGravityForms/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,26 @@ protected function createExpandArguments(string $expand): string
return urldecode(http_build_query(['expand' => $imploded], '', ','));
}

/**
* Supplements the '_embedded' array with additional data retrieved via the '_links' array,
* using the configured expand arguments to fetch and merge related resources.
*/
protected function supplementEmbeddedByLinks(array $apiResponse, string $embedType = '', string $doelBinding = ''): array
{
if (! isset($apiResponse['_links'][$embedType])) {
return $apiResponse;
}

foreach ($apiResponse['_links'][$embedType] as $key => $embeddedItem) {
$response = $this->requestEmbedded(str_replace('https://api.acc-vrijbrp-hoeksche-waard.commonground.nu/haal-centraal-brp-bevragen/api/v1.3/ingeschrevenpersonen', 'https://api.acc-vrijbrp-hoeksche-waard.commonground.nu/api/haalcentraal-brp-bevragen/api/v1.3/ingeschrevenpersonen', $embeddedItem['href']), $doelBinding);
$apiResponse['_embedded'][$embedType][$key] = array_merge($apiResponse['_embedded'][$embedType][$key], $response);
}

return $apiResponse;
}

abstract protected function requestEmbedded(string $url, string $doelBinding): array;

protected function getCurlHeaders(string $doelBinding = ''): array
{
$headers = [
Expand Down
15 changes: 15 additions & 0 deletions src/PrefillGravityForms/Controllers/EnableUController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ protected function fetchApiResponse(string $bsn, string $doelBinding = '', strin
return [];
}

foreach (array_filter(explode(',', $expand)) as $expandItem) {
$apiResponse = $this->supplementEmbeddedByLinks($apiResponse, trim($expandItem), $doelBinding);
}
dd($apiResponse);

return $apiResponse;
}

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

return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
}

protected function requestEmbedded(string $url, string $doelBinding): array
{
$curlArgs = [
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $this->getCurlHeaders($doelBinding),
];

return $this->handleCurl($curlArgs, CacheService::formatTransientKey($url));
}
}
17 changes: 17 additions & 0 deletions src/PrefillGravityForms/Controllers/PinkRoccadeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ protected function fetchApiResponse(string $bsn, string $doelBinding = '', strin
return [];
}

foreach (array_filter(explode(',', $expand)) as $expandItem) {
$apiResponse = $this->supplementEmbeddedByLinks($apiResponse, trim($expandItem), $doelBinding);
}
dd($apiResponse);

return $apiResponse;
}

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

return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
}

protected function requestEmbedded(string $url, string $doelBinding): array
{
$curlArgs = [
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $this->getCurlHeaders($doelBinding),
CURLOPT_SSLCERT => $this->settings->getPublicCertificate(),
CURLOPT_SSLKEY => $this->settings->getPrivateCertificate(),
];

return $this->handleCurl($curlArgs, CacheService::formatTransientKey($url));
}
}
21 changes: 21 additions & 0 deletions src/PrefillGravityForms/Controllers/VrijBRPController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function handle(array $form): array
}

$bsn = $this->getBSN();
$bsn = '900231038';
// $bsn = '311204557';

if ('' === $bsn) {
return $form;
Expand Down Expand Up @@ -65,6 +67,10 @@ protected function fetchApiResponse(string $bsn, string $doelBinding = '', strin
return [];
}

foreach (array_filter(explode(',', $expand)) as $expandItem) {
$apiResponse = $this->supplementEmbeddedByLinks($apiResponse, trim($expandItem), $doelBinding);
}

return $apiResponse;
}

Expand All @@ -79,4 +85,19 @@ protected function request(string $bsn = '', string $doelBinding = '', string $e

return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
}

protected function requestEmbedded(string $url, string $doelBinding): array
{
// return ['overleden' => true];
$curlArgs = [
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $this->getCurlHeaders($doelBinding),
CURLOPT_SSLCERT => $this->settings->getPublicCertificate(),
CURLOPT_SSLKEY => $this->settings->getPrivateCertificate(),
];

$urlParts = explode('/', $url);

return $this->handleCurl($curlArgs, CacheService::formatTransientKey($urlParts[8]));
}
}
18 changes: 18 additions & 0 deletions src/PrefillGravityForms/Controllers/WeAreFrankController.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ protected function request(array $data = [], string $bsn = ''): array
return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
}

/**
* This one breaks the contract, fix later.
*/
protected function requestEmbedded(array $data = [], string $bsn = ''): array
{
$curlArgs = [
CURLOPT_URL => $this->settings->getBaseURL(),
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
sprintf('%s: %s', $this->settings->getAPITokenUsername(), $this->settings->getAPITokenPassword()),
],
];

return $this->handleCurl($curlArgs, CacheService::formatTransientKey($bsn));
}

protected function getDefaultCurlArgs(): array
{
return [
Expand Down