Make set description optional and nullable

This commit is contained in:
Sebastian Meyer 2024-01-12 07:05:27 +01:00
parent c4f6bc09ac
commit a2cb531bbc
3 changed files with 35 additions and 16 deletions

View File

@ -47,7 +47,13 @@ abstract class Entity
{ {
$url = trim($url); $url = trim($url);
$validator = Validation::createValidator(); $validator = Validation::createValidator();
$violations = $validator->validate($url, new Assert\Url()); $violations = $validator->validate(
$url,
[
new Assert\Url(),
new Assert\NotBlank()
]
);
if ($violations->count() > 0) { if ($violations->count() > 0) {
throw new ValidationFailedException(null, $violations); throw new ValidationFailedException(null, $violations);
} }
@ -73,8 +79,7 @@ abstract class Entity
new Assert\Regex([ new Assert\Regex([
'pattern' => $regEx, 'pattern' => $regEx,
'message' => 'This value does not match the regular expression "{{ pattern }}".' 'message' => 'This value does not match the regular expression "{{ pattern }}".'
]), ])
new Assert\NotBlank()
] ]
); );
if ($violations->count() > 0) { if ($violations->count() > 0) {

View File

@ -54,8 +54,8 @@ class Set extends Entity
/** /**
* A description of the set. * A description of the set.
*/ */
#[ORM\Column(type: 'text')] #[ORM\Column(type: 'text', nullable: true)]
private string $description = ''; private ?string $description = null;
/** /**
* Collection of associated records. * Collection of associated records.
@ -83,9 +83,9 @@ class Set extends Entity
/** /**
* Get the description of this set. * 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; return $this->description;
} }
@ -120,6 +120,16 @@ class Set extends Entity
return $this->records->toArray(); 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. * Whether this set contains any records.
* *
@ -148,19 +158,23 @@ class Set extends Entity
/** /**
* Set the description for this set. * Set the description for this set.
* *
* @param string $description The description * @param ?string $description The description
* *
* @return void * @return void
* *
* @throws ValidationFailedException * @throws ValidationFailedException
*/ */
public function setDescription(string $description): void public function setDescription(?string $description): void
{ {
try { if (isset($description)) {
$this->description = $this->validateXml($description); $description = trim($description);
} catch (ValidationFailedException $exception) { try {
throw $exception; $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 $spec The set spec
* @param ?string $name The name of the set (defaults to 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 * @throws ValidationFailedException
*/ */
public function __construct(string $spec, ?string $name = null, string $description = '') public function __construct(string $spec, ?string $name = null, string $description = null)
{ {
try { try {
$this->spec = $this->validateRegEx($spec, '/^([A-Za-z0-9\-_\.!~\*\'\(\)])+(:[A-Za-z0-9\-_\.!~\*\'\(\)]+)*$/'); $this->spec = $this->validateRegEx($spec, '/^([A-Za-z0-9\-_\.!~\*\'\(\)])+(:[A-Za-z0-9\-_\.!~\*\'\(\)]+)*$/');

View File

@ -90,7 +90,7 @@ class ListSets extends Middleware
$setName = $document->createElement('setName', $oaiSet->getName()); $setName = $document->createElement('setName', $oaiSet->getName());
$set->appendChild($setName); $set->appendChild($setName);
if ($oaiSet->getDescription() !== '') { if ($oaiSet->hasDescription()) {
$setDescription = $document->createElement('setDescription'); $setDescription = $document->createElement('setDescription');
$set->appendChild($setDescription); $set->appendChild($setDescription);