From a2cb531bbc45bdfedf5f52c71cf445ecaed319ad Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Fri, 12 Jan 2024 07:05:27 +0100 Subject: [PATCH] Make set description optional and nullable --- src/Entity.php | 11 ++++++++--- src/Entity/Set.php | 38 +++++++++++++++++++++++++------------ src/Middleware/ListSets.php | 2 +- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/Entity.php b/src/Entity.php index 1b5d69a..5ffea2e 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -47,7 +47,13 @@ abstract class Entity { $url = trim($url); $validator = Validation::createValidator(); - $violations = $validator->validate($url, new Assert\Url()); + $violations = $validator->validate( + $url, + [ + new Assert\Url(), + new Assert\NotBlank() + ] + ); if ($violations->count() > 0) { throw new ValidationFailedException(null, $violations); } @@ -73,8 +79,7 @@ abstract class Entity new Assert\Regex([ 'pattern' => $regEx, 'message' => 'This value does not match the regular expression "{{ pattern }}".' - ]), - new Assert\NotBlank() + ]) ] ); if ($violations->count() > 0) { diff --git a/src/Entity/Set.php b/src/Entity/Set.php index e79457c..9355035 100644 --- a/src/Entity/Set.php +++ b/src/Entity/Set.php @@ -54,8 +54,8 @@ class Set extends Entity /** * A description of the set. */ - #[ORM\Column(type: 'text')] - private string $description = ''; + #[ORM\Column(type: 'text', nullable: true)] + private ?string $description = null; /** * Collection of associated records. @@ -83,9 +83,9 @@ class Set extends Entity /** * Get the description of this set. * - * @return string The set description + * @return ?string The set description or NULL */ - public function getDescription(): string + public function getDescription(): ?string { return $this->description; } @@ -120,6 +120,16 @@ class Set extends Entity return $this->records->toArray(); } + /** + * Whether this set has a description. + * + * @return bool TRUE if description exists, FALSE otherwise + */ + public function hasDescription(): bool + { + return isset($this->description); + } + /** * Whether this set contains any records. * @@ -148,19 +158,23 @@ class Set extends Entity /** * Set the description for this set. * - * @param string $description The description + * @param ?string $description The description * * @return void * * @throws ValidationFailedException */ - public function setDescription(string $description): void + public function setDescription(?string $description): void { - try { - $this->description = $this->validateXml($description); - } catch (ValidationFailedException $exception) { - throw $exception; + if (isset($description)) { + $description = trim($description); + try { + $description = $this->validateXml($description); + } catch (ValidationFailedException $exception) { + throw $exception; + } } + $this->description = $description; } /** @@ -180,11 +194,11 @@ class Set extends Entity * * @param string $spec The set spec * @param ?string $name The name of the set (defaults to spec) - * @param string $description The description of the set + * @param ?string $description The description of the set * * @throws ValidationFailedException */ - public function __construct(string $spec, ?string $name = null, string $description = '') + public function __construct(string $spec, ?string $name = null, string $description = null) { try { $this->spec = $this->validateRegEx($spec, '/^([A-Za-z0-9\-_\.!~\*\'\(\)])+(:[A-Za-z0-9\-_\.!~\*\'\(\)]+)*$/'); diff --git a/src/Middleware/ListSets.php b/src/Middleware/ListSets.php index 60a1ceb..b9fc022 100644 --- a/src/Middleware/ListSets.php +++ b/src/Middleware/ListSets.php @@ -90,7 +90,7 @@ class ListSets extends Middleware $setName = $document->createElement('setName', $oaiSet->getName()); $set->appendChild($setName); - if ($oaiSet->getDescription() !== '') { + if ($oaiSet->hasDescription()) { $setDescription = $document->createElement('setDescription'); $set->appendChild($setDescription);