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);
$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) {

View File

@ -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\-_\.!~\*\'\(\)]+)*$/');

View File

@ -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);