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-414] Add support for campo keys #7

Merged
merged 9 commits into from
May 24, 2024
2 changes: 2 additions & 0 deletions config/schema_draft.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fau\DegreeProgram\Common\Domain\AdmissionRequirement;
use Fau\DegreeProgram\Common\Domain\AdmissionRequirements;
use Fau\DegreeProgram\Common\Domain\CampoKeys;
use Fau\DegreeProgram\Common\Domain\Content;
use Fau\DegreeProgram\Common\Domain\ContentItem;
use Fau\DegreeProgram\Common\Domain\Degree;
Expand Down Expand Up @@ -150,5 +151,6 @@
DegreeProgram::STUDENT_INITIATIVES => MultilingualLink::SCHEMA,
DegreeProgram::APPLY_NOW_LINK => MultilingualLink::SCHEMA,
DegreeProgram::ENTRY_TEXT => MultilingualString::SCHEMA,
DegreeProgram::CAMPO_KEYS => CampoKeys::SCHEMA,
],
];
2 changes: 2 additions & 0 deletions config/schema_publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fau\DegreeProgram\Common\Domain\AdmissionRequirement;
use Fau\DegreeProgram\Common\Domain\AdmissionRequirements;
use Fau\DegreeProgram\Common\Domain\CampoKeys;
use Fau\DegreeProgram\Common\Domain\Content;
use Fau\DegreeProgram\Common\Domain\ContentItem;
use Fau\DegreeProgram\Common\Domain\Degree;
Expand Down Expand Up @@ -155,5 +156,6 @@
DegreeProgram::STUDENT_INITIATIVES => MultilingualLink::SCHEMA,
DegreeProgram::APPLY_NOW_LINK => MultilingualLink::SCHEMA_REQUIRED,
DegreeProgram::ENTRY_TEXT => MultilingualString::SCHEMA_REQUIRED,
DegreeProgram::CAMPO_KEYS => CampoKeys::SCHEMA_REQUIRED,
],
];
10 changes: 10 additions & 0 deletions src/Application/DegreeProgramViewRaw.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Fau\DegreeProgram\Common\Application;

use Fau\DegreeProgram\Common\Domain\AdmissionRequirements;
use Fau\DegreeProgram\Common\Domain\CampoKeys;
use Fau\DegreeProgram\Common\Domain\Content;
use Fau\DegreeProgram\Common\Domain\Degree;
use Fau\DegreeProgram\Common\Domain\DegreeProgram;
Expand Down Expand Up @@ -75,6 +76,7 @@ private function __construct(
private MultilingualLink $studentInitiatives,
private MultilingualLink $applyNowLink,
private MultilingualString $entryText,
private CampoKeys $campoKeys,
) {
}

Expand Down Expand Up @@ -134,6 +136,7 @@ public static function fromDegreeProgram(DegreeProgram $degreeProgram): self
$data[DegreeProgram::STUDENT_INITIATIVES],
$data[DegreeProgram::APPLY_NOW_LINK],
$data[DegreeProgram::ENTRY_TEXT],
$data[DegreeProgram::CAMPO_KEYS],
);
}

Expand Down Expand Up @@ -198,6 +201,7 @@ public static function fromArray(array $data): self
),
applyNowLink: MultilingualLink::fromArray($data[DegreeProgram::APPLY_NOW_LINK]),
entryText: MultilingualString::fromArray($data[DegreeProgram::ENTRY_TEXT]),
campoKeys: CampoKeys::fromArray($data[DegreeProgram::CAMPO_KEYS]),
);
}

Expand Down Expand Up @@ -261,6 +265,7 @@ public function asArray(): array
DegreeProgram::STUDENT_INITIATIVES => $this->studentInitiatives->asArray(),
DegreeProgram::APPLY_NOW_LINK => $this->applyNowLink->asArray(),
DegreeProgram::ENTRY_TEXT => $this->entryText->asArray(),
DegreeProgram::CAMPO_KEYS => $this->campoKeys->asArray(),
];
}

Expand Down Expand Up @@ -513,4 +518,9 @@ public function entryText(): MultilingualString
{
return $this->entryText;
}

public function campoKeys(): CampoKeys
{
return $this->campoKeys;
}
}
12 changes: 12 additions & 0 deletions src/Application/DegreeProgramViewTranslated.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Fau\DegreeProgram\Common\Application;

use Fau\DegreeProgram\Common\Domain\CampoKeys;
use Fau\DegreeProgram\Common\Domain\DegreeProgram;
use Fau\DegreeProgram\Common\Domain\DegreeProgramId;
use Fau\DegreeProgram\Common\Domain\MultilingualString;
Expand All @@ -21,6 +22,7 @@
* @psalm-import-type LanguageCodes from MultilingualString
* @psalm-import-type ImageViewType from ImageView
* @psalm-import-type NumberOfStudentsType from NumberOfStudents
* @psalm-import-type CampoKeysMap from CampoKeys
* @psalm-type DegreeProgramTranslation = array{
* link: string,
* slug: string,
Expand Down Expand Up @@ -73,6 +75,7 @@
* student_initiatives: LinkType,
* apply_now_link: LinkType,
* entry_text: string,
* campo_keys: CampoKeysMap
* }
* @psalm-type DegreeProgramViewTranslatedArrayType = DegreeProgramTranslation & array{
* id: int,
Expand Down Expand Up @@ -145,6 +148,7 @@ public function __construct(
private Link $studentInitiatives,
private Link $applyNowLink,
private string $entryText,
private CampoKeys $campoKeys,
) {
}

Expand Down Expand Up @@ -209,6 +213,7 @@ public static function empty(int $id, string $languageCode): self
studentInitiatives: Link::empty(),
applyNowLink: Link::empty(),
entryText: '',
campoKeys: CampoKeys::empty(),
);
}

Expand Down Expand Up @@ -277,6 +282,7 @@ public static function fromArray(array $data): self
studentInitiatives: Link::fromArray($data[DegreeProgram::STUDENT_INITIATIVES]),
applyNowLink: Link::fromArray($data[DegreeProgram::APPLY_NOW_LINK]),
entryText: $data[DegreeProgram::ENTRY_TEXT],
campoKeys: CampoKeys::fromArray($data[DegreeProgram::CAMPO_KEYS]),
);

if (empty($data[self::TRANSLATIONS])) {
Expand Down Expand Up @@ -350,6 +356,7 @@ public function asArray(): array
DegreeProgram::STUDENT_INITIATIVES => $this->studentInitiatives->asArray(),
DegreeProgram::APPLY_NOW_LINK => $this->applyNowLink->asArray(),
DegreeProgram::ENTRY_TEXT => $this->entryText,
DegreeProgram::CAMPO_KEYS => $this->campoKeys->asArray(),
self::TRANSLATIONS => $this->translationsAsArray(),
];
}
Expand Down Expand Up @@ -669,4 +676,9 @@ public function entryText(): string
{
return $this->entryText;
}

public function campoKeys(): CampoKeys
{
return $this->campoKeys;
}
}
25 changes: 25 additions & 0 deletions src/Application/Repository/CollectionCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* include?: array<int>,
* search?: string,
* order_by: OrderBy,
* his_codes?: array<string>
* }
*/
final class CollectionCriteria
Expand All @@ -36,6 +37,11 @@ final class CollectionCriteria
*/
private array $filters = [];

/**
* @var array<string>
*/
private array $hisCodes = [];

/**
* @var LanguageCodes|null
*/
Expand Down Expand Up @@ -170,6 +176,17 @@ public function withOrderBy(array $orderBy): self
return $instance;
}

/**
* @param array<string> $hisCodes
* @return self
*/
public function withHisCodes(array $hisCodes): self
{
$instance = clone $this;
$instance->hisCodes = $hisCodes;
return $instance;
}

/**
* @psalm-return SupportedArgs
*/
Expand All @@ -185,4 +202,12 @@ public function filters(): array
{
return $this->filters;
}

/**
* @return array<string>
*/
public function hisCodes(): array
{
return $this->hisCodes;
}
}
90 changes: 90 additions & 0 deletions src/Domain/CampoKeys.php
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like Campo Keys, which is part of our domain model, as it should be.
What do you think if we remove unrequired getters and setters. I could imagine something like

Index: src/Infrastructure/Repository/CampoKeysRepository.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/Infrastructure/Repository/CampoKeysRepository.php b/src/Infrastructure/Repository/CampoKeysRepository.php
--- a/src/Infrastructure/Repository/CampoKeysRepository.php	(revision 4a72cbaf0318d8804397f2d94b6884a695bbd7af)
+++ b/src/Infrastructure/Repository/CampoKeysRepository.php	(date 1716362430281)
@@ -25,8 +25,6 @@
 
     public function degreeProgramCampoKeys(DegreeProgramId $degreeProgramId): CampoKeys
     {
-        $campoKeys = CampoKeys::empty();
-
         /** @var WP_Error|array<WP_Term> $terms */
         $terms = wp_get_post_terms(
             $degreeProgramId->asInt(),
@@ -34,8 +32,10 @@
         );
 
         if ($terms instanceof WP_Error) {
-            return $campoKeys;
+            return CampoKeys::empty();
         }
+
+        $map = [];
 
         foreach ($terms as $term) {
             $campoKey = (string) get_term_meta($term->term_id, self::CAMPOKEY_TERM_META_KEY, true);
@@ -50,10 +50,10 @@
                 continue;
             }
 
-            $campoKeys->set($campoKeyType, $campoKey);
+            $map[$campoKeyType] = $campoKey;
         }
 
-        return $campoKeys;
+        return CampoKeys::fromArray($map);
     }
 
     /**
Index: src/Domain/CampoKeys.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/Domain/CampoKeys.php b/src/Domain/CampoKeys.php
--- a/src/Domain/CampoKeys.php	(revision 4a72cbaf0318d8804397f2d94b6884a695bbd7af)
+++ b/src/Domain/CampoKeys.php	(date 1716362430285)
@@ -4,8 +4,9 @@
 
 namespace Fau\DegreeProgram\Common\Domain;
 
-use InvalidArgumentException;
-
+/**
+ * @psalm-type CampoKeysMap = array<value-of<self::SUPPORTED_CAMPO_KEYS>, string>
+ */
 final class CampoKeys
 {
     public const SCHEMA = [
@@ -46,85 +47,50 @@
 
     private const HIS_CODE_DELIMITER = '|';
 
-    /** @var array<string, string> */
-    private array $map = [];
-
-    private function __construct()
-    {
+    private function __construct(
+        /**
+         * @var CampoKeysMap $map
+         */
+        private array $map
+    ) {
     }
 
     public static function empty(): self
     {
-        return new self();
+        return new self([]);
     }
 
     /**
-     * @psalm-param array<string, string> $data
+     * @param CampoKeysMap $map
      */
-    public static function fromArray(array $data): self
+    public static function fromArray(array $map): self
     {
-        $instance = new self();
-
-        foreach ($data as $key => $value) {
-            $instance->set($key, $value);
-        }
-
-        return $instance;
+        return new self($map);
     }
 
     public static function fromHisCode(string $hisCode): self
     {
         $parts = explode(self::HIS_CODE_DELIMITER, $hisCode);
+        $map = [];
 
-        $instance = new self();
 
         if (isset($parts[0])) {
-            $instance->set(DegreeProgram::DEGREE, $parts[0]);
+            $map[DegreeProgram::DEGREE] = $parts[0];
         }
 
         if (isset($parts[1])) {
-            $instance->set(DegreeProgram::AREA_OF_STUDY, $parts[1]);
+            $map[DegreeProgram::AREA_OF_STUDY] = $parts[1];
         }
 
         if (isset($parts[6])) {
-            $instance->set(DegreeProgram::LOCATION, $parts[6]);
+            $map[DegreeProgram::LOCATION] = $parts[6];
         }
-
-        return $instance;
-    }
-
-    public function set(string $key, string $value): self
-    {
-        if (! in_array($key, self::SUPPORTED_CAMPO_KEYS, true)) {
-            throw new InvalidArgumentException('Unsupported field key.');
-        }
-
-        $this->map[$key] = $value;
-        return $this;
-    }
-
-    public function degree(): ?string
-    {
-        return $this->get(DegreeProgram::DEGREE);
-    }
-
-    public function areaOfStudy(): ?string
-    {
-        return $this->get(DegreeProgram::AREA_OF_STUDY);
-    }
 
-    public function studyLocation(): ?string
-    {
-        return $this->get(DegreeProgram::LOCATION);
-    }
-
-    public function get(string $key): ?string
-    {
-        return $this->map[$key] ?? null;
+        return new self($map);
     }
 
     /**
-     * @return array<string, string>
+     * @return CampoKeysMap
      */
     public function asArray(): array
     {

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace Fau\DegreeProgram\Common\Domain;

/**
* @psalm-type CampoKeysMap = array<value-of<self::SUPPORTED_CAMPO_KEYS>, string>
*/
final class CampoKeys
{
public const SCHEMA = [
'type' => 'object',
'properties' => [
DegreeProgram::DEGREE => [
'type' => 'string',
],
DegreeProgram::AREA_OF_STUDY => [
'type' => 'string',
],
DegreeProgram::LOCATION => [
'type' => 'string',
],
],
];

public const SCHEMA_REQUIRED = [
'type' => 'object',
'properties' => [
DegreeProgram::DEGREE => [
'type' => 'string',
],
DegreeProgram::AREA_OF_STUDY => [
'type' => 'string',
],
DegreeProgram::LOCATION => [
'type' => 'string',
],
],
];
shvlv marked this conversation as resolved.
Show resolved Hide resolved

public const SUPPORTED_CAMPO_KEYS = [
DegreeProgram::DEGREE,
DegreeProgram::AREA_OF_STUDY,
DegreeProgram::LOCATION,
];

private const HIS_CODE_DELIMITER = '|';

private function __construct(
/**
* @var CampoKeysMap $map
*/
private array $map
) {
}

public static function empty(): self
{
return new self([]);
}

/**
* @param CampoKeysMap $map
*/
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
*/
public function asArray(): array
{
return $this->map;
}
}
10 changes: 10 additions & 0 deletions src/Domain/DegreeProgram.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* @psalm-import-type AdmissionRequirementsType from AdmissionRequirements
* @psalm-import-type DegreeType from Degree
* @psalm-import-type NumberOfStudentsType from NumberOfStudents
* @psalm-import-type CampoKeysMap from CampoKeys
* @psalm-type DegreeProgramArrayType = array{
* id: int,
* slug: MultilingualStringType,
Expand Down Expand Up @@ -67,6 +68,7 @@
* student_initiatives: MultilingualLinkType,
* apply_now_link: MultilingualLinkType,
* entry_text: MultilingualStringType,
* campo_keys: CampoKeysMap,
* }
*/
final class DegreeProgram
Expand Down Expand Up @@ -122,6 +124,7 @@ final class DegreeProgram
public const NOTES_FOR_INTERNATIONAL_APPLICANTS = 'notes_for_international_applicants';
public const STUDENT_INITIATIVES = 'student_initiatives';
public const APPLY_NOW_LINK = 'apply_now_link';
public const CAMPO_KEYS = 'campo_keys';

private IntegersListChangeset $combinationsChangeset;
private IntegersListChangeset $limitedCombinationsChangeset;
Expand Down Expand Up @@ -315,6 +318,10 @@ public function __construct(
* Eingeschränkt Kombinationsmöglichkeiten
*/
private DegreeProgramIds $limitedCombinations,
/**
* CampoKeys
*/
private CampoKeys $campoKeys,
) {

$this->combinationsChangeset = IntegersListChangeset::new(
Expand Down Expand Up @@ -451,6 +458,7 @@ private function update(array $data): void
$this->studentInitiatives = MultilingualLink::fromArray($data[self::STUDENT_INITIATIVES]);
$this->applyNowLink = MultilingualLink::fromArray($data[self::APPLY_NOW_LINK]);
$this->entryText = MultilingualString::fromArray($data[self::ENTRY_TEXT]);
$this->campoKeys = CampoKeys::fromArray($data[self::CAMPO_KEYS]);

$this->combinationsChangeset = $this
->combinationsChangeset
Expand Down Expand Up @@ -515,6 +523,7 @@ private function update(array $data): void
* student_initiatives: MultilingualLink,
* apply_now_link: MultilingualLink,
* entry_text: MultilingualString,
* campo_keys: CampoKeys,
* }
* @internal Only for repositories usage
* phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
Expand Down Expand Up @@ -574,6 +583,7 @@ public function asArray(): array
self::STUDENT_INITIATIVES => $this->studentInitiatives,
self::APPLY_NOW_LINK => $this->applyNowLink,
self::ENTRY_TEXT => $this->entryText,
self::CAMPO_KEYS => $this->campoKeys,
];
}

Expand Down
Loading