Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FAU-434] Include campo keys from all related terms to degree program data #17

Merged
merged 4 commits into from
Sep 4, 2024
Merged
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
28 changes: 7 additions & 21 deletions src/Domain/CampoKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
namespace Fau\DegreeProgram\Common\Domain;

/**
* @psalm-type CampoKeysMap = array<value-of<self::SUPPORTED_CAMPO_KEYS>, string>
* @psalm-type CampoKeysMap = array<value-of<self::SUPPORTED_CAMPO_KEYS>, array<int, string>>
*/
final class CampoKeys
{
public const SCHEMA = [
'type' => 'object',
'properties' => [
DegreeProgram::DEGREE => [
'type' => 'string',
'type' => 'array',
],
DegreeProgram::AREA_OF_STUDY => [
'type' => 'string',
'type' => 'array',
],
DegreeProgram::LOCATION => [
'type' => 'string',
'type' => 'array',
],
],
];
Expand All @@ -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',
],
],
];
Expand All @@ -45,8 +45,6 @@ final class CampoKeys
DegreeProgram::LOCATION,
];

private const HIS_CODE_DELIMITER = '|';

private function __construct(
/**
* @var CampoKeysMap $map
Expand All @@ -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
*/
Expand Down
32 changes: 20 additions & 12 deletions src/Infrastructure/Repository/CampoKeysRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<WP_Term> $terms */
Expand Down Expand Up @@ -51,7 +53,7 @@ public function degreeProgramCampoKeys(DegreeProgramId $degreeProgramId): CampoK
continue;
}

$map[$campoKeyType] = $campoKey;
$map[$campoKeyType][$term->term_id] = $campoKey;
}

return CampoKeys::fromArray($map);
Expand All @@ -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<string, int>
*/
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;
Expand All @@ -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));
}
}
8 changes: 1 addition & 7 deletions src/Infrastructure/Repository/WpQueryArgsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down