diff --git a/src/Domain/CampoKeys.php b/src/Domain/CampoKeys.php index 7788684..48128bd 100644 --- a/src/Domain/CampoKeys.php +++ b/src/Domain/CampoKeys.php @@ -5,7 +5,7 @@ namespace Fau\DegreeProgram\Common\Domain; /** - * @psalm-type CampoKeysMap = array, string> + * @psalm-type CampoKeysMap = array, array> */ final class CampoKeys { @@ -13,13 +13,13 @@ final class CampoKeys 'type' => 'object', 'properties' => [ DegreeProgram::DEGREE => [ - 'type' => 'string', + 'type' => 'array', ], DegreeProgram::AREA_OF_STUDY => [ - 'type' => 'string', + 'type' => 'array', ], DegreeProgram::LOCATION => [ - 'type' => 'string', + 'type' => 'array', ], ], ]; @@ -28,13 +28,13 @@ final class CampoKeys 'type' => 'object', 'properties' => [ DegreeProgram::DEGREE => [ - 'type' => 'string', + 'type' => 'array', ], DegreeProgram::AREA_OF_STUDY => [ - 'type' => 'string', + 'type' => 'array', ], DegreeProgram::LOCATION => [ - 'type' => 'string', + 'type' => 'array', ], ], ]; @@ -45,8 +45,6 @@ final class CampoKeys DegreeProgram::LOCATION, ]; - private const HIS_CODE_DELIMITER = '|'; - private function __construct( /** * @var CampoKeysMap $map @@ -68,18 +66,6 @@ public static function fromArray(array $map): self return new self($map); } - public static function fromHisCode(string $hisCode): self - { - $parts = explode(self::HIS_CODE_DELIMITER, $hisCode); - $map = [ - DegreeProgram::DEGREE => $parts[0] ?? null, - DegreeProgram::AREA_OF_STUDY => $parts[1] ?? null, - DegreeProgram::LOCATION => $parts[6] ?? null, - ]; - - return new self(array_filter($map, fn($value) => !is_null($value))); - } - /** * @return CampoKeysMap */ diff --git a/src/Infrastructure/Repository/CampoKeysRepository.php b/src/Infrastructure/Repository/CampoKeysRepository.php index 3031a45..3cd6f3e 100644 --- a/src/Infrastructure/Repository/CampoKeysRepository.php +++ b/src/Infrastructure/Repository/CampoKeysRepository.php @@ -24,6 +24,8 @@ final class CampoKeysRepository public const CAMPO_KEY_TERM_META_KEY = 'uniquename'; + private const HIS_CODE_DELIMITER = '|'; + public function degreeProgramCampoKeys(DegreeProgramId $degreeProgramId): CampoKeys { /** @var WP_Error|array $terms */ @@ -51,7 +53,7 @@ public function degreeProgramCampoKeys(DegreeProgramId $degreeProgramId): CampoK continue; } - $map[$campoKeyType] = $campoKey; + $map[$campoKeyType][$term->term_id] = $campoKey; } return CampoKeys::fromArray($map); @@ -60,29 +62,23 @@ public function degreeProgramCampoKeys(DegreeProgramId $degreeProgramId): CampoK /** * Return a map of taxonomy keys to terms based on a given HIS code. * - * @throws RuntimeException * @return array */ - public function taxonomyToTermsMapFromCampoKeys(CampoKeys $campoKeys): array + public function taxonomyToTermsMapFromHisCode(string $hisCode): array { $result = []; - $campoKeys = $campoKeys->asArray(); + $campoKeys = $this->campoKeysFromHisCode($hisCode); foreach (self::TAXONOMY_TO_CAMPO_KEY_MAP as $taxonomy => $campoKeyType) { - $campoKey = $campoKeys[$campoKeyType] ?? ''; + $campoKey = $campoKeys[$campoKeyType] ?? null; - if ($campoKey === '') { + if (!is_string($campoKey) || $campoKey === '') { continue; } $term = $this->findTermByCampoKey($taxonomy, $campoKey); - - if (! $term instanceof WP_Term) { - throw new RuntimeException('Could not find term for Campo key: ' . $campoKey); - } - - $result[$taxonomy] = $term->term_id; + $result[$taxonomy] = $term instanceof WP_Term ? $term->term_id : 0; } return $result; @@ -107,4 +103,16 @@ private function findTermByCampoKey(string $taxonomy, string $campoKey): ?WP_Ter return $terms[0] ?? null; } + + public function campoKeysFromHisCode(string $hisCode): array + { + $parts = explode(self::HIS_CODE_DELIMITER, $hisCode); + $map = [ + DegreeProgram::DEGREE => $parts[0] ?? null, + DegreeProgram::AREA_OF_STUDY => $parts[1] ?? null, + DegreeProgram::LOCATION => $parts[6] ?? null, + ]; + + return array_filter($map, fn($value) => !is_null($value)); + } } diff --git a/src/Infrastructure/Repository/WpQueryArgsBuilder.php b/src/Infrastructure/Repository/WpQueryArgsBuilder.php index 4fe1d87..8f3f41d 100644 --- a/src/Infrastructure/Repository/WpQueryArgsBuilder.php +++ b/src/Infrastructure/Repository/WpQueryArgsBuilder.php @@ -107,13 +107,7 @@ public function applyHisCodes(array $hisCodes, WpQueryArgs $queryArgs): WpQueryA 'relation' => 'AND', ]; - try { - $taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromCampoKeys( - CampoKeys::fromHisCode($hisCode) - ); - } catch (RuntimeException) { - continue; - } + $taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromHisCode($hisCode); if (count($taxonomyToTermMapping) === 0) { continue;