From 421ed96a96555218c552a413d1ec552989f794e5 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Fri, 15 May 2026 16:44:32 +0300 Subject: [PATCH 1/6] OS-244 datafordeler address lookup --- CHANGELOG.md | 2 + src/LookupResult/AddressLookupResult.php | 361 ++++++++++++++++++ .../DataLookup/DatafordelerAddressLookup.php | 232 +++++++++++ .../DatafordelerAddressLookupInterface.php | 41 ++ .../DatafordelerMartikulaLookupInterface.php | 36 ++ .../DatafordelerMatrikulaLookup.php | 176 +++++++++ 6 files changed, 848 insertions(+) create mode 100644 src/LookupResult/AddressLookupResult.php create mode 100644 src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php create mode 100644 src/Plugin/os2web/DataLookup/DatafordelerAddressLookupInterface.php create mode 100644 src/Plugin/os2web/DataLookup/DatafordelerMartikulaLookupInterface.php create mode 100644 src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php diff --git a/CHANGELOG.md b/CHANGELOG.md index ffbab9d..ee9853f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +* Adding Datafordeler address lookup + ## [3.1.0] - 2026-04-27 * [PR-29](https://github.com/OS2web/os2web_datalookup/pull/29) diff --git a/src/LookupResult/AddressLookupResult.php b/src/LookupResult/AddressLookupResult.php new file mode 100644 index 0000000..29343be --- /dev/null +++ b/src/LookupResult/AddressLookupResult.php @@ -0,0 +1,361 @@ +successful; + } + + /** + * Set state of successfulness. + * + * @param bool $successful + * The state. + */ + public function setSuccessful(bool $successful = TRUE): void { + $this->successful = $successful; + } + + /** + * Get an error message. + * + * @return string + * The message. + */ + public function getErrorMessage(): string { + return $this->errorMessage; + } + + /** + * Set error message. + * + * @param string $errorMessage + * The message. + */ + public function setErrorMessage(string $errorMessage): void { + $this->errorMessage = $errorMessage; + } + + /** + * Get ID. + * + * @return string + * The ID number. + */ + public function getId(): string { + return $this->id; + } + + /** + * Set ID. + * + * @param string $id + * ID number. + */ + public function setId(string $id): void { + $this->id = $id; + } + + /** + * Get access ID. + * + * @return string + * Access id number. + */ + public function getAccessAddressId(): string { + return $this->accessAddressId; + } + + /** + * Sets access ID. + * + * @param string $accessAddressId + * Access id number. + */ + public function setAccessAddressId(string $accessAddressId): void { + $this->accessAddressId = $accessAddressId; + } + + /** + * Get full address. + * + * @return string + * The full address. + */ + public function getFullAddress(): string { + return $this->fullAddress; + } + + /** + * Set full address. + * + * @param string $fullAddress + * The full address. + */ + public function setFullAddress(string $fullAddress): void { + $this->fullAddress = $fullAddress; + } + + /** + * Get street. + * + * @return string + * The street. + */ + public function getStreet(): string { + return $this->street; + } + + /** + * Set street. + * + * @param string $street + * The street. + */ + public function setStreet(string $street): void { + $this->street = $street; + } + + /** + * Get house number. + * + * @return string + * The number. + */ + public function getHouseNr(): string { + return $this->houseNr; + } + + /** + * Set house number. + * + * @param string $houseNr + * The number. + */ + public function setHouseNr(string $houseNr): void { + $this->houseNr = $houseNr; + } + + /** + * Get floor. + * + * @return string + * The floor. + */ + public function getFloor(): string { + return $this->floor; + } + + /** + * Set floor. + * + * @param string $floor + * The floor. + */ + public function setFloor(string $floor): void { + $this->floor = $floor; + } + + /** + * Get apartment number. + * + * @return string + * The number. + */ + public function getApartmentNr(): string { + return $this->apartmentNr; + } + + /** + * Set apartment number. + * + * @param string $apartmentNr + * The number. + */ + public function setApartmentNr(string $apartmentNr): void { + $this->apartmentNr = $apartmentNr; + } + + /** + * Get postal code. + * + * @return string + * The code. + */ + public function getPostalCode(): string { + return $this->postalCode; + } + + /** + * Set postal code. + * + * @param string $postalCode + * The code. + */ + public function setPostalCode(string $postalCode): void { + $this->postalCode = $postalCode; + } + + /** + * Get city name. + * + * @return string + * The city name. + */ + public function getCity(): string { + return $this->city; + } + + /** + * Set city. + * + * @param string $city + * The city name. + */ + public function setCity(string $city): void { + $this->city = $city; + } + + /** + * Get municipality code. + * + * @return string + * The code. + */ + public function getMunicipalityCode(): string { + return $this->municipalityCode; + } + + /** + * Set municipality code. + * + * @param string $municipalityCode + * The municipality code. + */ + public function setMunicipalityCode(string $municipalityCode): void { + $this->municipalityCode = $municipalityCode; + } + + /** + * Set supplementary locality / village name. + * + * @param string $supplementaryCity + * Supplementary city name. + */ + public function setSupplementaryCity(string $supplementaryCity): void { + $this->supplementaryCity = $supplementaryCity; + } + + /** + * Get supplementary locality / village name. + * + * @return string + * Supplementary city name. + */ + public function getSupplementaryCity(): string { + return $this->supplementaryCity; + } + +} diff --git a/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php b/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php new file mode 100644 index 0000000..a725d9e --- /dev/null +++ b/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php @@ -0,0 +1,232 @@ +get('os2web_audit.logger'); + /** @var \Drupal\key\KeyRepositoryInterface $keyRepository */ + $keyRepository = $container->get('key.repository'); + /** @var \Drupal\Core\File\FileSystem $fileSystem */ + $fileSystem = $container->get('file_system'); + + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('http_client'), + $auditLogger, + $keyRepository, + $fileSystem, + ); + } + + /** + * {@inheritdoc} + */ + public function getAddressMatches(ParameterBag $params, $fetchColumn = 'titel') : array { + $token = $this->getConfiguration()['token']; + $url = "https://adressevaelger.dk/adresser/soeg"; + + $json = ''; + + // Get autocomplete query. + $q = $params->get('q') ?: ''; + // Adding limit by municipality limit, if present. + $limitByMunicipality = $params->get('limit_by_municipality') ?: ''; + + $query =[ + 'token' => $token, + 'tekst' => $q + ]; + + if (!empty($limitByMunicipality)) { + $query['kommunekode'] = $limitByMunicipality; + } + + try { + $json = $this->httpClient->request('GET', $url, [ + 'query' => $query, + ])->getBody(); + } catch (GuzzleException $e) { + \Drupal::logger('os2web_datalookup')->warning('Request failed: @e', ['@e' => $e->getMessage()]); + } + + $jsonDecoded = json_decode($json, TRUE); + $addresses = []; + if (is_array($jsonDecoded) && !empty($jsonDecoded['fund'])) { + $addresses = $jsonDecoded['fund']; + } + + if ($fetchColumn) { + // Checking if remove_place_name is enabled. + $removePlaceName = $params->get('remove_place_name') ?: ''; + if ($removePlaceName) { + foreach ($addresses as &$entry) { + $addressId = $entry['id']; + + $address = $this->fetchAddressLookupResult($addressId); + if ($supplerendebynavn = $address->getSupplementaryCity()) { + $entry['titel'] = preg_replace("/$supplerendebynavn,/", '', $entry['titel']); + } + } + } + + $matches = array_column($addresses, $fetchColumn); + } + else { + $matches = $addresses; + } + + return $matches; + } + + /** + * {@inheritdoc} + */ + public function getSingleAddress(ParameterBag $params) : AddressLookupResult { + $address = NULL; + + // Getting address_id. + $matches = $this->getAddressMatches($params, NULL); + if (!empty($matches)) { + $addressSource = $matches[0]; + + // Fetching address. + $address = $this->fetchAddressLookupResult($addressSource['id']); + } + + return $address; + } + + /** + * Returns single address from address API. + * + * @param string $id + * ID of the address. + * + * @return \Drupal\os2web_datalookup\LookupResult\AddressLookupResult + * The found address. + */ + public function fetchAddressLookupResult($id) { + $token = $this->getConfiguration()['token']; + $url = "https://adressevaelger.dk/adresser/$id"; + + $address = new AddressLookupResult(); + $address->setSuccessful(FALSE); + + $json = ''; + + // Fetching address. + try { + $json = $this->httpClient->request('GET', $url, [ + 'query' => [ + 'token' => $token, + ], + ])->getBody(); + } catch (GuzzleException $e) { + \Drupal::logger('os2web_datalookup')->warning('Request failed: @e', ['@e' => $e->getMessage()]); + } + + $jsonDecoded = json_decode($json, TRUE); + if (is_array($jsonDecoded) && !empty($jsonDecoded)) { + if ($jsonDecoded['status'] == 'ok') { + $address->setSuccessful(); + + $address_raw = $jsonDecoded['adresse']; + $address->setId($address_raw['id_lokalid']); + $address->setAccessAddressId($address_raw['husnummer']['adgangspunkt']['id_lokalid']); + $address->setFullAddress($address_raw['adressebetegnelse']); + + $address->setStreet($address_raw['husnummer']['vejnavn'] ?? ''); + $address->setHouseNr($address_raw['husnummer']['husnummertekst'] ?? ''); + $address->setFloor($address_raw['etagebetegnelse'] ?? ''); + $address->setApartmentNr($address_raw['doerbetegnelse'] ?? ''); + $address->setPostalCode($address_raw['husnummer']['postnummer']['postnr'] ?? ''); + $address->setCity($address_raw['husnummer']['postnummer']['navn'] ?? ''); + $address->setMunicipalityCode($address_raw['husnummer']['navngivenvejkommunedel']['kommune'] ?? ''); + $address->setSupplementaryCity($address_raw['husnummer']['supplerendebynavn']['navn'] ?? ''); + } + } + + return $address; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration(): array { + return [ + 'username' => '', + 'password' => '', + ] + parent::defaultConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form['token'] = [ + '#type' => 'textfield', + '#title' => $this->t('Token for service calls'), + '#default_value' => $this->configuration['token'] ?? '', + '#required' => TRUE, + '#description' => $this->t('Token required for performing API requests'), + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $configuration = $this->getConfiguration(); + $configuration['token'] = $form_state->getValue('token'); + $this->setConfiguration($configuration); + } + +} diff --git a/src/Plugin/os2web/DataLookup/DatafordelerAddressLookupInterface.php b/src/Plugin/os2web/DataLookup/DatafordelerAddressLookupInterface.php new file mode 100644 index 0000000..6c8c5cb --- /dev/null +++ b/src/Plugin/os2web/DataLookup/DatafordelerAddressLookupInterface.php @@ -0,0 +1,41 @@ +get('os2web_audit.logger'); + /** @var \Drupal\key\KeyRepositoryInterface $keyRepository */ + $keyRepository = $container->get('key.repository'); + /** @var \Drupal\Core\File\FileSystem $fileSystem */ + $fileSystem = $container->get('file_system'); + + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('http_client'), + $auditLogger, + $keyRepository, + $fileSystem, + ); + } + + /** + * {@inheritdoc} + */ + public function getMatrikulaId(string $addressAccessId) : ?string { + $url = "https://services.datafordeler.dk/DAR/DAR/3.0.0/rest/husnummerTilJordstykke"; + + $json = ''; + + try { + $json = $this->httpClient->request('GET', $url, [ + 'query' => [ + 'husnummerid' => $addressAccessId, + ], + ])->getBody(); + } catch (GuzzleException $e) { + \Drupal::logger('os2web_datalookup')->warning('Request failed: @e', ['@e' => $e->getMessage()]); + } + + $jsonDecoded = json_decode($json, TRUE); + if (is_array($jsonDecoded)) { + if (NestedArray::keyExists($jsonDecoded, ['gældendeJordstykke', 'jordstykkeLokalId'])) { + return NestedArray::getValue($jsonDecoded, ['gældendeJordstykke', 'jordstykkeLokalId']); + } + } + + return NULL; + } + + /** + * {@inheritdoc} + */ + public function getMatrikulaEntries(string $matrikulaId) : array { + $matrikulaEntries = []; + $url = "https://services.datafordeler.dk/Matriklen2/Matrikel/2.0.0/rest/SamletFastEjendom"; + + $configuration = $this->getConfiguration(); + + $json = ''; + + try { + $json = $this->httpClient->request('GET', $url, [ + 'query' => [ + 'jordstykkeid' => $matrikulaId, + 'username' => $configuration['username'], + 'password' => $configuration['password'], + ], + ])->getBody(); + } + catch (GuzzleException $e) { + \Drupal::logger('os2web_datalookup')->warning('Request failed: @e', ['@e' => $e->getMessage()]); + } + + $jsonDecoded = json_decode($json, TRUE); + + if (is_array($jsonDecoded)) { + if (NestedArray::keyExists($jsonDecoded, ['features', 0, 'properties', 'jordstykke'])) { + $jordstykker = NestedArray::getValue($jsonDecoded, ['features', 0, 'properties', 'jordstykke']); + foreach ($jordstykker as $jordstyk) { + try { + $matrikulaEntries[] = new DatafordelerMatrikula($jordstyk); + } + catch (\TypeError $e) { + // Could not create matrikula object. + } + } + } + } + + return $matrikulaEntries; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration(): array { + return [ + 'username' => '', + 'password' => '', + ] + parent::defaultConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form['username'] = [ + '#type' => 'textfield', + '#title' => $this->t('Username for service calls'), + '#default_value' => $this->configuration['username'], + '#required' => TRUE, + '#description' => $this->t('Username required for performing API requests'), + ]; + $form['password'] = [ + '#type' => 'textfield', + '#title' => $this->t('Password for service calls'), + '#default_value' => $this->configuration['password'], + '#required' => TRUE, + '#description' => $this->t('Password required for performing API requests'), + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $configuration = $this->getConfiguration(); + $configuration['username'] = $form_state->getValue('username'); + $configuration['password'] = $form_state->getValue('password'); + $this->setConfiguration($configuration); + } + +} From 1e55ba2134833c4951d2f437d13cbfa35ebb41a3 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Fri, 15 May 2026 16:46:54 +0300 Subject: [PATCH 2/6] Updating changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee9853f..7400263 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -* Adding Datafordeler address lookup +* [PR-32](https://github.com/OS2web/os2web_datalookup/pull/32) + Adding Datafordeler address lookup ## [3.1.0] - 2026-04-27 From 20a9a91d4b91a094c5774c5f712af8716f1aade0 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Fri, 15 May 2026 16:56:25 +0300 Subject: [PATCH 3/6] PHPCS fixes --- src/LookupResult/AddressLookupResult.php | 2 +- .../DataLookup/DatafordelerAddressLookup.php | 16 +++++++--------- .../DatafordelerAddressLookupInterface.php | 6 +++--- .../DataLookup/DatafordelerMatrikulaLookup.php | 3 ++- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/LookupResult/AddressLookupResult.php b/src/LookupResult/AddressLookupResult.php index 29343be..48fbcba 100644 --- a/src/LookupResult/AddressLookupResult.php +++ b/src/LookupResult/AddressLookupResult.php @@ -352,7 +352,7 @@ public function setSupplementaryCity(string $supplementaryCity): void { * Get supplementary locality / village name. * * @return string - * Supplementary city name. + * Supplementary city name. */ public function getSupplementaryCity(): string { return $this->supplementaryCity; diff --git a/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php b/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php index a725d9e..df12c8c 100644 --- a/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php +++ b/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php @@ -2,16 +2,12 @@ namespace Drupal\os2web_datalookup\Plugin\os2web\DataLookup; -use Drupal\Component\Utility\NestedArray; use Drupal\Core\File\FileSystem; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\key\KeyRepositoryInterface; -use Drupal\os2forms_dawa\Entity\DatafordelerMatrikula; -use Drupal\os2forms_dawa\Entity\DawaAddress; use Drupal\os2web_audit\Service\Logger; use Drupal\os2web_datalookup\LookupResult\AddressLookupResult; -use Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupBase; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -78,9 +74,9 @@ public function getAddressMatches(ParameterBag $params, $fetchColumn = 'titel') // Adding limit by municipality limit, if present. $limitByMunicipality = $params->get('limit_by_municipality') ?: ''; - $query =[ + $query = [ 'token' => $token, - 'tekst' => $q + 'tekst' => $q, ]; if (!empty($limitByMunicipality)) { @@ -91,7 +87,8 @@ public function getAddressMatches(ParameterBag $params, $fetchColumn = 'titel') $json = $this->httpClient->request('GET', $url, [ 'query' => $query, ])->getBody(); - } catch (GuzzleException $e) { + } + catch (GuzzleException $e) { \Drupal::logger('os2web_datalookup')->warning('Request failed: @e', ['@e' => $e->getMessage()]); } @@ -127,7 +124,7 @@ public function getAddressMatches(ParameterBag $params, $fetchColumn = 'titel') /** * {@inheritdoc} */ - public function getSingleAddress(ParameterBag $params) : AddressLookupResult { + public function getSingleAddress(ParameterBag $params) : AddressLookupResult { $address = NULL; // Getting address_id. @@ -167,7 +164,8 @@ public function fetchAddressLookupResult($id) { 'token' => $token, ], ])->getBody(); - } catch (GuzzleException $e) { + } + catch (GuzzleException $e) { \Drupal::logger('os2web_datalookup')->warning('Request failed: @e', ['@e' => $e->getMessage()]); } diff --git a/src/Plugin/os2web/DataLookup/DatafordelerAddressLookupInterface.php b/src/Plugin/os2web/DataLookup/DatafordelerAddressLookupInterface.php index 6c8c5cb..c474f04 100644 --- a/src/Plugin/os2web/DataLookup/DatafordelerAddressLookupInterface.php +++ b/src/Plugin/os2web/DataLookup/DatafordelerAddressLookupInterface.php @@ -15,7 +15,7 @@ interface DatafordelerAddressLookupInterface extends DataLookupInterface { /** - * Returns array of matches for 'os2forms_dawa_address' element autocomplete route. + * Returns array of matches for 'os2forms_dawa_address' element autocomplete. * * @param \Symfony\Component\HttpFoundation\ParameterBag $params * The query params. @@ -27,15 +27,15 @@ interface DatafordelerAddressLookupInterface extends DataLookupInterface { */ public function getAddressMatches(ParameterBag $params, $fetchColumn = 'titel') : array; - /** * Returns single address from address API. * * @param \Symfony\Component\HttpFoundation\ParameterBag $params * The query params. * - * @return \Drupal\os2web_datalookup\LookupResult\AddressLookupResult|NULL + * @return \Drupal\os2web_datalookup\LookupResult\AddressLookupResult|null * The found address. */ public function getSingleAddress(ParameterBag $params) : ?AddressLookupResult; + } diff --git a/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php b/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php index 7075acf..7e19686 100644 --- a/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php +++ b/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php @@ -74,7 +74,8 @@ public function getMatrikulaId(string $addressAccessId) : ?string { 'husnummerid' => $addressAccessId, ], ])->getBody(); - } catch (GuzzleException $e) { + } + catch (GuzzleException $e) { \Drupal::logger('os2web_datalookup')->warning('Request failed: @e', ['@e' => $e->getMessage()]); } From edb4a558273b5088700e694adf551442cd83e875 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Mon, 25 May 2026 12:11:57 +0300 Subject: [PATCH 4/6] OS-244 Replacing accessId with houseId --- src/LookupResult/AddressLookupResult.php | 18 +++++++++--------- .../DataLookup/DatafordelerAddressLookup.php | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/LookupResult/AddressLookupResult.php b/src/LookupResult/AddressLookupResult.php index 48fbcba..9420d8b 100644 --- a/src/LookupResult/AddressLookupResult.php +++ b/src/LookupResult/AddressLookupResult.php @@ -29,11 +29,11 @@ class AddressLookupResult { protected string $id; /** - * Address access ID. + * Address house ID. * * @var string */ - protected $accessAddressId; + protected $houseId; /** * Name of the person. @@ -159,23 +159,23 @@ public function setId(string $id): void { } /** - * Get access ID. + * Get house ID. * * @return string * Access id number. */ - public function getAccessAddressId(): string { - return $this->accessAddressId; + public function getHouseId(): string { + return $this->houseId; } /** - * Sets access ID. + * Sets house ID. * - * @param string $accessAddressId + * @param string $houseId * Access id number. */ - public function setAccessAddressId(string $accessAddressId): void { - $this->accessAddressId = $accessAddressId; + public function setHouseId(string $houseId): void { + $this->houseId = $houseId; } /** diff --git a/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php b/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php index df12c8c..3676cdd 100644 --- a/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php +++ b/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php @@ -176,7 +176,7 @@ public function fetchAddressLookupResult($id) { $address_raw = $jsonDecoded['adresse']; $address->setId($address_raw['id_lokalid']); - $address->setAccessAddressId($address_raw['husnummer']['adgangspunkt']['id_lokalid']); + $address->setHouseId($address_raw['husnummer']['id_lokalid']); $address->setFullAddress($address_raw['adressebetegnelse']); $address->setStreet($address_raw['husnummer']['vejnavn'] ?? ''); From bb7616b03f405f4f1487a200f6f7f3e052121ae9 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Mon, 25 May 2026 17:16:02 +0300 Subject: [PATCH 5/6] OS-244 refactoring --- src/LookupResult/AddressLookupResult.php | 6 +- src/LookupResult/MatrikulaLookupResult.php | 95 +++++++++++++++++++ .../DataLookup/DatafordelerAddressLookup.php | 15 ++- .../DatafordelerMatrikulaLookup.php | 40 +++++--- ... DatafordelerMatrikulaLookupInterface.php} | 6 +- 5 files changed, 134 insertions(+), 28 deletions(-) create mode 100644 src/LookupResult/MatrikulaLookupResult.php rename src/Plugin/os2web/DataLookup/{DatafordelerMartikulaLookupInterface.php => DatafordelerMatrikulaLookupInterface.php} (81%) diff --git a/src/LookupResult/AddressLookupResult.php b/src/LookupResult/AddressLookupResult.php index 9420d8b..22ec355 100644 --- a/src/LookupResult/AddressLookupResult.php +++ b/src/LookupResult/AddressLookupResult.php @@ -3,7 +3,7 @@ namespace Drupal\os2web_datalookup\LookupResult; /** - * Representation or value object for the result of a CPR lookup. + * Representation or value object for the result of an address lookup. */ class AddressLookupResult { @@ -36,14 +36,14 @@ class AddressLookupResult { protected $houseId; /** - * Name of the person. + * Full address. * * @var string */ protected string $fullAddress; /** - * Street of the person. + * Street of the address. * * @var string */ diff --git a/src/LookupResult/MatrikulaLookupResult.php b/src/LookupResult/MatrikulaLookupResult.php new file mode 100644 index 0000000..80b91d9 --- /dev/null +++ b/src/LookupResult/MatrikulaLookupResult.php @@ -0,0 +1,95 @@ +ownerLicenseCode = $ownerLicenseCode; + } + + /** + * Returns owner license code. + * + * @return string + * Owners license code. + */ + public function getOwnerLicenseCode(): string { + return $this->ownerLicenseCode; + } + + /** + * Sets ownership name. + * + * @param string $ownershipName + * Ownership name. + */ + public function setOwnershipName(string $ownershipName): void { + $this->ownershipName = $ownershipName; + } + + /** + * Returns ownership name. + * + * @return string + * Ownership name. + */ + public function getOwnershipName(): string { + return $this->ownershipName; + } + + /** + * Sets matrikula number. + * + * @param string $matrikulaNumber + * Matrikula number. + */ + public function setMatrikulaNumber(string $matrikulaNumber): void { + $this->matrikulaNumber = $matrikulaNumber; + } + + /** + * Returns matrikula number. + * + * @return string + * Matrikula number. + */ + public function getMatrikulaNumber(): string { + return $this->matrikulaNumber; + } + +} diff --git a/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php b/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php index 3676cdd..af222af 100644 --- a/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php +++ b/src/Plugin/os2web/DataLookup/DatafordelerAddressLookup.php @@ -18,7 +18,7 @@ * * @DataLookup( * id = "datafordeler_address_lookup", - * label = @Translation("Datafordeler Address Lookup"), + * label = @Translation("Datafordeler Address Lookup") * ) */ class DatafordelerAddressLookup extends DataLookupBase implements DatafordelerAddressLookupInterface, ContainerFactoryPluginInterface { @@ -67,8 +67,6 @@ public function getAddressMatches(ParameterBag $params, $fetchColumn = 'titel') $token = $this->getConfiguration()['token']; $url = "https://adressevaelger.dk/adresser/soeg"; - $json = ''; - // Get autocomplete query. $q = $params->get('q') ?: ''; // Adding limit by municipality limit, if present. @@ -90,6 +88,7 @@ public function getAddressMatches(ParameterBag $params, $fetchColumn = 'titel') } catch (GuzzleException $e) { \Drupal::logger('os2web_datalookup')->warning('Request failed: @e', ['@e' => $e->getMessage()]); + return []; } $jsonDecoded = json_decode($json, TRUE); @@ -124,7 +123,7 @@ public function getAddressMatches(ParameterBag $params, $fetchColumn = 'titel') /** * {@inheritdoc} */ - public function getSingleAddress(ParameterBag $params) : AddressLookupResult { + public function getSingleAddress(ParameterBag $params) : ?AddressLookupResult { $address = NULL; // Getting address_id. @@ -155,8 +154,6 @@ public function fetchAddressLookupResult($id) { $address = new AddressLookupResult(); $address->setSuccessful(FALSE); - $json = ''; - // Fetching address. try { $json = $this->httpClient->request('GET', $url, [ @@ -167,11 +164,12 @@ public function fetchAddressLookupResult($id) { } catch (GuzzleException $e) { \Drupal::logger('os2web_datalookup')->warning('Request failed: @e', ['@e' => $e->getMessage()]); + return $address; } $jsonDecoded = json_decode($json, TRUE); if (is_array($jsonDecoded) && !empty($jsonDecoded)) { - if ($jsonDecoded['status'] == 'ok') { + if ($jsonDecoded['status'] === 'ok') { $address->setSuccessful(); $address_raw = $jsonDecoded['adresse']; @@ -198,8 +196,7 @@ public function fetchAddressLookupResult($id) { */ public function defaultConfiguration(): array { return [ - 'username' => '', - 'password' => '', + 'token' => '', ] + parent::defaultConfiguration(); } diff --git a/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php b/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php index 7e19686..9a9253b 100644 --- a/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php +++ b/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php @@ -7,10 +7,11 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\key\KeyRepositoryInterface; -use Drupal\os2forms_dawa\Entity\DatafordelerMatrikula; use Drupal\os2web_audit\Service\Logger; +use Drupal\os2web_datalookup\LookupResult\MatrikulaLookupResult; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; +use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -21,7 +22,14 @@ * label = @Translation("Datafordeler Matrikula Lookup"), * ) */ -class DatafordelerMatrikulaLookup extends DataLookupBase implements DatafordelerMartikulaLookupInterface, ContainerFactoryPluginInterface { +class DatafordelerMatrikulaLookup extends DataLookupBase implements DatafordelerMatrikulaLookupInterface, ContainerFactoryPluginInterface { + + /** + * Logger channel. + * + * @var LoggerInterface + */ + protected LoggerInterface $logger; /** * {@inheritdoc} @@ -34,8 +42,10 @@ public function __construct( Logger $auditLogger, KeyRepositoryInterface $keyRepository, FileSystem $fileSystem, + LoggerInterface $logger, ) { parent::__construct($configuration, $plugin_id, $plugin_definition, $auditLogger, $keyRepository, $fileSystem); + $this->logger = $logger; } /** @@ -49,6 +59,7 @@ public static function create(ContainerInterface $container, array $configuratio /** @var \Drupal\Core\File\FileSystem $fileSystem */ $fileSystem = $container->get('file_system'); + return new static( $configuration, $plugin_id, @@ -57,6 +68,7 @@ public static function create(ContainerInterface $container, array $configuratio $auditLogger, $keyRepository, $fileSystem, + $container->get('logger.factory')->get('os2web_datalookup') ); } @@ -66,8 +78,6 @@ public static function create(ContainerInterface $container, array $configuratio public function getMatrikulaId(string $addressAccessId) : ?string { $url = "https://services.datafordeler.dk/DAR/DAR/3.0.0/rest/husnummerTilJordstykke"; - $json = ''; - try { $json = $this->httpClient->request('GET', $url, [ 'query' => [ @@ -76,7 +86,9 @@ public function getMatrikulaId(string $addressAccessId) : ?string { ])->getBody(); } catch (GuzzleException $e) { - \Drupal::logger('os2web_datalookup')->warning('Request failed: @e', ['@e' => $e->getMessage()]); + $this->logger->warning('Request failed: @e', ['@e' => $e->getMessage()]); + + return NULL; } $jsonDecoded = json_decode($json, TRUE); @@ -98,8 +110,6 @@ public function getMatrikulaEntries(string $matrikulaId) : array { $configuration = $this->getConfiguration(); - $json = ''; - try { $json = $this->httpClient->request('GET', $url, [ 'query' => [ @@ -110,7 +120,9 @@ public function getMatrikulaEntries(string $matrikulaId) : array { ])->getBody(); } catch (GuzzleException $e) { - \Drupal::logger('os2web_datalookup')->warning('Request failed: @e', ['@e' => $e->getMessage()]); + $this->logger->warning('Request failed: @e', ['@e' => $e->getMessage()]); + + return $matrikulaEntries; } $jsonDecoded = json_decode($json, TRUE); @@ -119,11 +131,13 @@ public function getMatrikulaEntries(string $matrikulaId) : array { if (NestedArray::keyExists($jsonDecoded, ['features', 0, 'properties', 'jordstykke'])) { $jordstykker = NestedArray::getValue($jsonDecoded, ['features', 0, 'properties', 'jordstykke']); foreach ($jordstykker as $jordstyk) { - try { - $matrikulaEntries[] = new DatafordelerMatrikula($jordstyk); - } - catch (\TypeError $e) { - // Could not create matrikula object. + if (isset($jordstyk['properties']) && !empty($jordstyk['properties'])) { + $matrikula = new MatrikulaLookupResult(); + $matrikula->setOwnerLicenseCode($jordstyk['properties']['ejerlavskode']); + $matrikula->setOwnershipName($jordstyk['properties']['ejerlavsnavn']); + $matrikula->setMatrikulaNumber($jordstyk['properties']['matrikelnummer']); + + $matrikulaEntries[] = $matrikula; } } } diff --git a/src/Plugin/os2web/DataLookup/DatafordelerMartikulaLookupInterface.php b/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookupInterface.php similarity index 81% rename from src/Plugin/os2web/DataLookup/DatafordelerMartikulaLookupInterface.php rename to src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookupInterface.php index 5bc1225..4595062 100644 --- a/src/Plugin/os2web/DataLookup/DatafordelerMartikulaLookupInterface.php +++ b/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookupInterface.php @@ -9,16 +9,16 @@ * * @ingroup plugin_api */ -interface DatafordelerMartikulaLookupInterface extends DataLookupInterface { +interface DatafordelerMatrikulaLookupInterface extends DataLookupInterface { /** - * Returns list of ID for Matrikula / jordstykke related with this address. + * Returns of ID for Matrikula / jordstykke related with this address. * * @param string $addressAccessId * Address to make search against. * * @return string|null - * List if IDs. + * IDs of matrikula. */ public function getMatrikulaId(string $addressAccessId) : ?string; From 0213a348fbc69cfc55061ca2eb526f77cf29c6a9 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Mon, 25 May 2026 17:18:12 +0300 Subject: [PATCH 6/6] OS-244 refactoring --- src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php b/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php index 9a9253b..dc2a908 100644 --- a/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php +++ b/src/Plugin/os2web/DataLookup/DatafordelerMatrikulaLookup.php @@ -27,7 +27,7 @@ class DatafordelerMatrikulaLookup extends DataLookupBase implements Datafordeler /** * Logger channel. * - * @var LoggerInterface + * @var \Psr\Log\LoggerInterface */ protected LoggerInterface $logger; @@ -59,7 +59,6 @@ public static function create(ContainerInterface $container, array $configuratio /** @var \Drupal\Core\File\FileSystem $fileSystem */ $fileSystem = $container->get('file_system'); - return new static( $configuration, $plugin_id,