Skip to content

Commit

Permalink
feat: extend MultilingualLink with parent property
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyian committed Dec 24, 2024
1 parent 9a754f8 commit f2b8f3d
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 55 deletions.
23 changes: 20 additions & 3 deletions src/Application/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,37 @@
use Fau\DegreeProgram\Common\Domain\MultilingualLink;

/**
* @psalm-type LinkType = array{
* @psalm-type Link = array{
* name: string,
* link_text: string,
* link_url: string,
* }
* @psalm-type LinkType = Link & array{parent?: Link|null}
*/
final class Link
{
private function __construct(
private string $name,
private string $linkText,
private string $linkUrl,
private ?Link $parent,
) {
}

public static function new(
string $name,
string $linkText,
string $linkUrl,
?Link $parent = null
): self {

return new self($name, $linkText, $linkUrl);
return new self($name, $linkText, $linkUrl, $parent);
}

public static function empty(): self
{

return new self('', '', '');
return new self('', '', '', null);
}

public static function fromMultilingualLink(
Expand All @@ -47,6 +50,7 @@ public static function fromMultilingualLink(
$multilingualLink->name()->asString($languageCode),
$multilingualLink->linkText()->asString($languageCode),
$multilingualLink->linkUrl()->asString($languageCode),
!is_null($multilingualLink->parent()) ? self::fromMultilingualLink($multilingualLink->parent(), $languageCode) : null,
);
}

Expand All @@ -55,10 +59,14 @@ public static function fromMultilingualLink(
*/
public static function fromArray(array $data): self
{
/** @var Link|null $parentData */
$parentData = $data[MultilingualLink::PARENT] ?? null;

return new self(
$data[MultilingualLink::NAME],
$data[MultilingualLink::LINK_TEXT],
$data[MultilingualLink::LINK_URL],
!empty($parentData) ? self::fromArray($parentData) : null,
);
}

Expand All @@ -77,15 +85,24 @@ public function linkUrl(): string
return $this->linkUrl;
}

public function parent(): ?Link
{
return $this->parent;
}

/**
* @return LinkType $data
*/
public function asArray(): array
{
/** @var Link|null $parentData */
$parentData = $this->parent?->asArray();

return [
MultilingualLink::NAME => $this->name,
MultilingualLink::LINK_TEXT => $this->linkText,
MultilingualLink::LINK_URL => $this->linkUrl,
MultilingualLink::PARENT => $parentData,
];
}

Expand Down
38 changes: 35 additions & 3 deletions src/Domain/MultilingualLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@

/**
* @psalm-import-type MultilingualStringType from MultilingualString
* @psalm-type MultilingualLinkType = array{
* @psalm-type MultilingualLink = array{
* id: string,
* name: MultilingualStringType,
* link_text: MultilingualStringType,
* link_url: MultilingualStringType
* }
* @psalm-type MultilingualLinkType = MultilingualLink & array{parent?: MultilingualLink|null}
*/
final class MultilingualLink
{
public const ID = 'id';
public const NAME = 'name';
public const LINK_TEXT = 'link_text';
public const LINK_URL = 'link_url';
public const PARENT = 'parent';

public const SCHEMA = [
'type' => 'object',
'additionalProperties' => false,
'additionalProperties' => true,
'required' => [
MultilingualLink::ID,
MultilingualLink::NAME,
Expand All @@ -47,6 +49,7 @@ final class MultilingualLink
MultilingualLink::NAME,
MultilingualLink::LINK_TEXT,
MultilingualLink::LINK_URL,
MultilingualLink::PARENT,
],
'properties' => [
MultilingualLink::ID => [
Expand All @@ -56,6 +59,19 @@ final class MultilingualLink
MultilingualLink::NAME => MultilingualString::SCHEMA,
MultilingualLink::LINK_TEXT => MultilingualString::SCHEMA,
MultilingualLink::LINK_URL => MultilingualString::SCHEMA,
MultilingualLink::PARENT => [
'type' => ['object', 'null'],
'additionalProperties' => true,
'required' => [
MultilingualLink::ID,
],
'properties' => [
MultilingualLink::ID => [
'type' => 'string',
'minLength' => 1,
],
],
],
],
];

Expand All @@ -64,6 +80,7 @@ private function __construct(
private MultilingualString $name,
private MultilingualString $linkText,
private MultilingualString $linkUrl,
private ?MultilingualLink $parent,
) {
}

Expand All @@ -72,9 +89,10 @@ public static function new(
MultilingualString $name,
MultilingualString $linkText,
MultilingualString $linkUrl,
?MultilingualLink $parent = null
): self {

return new self($id, $name, $linkText, $linkUrl);
return new self($id, $name, $linkText, $linkUrl, $parent);
}

public static function empty(): self
Expand All @@ -84,6 +102,7 @@ public static function empty(): self
MultilingualString::empty(),
MultilingualString::empty(),
MultilingualString::empty(),
null
);
}

Expand All @@ -92,11 +111,15 @@ public static function empty(): self
*/
public static function fromArray(array $data): self
{
/** @var MultilingualLink|null $parentData */
$parentData = $data[self::PARENT] ?? null;

return new self(
$data[self::ID],
MultilingualString::fromArray($data[self::NAME]),
MultilingualString::fromArray($data[self::LINK_TEXT]),
MultilingualString::fromArray($data[self::LINK_URL]),
!empty($parentData) ? self::fromArray($parentData) : null,
);
}

Expand All @@ -105,11 +128,15 @@ public static function fromArray(array $data): self
*/
public function asArray(): array
{
/** @var MultilingualLink|null $parentData */
$parentData = $this->parent?->asArray();

return [
self::ID => $this->id,
self::NAME => $this->name->asArray(),
self::LINK_TEXT => $this->linkText->asArray(),
self::LINK_URL => $this->linkUrl->asArray(),
self::PARENT => $parentData,
];
}

Expand All @@ -132,4 +159,9 @@ public function linkUrl(): MultilingualString
{
return $this->linkUrl;
}

public function parent(): ?MultilingualLink
{
return $this->parent;
}
}
7 changes: 5 additions & 2 deletions src/Infrastructure/Repository/BilingualRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,16 @@ final protected function bilingualOption(string $key): MultilingualString
);
}

final protected function bilingualLinkFromTerm(?WP_Term $term): MultilingualLink
final protected function bilingualLinkFromTerm(?WP_Term $term, string $taxonomy): MultilingualLink
{
$parentTerm = $term instanceof WP_Term && $term->parent ? get_term($term->parent, $taxonomy) : null;

return MultilingualLink::new(
$term instanceof WP_Term ? $this->idGenerator->generateTermId($term) : '',
name: $this->bilingualTermName($term),
linkText: $this->bilingualTermMeta($term, MultilingualLink::LINK_TEXT),
linkUrl: $this->bilingualTermMeta($term, MultilingualLink::LINK_URL),
parent: $parentTerm instanceof WP_Term ? $this->bilingualLinkFromTerm($parentTerm, $taxonomy) : null,
);
}

Expand All @@ -144,7 +147,7 @@ final protected function bilingualTermLinks(WP_Post $post, string $taxonomy): Mu

$links = [];
foreach ($terms as $term) {
$links[] = $this->bilingualLinkFromTerm($term);
$links[] = $this->bilingualLinkFromTerm($term, $taxonomy);
}

return MultilingualLinks::new(...$links);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,23 @@ public function getById(DegreeProgramId $degreeProgramId): DegreeProgram
bachelorOrTeachingDegree: $this->admissionRequirement(
$this->firstTerm(
$post,
BachelorOrTeachingDegreeAdmissionRequirementTaxonomy::KEY
)
BachelorOrTeachingDegreeAdmissionRequirementTaxonomy::KEY,
),
BachelorOrTeachingDegreeAdmissionRequirementTaxonomy::KEY,
),
teachingDegreeHigherSemester: $this->admissionRequirement(
$this->firstTerm(
$post,
TeachingDegreeHigherSemesterAdmissionRequirementTaxonomy::KEY
)
TeachingDegreeHigherSemesterAdmissionRequirementTaxonomy::KEY,
),
TeachingDegreeHigherSemesterAdmissionRequirementTaxonomy::KEY,
),
master: $this->admissionRequirement(
$this->firstTerm(
$post,
MasterDegreeAdmissionRequirementTaxonomy::KEY,
)
),
MasterDegreeAdmissionRequirementTaxonomy::KEY,
),
),
contentRelatedMasterRequirements: $this->bilingualPostMeta(
Expand All @@ -182,15 +185,17 @@ public function getById(DegreeProgramId $degreeProgramId): DegreeProgram
true
),
germanLanguageSkillsForInternationalStudents: $this->bilingualLinkFromTerm(
$this->firstTopTerm(
$this->firstTerm(
$post,
GermanLanguageSkillsForInternationalStudentsTaxonomy::KEY,
)
),
GermanLanguageSkillsForInternationalStudentsTaxonomy::KEY,
),
startOfSemester: $this->bilingualLinkFromOption(DegreeProgram::START_OF_SEMESTER),
semesterDates: $this->bilingualLinkFromOption(DegreeProgram::SEMESTER_DATES),
examinationsOffice: $this->bilingualLinkFromTerm(
$this->firstTerm($post, ExaminationsOfficeTaxonomy::KEY)
$this->firstTerm($post, ExaminationsOfficeTaxonomy::KEY),
ExaminationsOfficeTaxonomy::KEY,
),
examinationRegulations: (string) get_post_meta(
$postId,
Expand All @@ -206,7 +211,8 @@ public function getById(DegreeProgramId $degreeProgramId): DegreeProgram
department: $this->bilingualPostMeta($post, DegreeProgram::DEPARTMENT),
studentAdvice: $this->bilingualLinkFromOption(DegreeProgram::STUDENT_ADVICE),
subjectSpecificAdvice: $this->bilingualLinkFromTerm(
$this->firstTerm($post, SubjectSpecificAdviceTaxonomy::KEY)
$this->firstTerm($post, SubjectSpecificAdviceTaxonomy::KEY),
SubjectSpecificAdviceTaxonomy::KEY,
),
serviceCenters: $this->bilingualLinkFromOption(DegreeProgram::SERVICE_CENTERS),
infoBrochure: (string) get_post_meta(
Expand Down Expand Up @@ -235,6 +241,7 @@ public function getById(DegreeProgramId $degreeProgramId): DegreeProgram
),
applyNowLink: $this->bilingualLinkFromTerm(
$this->firstTerm($post, ApplyNowLinkTaxonomy::KEY),
ApplyNowLinkTaxonomy::KEY,
),
combinations: $this->idsFromPostMeta($postId, DegreeProgram::COMBINATIONS),
limitedCombinations: $this->idsFromPostMeta(
Expand Down Expand Up @@ -273,27 +280,6 @@ private function firstTerm(WP_Post $post, string $taxonomy): ?WP_Term
return $terms[0];
}

private function firstTopTerm(WP_Post $post, string $taxonomy): ?WP_Term
{
$terms = get_the_terms($post, $taxonomy);

if (!is_array($terms)) {
return null;
}

if (!isset($terms[0]) || !$terms[0] instanceof WP_Term) {
return null;
}

if (!$terms[0]->parent) {
return $terms[0];
}

$parent = get_term($terms[0]->parent);

return $parent instanceof WP_Term ? $parent : $terms[0];
}

private function degree(WP_Post $post): Degree
{
$term = $this->firstTerm($post, DegreeTaxonomy::KEY);
Expand All @@ -317,7 +303,7 @@ private function degreeFromTerm(WP_Term $term): Degree
);
}

private function admissionRequirement(?WP_Term $term): AdmissionRequirement
private function admissionRequirement(?WP_Term $term, string $taxonomy): AdmissionRequirement
{
if (!$term instanceof WP_Term) {
return AdmissionRequirement::empty();
Expand All @@ -326,8 +312,8 @@ private function admissionRequirement(?WP_Term $term): AdmissionRequirement
$parent = $term->parent ? get_term($term->parent) : null;

return AdmissionRequirement::new(
$this->bilingualLinkFromTerm($term),
$parent instanceof WP_Term ? $this->admissionRequirement($parent) : null,
$this->bilingualLinkFromTerm($term, $taxonomy),
$parent instanceof WP_Term ? $this->admissionRequirement($parent, $taxonomy) : null,
$term->slug
);
}
Expand Down
Loading

0 comments on commit f2b8f3d

Please sign in to comment.