Skip to content

Commit

Permalink
Make set description optional and nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Jan 12, 2024
1 parent c4f6bc0 commit a2cb531
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
11 changes: 8 additions & 3 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ protected function validateUrl(string $url): string
{
$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);
}
Expand All @@ -73,8 +79,7 @@ protected function validateRegEx(string $string, string $regEx): string
new Assert\Regex([
'pattern' => $regEx,
'message' => 'This value does not match the regular expression "{{ pattern }}".'
]),
new Assert\NotBlank()
])
]
);
if ($violations->count() > 0) {
Expand Down
38 changes: 26 additions & 12 deletions src/Entity/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -83,9 +83,9 @@ public function addRecord(Record $record): void
/**
* 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;
}
Expand Down Expand Up @@ -120,6 +120,16 @@ public function getRecords(): array
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.
*
Expand Down Expand Up @@ -148,19 +158,23 @@ public function removeRecord(Record $record): void
/**
* 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;
}

/**
Expand All @@ -180,11 +194,11 @@ public function setName(?string $name): void
*
* @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\-_\.!~\*\'\(\)]+)*$/');
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/ListSets.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function prepareResponse(ServerRequestInterface $request): void
$setName = $document->createElement('setName', $oaiSet->getName());
$set->appendChild($setName);

if ($oaiSet->getDescription() !== '') {
if ($oaiSet->hasDescription()) {
$setDescription = $document->createElement('setDescription');
$set->appendChild($setDescription);

Expand Down

0 comments on commit a2cb531

Please sign in to comment.