Skip to content

Commit

Permalink
feat: include slugs in admission requirements data and allow the use …
Browse files Browse the repository at this point in the history
…of custom admission requirements
  • Loading branch information
zhyian committed Jul 12, 2024
1 parent fc1f347 commit 07756cd
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 28 deletions.
16 changes: 14 additions & 2 deletions src/Application/AdmissionRequirementTranslated.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* name: string,
* link_text: string,
* link_url: string,
* slug: string
* }
* @psalm-type AdmissionRequirementTranslatedType = AdmissionRequirementTranslated
* & array{parent: AdmissionRequirementTranslated|null}
Expand All @@ -20,15 +21,17 @@ final class AdmissionRequirementTranslated
private function __construct(
private Link $current,
private ?AdmissionRequirementTranslated $parent,
private string $slug
) {
}

public static function new(
Link $current,
?AdmissionRequirementTranslated $parent,
string $slug = ''
): self {

return new self($current, $parent);
return new self($current, $parent, $slug);
}

public static function fromAdmissionRequirement(
Expand All @@ -41,6 +44,7 @@ public static function fromAdmissionRequirement(
$admissionRequirement->parent()
? self::fromAdmissionRequirement($admissionRequirement->parent(), $languageCode)
: null,
$admissionRequirement->slug()
);
}

Expand All @@ -57,10 +61,12 @@ public static function fromArray(array $data): self

/** @var AdmissionRequirementTranslatedType|null $parentData */
$parentData = $data[AdmissionRequirement::PARENT];
$slug = $data[AdmissionRequirement::SLUG] ?? '';

return new self(
Link::fromArray($currentData),
$parentData ? self::fromArray($parentData) : null
$parentData ? self::fromArray($parentData) : null,
$slug
);
}

Expand All @@ -73,6 +79,7 @@ public function asArray(): array
$parentData = $this->parent?->asArray();
$currentData = $this->current->asArray();
$currentData[AdmissionRequirement::PARENT] = $parentData;
$currentData[AdmissionRequirement::SLUG] = $this->slug;

return $currentData;
}
Expand All @@ -82,6 +89,11 @@ public function name(): string
return $this->current->name();
}

public function slug(): string
{
return $this->slug;
}

public function linkText(): string
{
return $this->current->linkText();
Expand Down
21 changes: 4 additions & 17 deletions src/Application/Filter/AdmissionRequirementTypeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,18 @@

namespace Fau\DegreeProgram\Common\Application\Filter;

/**
* @psalm-type AdmissionRequirementType = 'frei' | 'eingeschraenkt' | 'frei-mit-einschraenkung'
*/
final class AdmissionRequirementTypeFilter implements Filter
{
public const FREE = 'frei';
public const FREE_WITH_RESTRICTION = 'frei-mit-einschraenkung';
public const RESTRICTED = 'eingeschraenkt';

public const KEY = 'admission-requirement';

/**
* @var array<AdmissionRequirementType>
* @var array<string>
*/
private array $types;

/**
* @psalm-param AdmissionRequirementType $types
*/
private function __construct(
string ...$types
) {
Expand All @@ -35,9 +28,6 @@ public function id(): string
return self::KEY;
}

/**
* @psalm-return array<AdmissionRequirementType>
*/
public function value(): array
{
return array_unique($this->types);
Expand All @@ -56,7 +46,7 @@ public static function empty(): static
}

/**
* @psalm-return ?array<AdmissionRequirementType>
* @return array<string>|null
*/
private static function sanitize(mixed $value): ?array
{
Expand All @@ -68,12 +58,9 @@ private static function sanitize(mixed $value): ?array
return null;
}

/** @psalm-var array<AdmissionRequirementType> $value */
$value = array_filter(
return array_filter(
$value,
static fn ($item) => in_array($item, [self::FREE, self::FREE_WITH_RESTRICTION, self::RESTRICTED], true),
'is_string',
);

return $value;
}
}
34 changes: 31 additions & 3 deletions src/Domain/AdmissionRequirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* name: MultilingualStringType,
* link_text: MultilingualStringType,
* link_url: MultilingualStringType,
* slug: string
* }
*
* @psalm-type AdmissionRequirementType = AdmissionRequirement & array{
Expand All @@ -22,6 +23,7 @@ final class AdmissionRequirement
{
public const ID = 'id';
public const NAME = 'name';
public const SLUG = 'slug';
public const LINK_TEXT = 'link_text';
public const LINK_URL = 'link_url';

Expand All @@ -33,6 +35,7 @@ final class AdmissionRequirement
'required' => [
AdmissionRequirement::ID,
AdmissionRequirement::NAME,
AdmissionRequirement::SLUG,
AdmissionRequirement::LINK_TEXT,
AdmissionRequirement::LINK_URL,
AdmissionRequirement::PARENT,
Expand All @@ -43,6 +46,10 @@ final class AdmissionRequirement
'minLength' => 1,
],
AdmissionRequirement::NAME => MultilingualString::SCHEMA,
AdmissionRequirement::SLUG => [
'type' => 'string',
'minLength' => 1,
],
AdmissionRequirement::LINK_TEXT => MultilingualString::SCHEMA,
AdmissionRequirement::LINK_URL => MultilingualString::SCHEMA,
AdmissionRequirement::PARENT => [
Expand All @@ -51,13 +58,18 @@ final class AdmissionRequirement
'required' => [
AdmissionRequirement::ID,
AdmissionRequirement::NAME,
AdmissionRequirement::SLUG,
],
'properties' => [
AdmissionRequirement::ID => [
'type' => 'string',
'minLength' => 1,
],
AdmissionRequirement::NAME => MultilingualString::SCHEMA,
AdmissionRequirement::SLUG => [
'type' => 'string',
'minLength' => 1,
],
],
],
],
Expand All @@ -69,6 +81,7 @@ final class AdmissionRequirement
'required' => [
AdmissionRequirement::ID,
AdmissionRequirement::NAME,
AdmissionRequirement::SLUG,
AdmissionRequirement::LINK_TEXT,
AdmissionRequirement::LINK_URL,
],
Expand All @@ -78,6 +91,10 @@ final class AdmissionRequirement
'maxLength' => 0,
],
AdmissionRequirement::NAME => MultilingualString::SCHEMA,
AdmissionRequirement::SLUG => [
'type' => 'string',
'maxLength' => 0,
],
AdmissionRequirement::LINK_TEXT => MultilingualString::SCHEMA,
AdmissionRequirement::LINK_URL => MultilingualString::SCHEMA,
AdmissionRequirement::PARENT => [
Expand All @@ -89,22 +106,25 @@ final class AdmissionRequirement
private function __construct(
private MultilingualLink $current,
private ?AdmissionRequirement $parent,
private string $slug
) {
}

public static function new(
MultilingualLink $current,
?AdmissionRequirement $parent,
string $slug = ''
): self {

return new self($current, $parent);
return new self($current, $parent, $slug);
}

public static function empty(): self
{
return new self(
MultilingualLink::empty(),
null
null,
''
);
}

Expand All @@ -122,10 +142,12 @@ public static function fromArray(array $data): self

/** @var AdmissionRequirementType|null $parentData */
$parentData = $data[self::PARENT] ?? null;
$slug = $data[self::SLUG] ?? '';

return new self(
MultilingualLink::fromArray($currentData),
$parentData ? self::fromArray($parentData) : null
$parentData ? self::fromArray($parentData) : null,
$slug
);
}

Expand All @@ -138,6 +160,7 @@ public function asArray(): array
$parentData = $this->parent?->asArray();
$currentData = $this->current->asArray();
$currentData[self::PARENT] = $parentData;
$currentData[self::SLUG] = $this->slug;

return $currentData;
}
Expand All @@ -152,6 +175,11 @@ public function name(): MultilingualString
return $this->current->name();
}

public function slug(): string
{
return $this->slug;
}

public function linkText(): MultilingualString
{
return $this->current->linkText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ private function admissionRequirement(?WP_Term $term): AdmissionRequirement
return AdmissionRequirement::new(
$this->bilingualLinkFromTerm($term),
$parent instanceof WP_Term ? $this->admissionRequirement($parent) : null,
$term->slug
);
}

Expand Down
18 changes: 12 additions & 6 deletions tests/resources/fixtures/degree_program.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,10 @@
"de": "",
"en": ""
},
"parent": null
}
"parent": null,
"slug": "frei"
},
"slug": "admission_bachelor"
},
"teaching_degree_higher_semester": {
"id": "term:24",
Expand Down Expand Up @@ -299,8 +301,10 @@
"de": "",
"en": ""
},
"parent": null
}
"parent": null,
"slug": "frei"
},
"slug": "admission_higher_semester"
},
"master": {
"id": "term:19",
Expand Down Expand Up @@ -336,8 +340,10 @@
"de": "",
"en": ""
},
"parent": null
}
"parent": null,
"slug": "nich_frei"
},
"slug": "admission_master"
}
},
"content_related_master_requirements": {
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/Domain/AdmissionRequirementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function testFromArray(): void
'en' => 'Link URL Bachelor EN',
],
'parent' => null,
'slug' => 'name_bachelor',
],
'teaching_degree_higher_semester' => [
'id' => 'term:6',
Expand Down Expand Up @@ -66,7 +67,9 @@ public function testFromArray(): void
'en' => '',
],
'parent' => null,
'slug' => 'frei',
],
'slug' => 'name_higher_semester',
],
'master' => [
'id' => 'term:7',
Expand All @@ -86,6 +89,7 @@ public function testFromArray(): void
'en' => 'Link URL Master EN',
],
'parent' => null,
'slug' => 'name_master',
],
];

Expand Down

0 comments on commit 07756cd

Please sign in to comment.