From a1e905ba9381ca4d69a4a242ce4dac40fad01567 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Thu, 4 Jan 2024 05:20:55 +0100 Subject: [PATCH] Update ORM entity associations --- src/Database/Format.php | 73 +++++++++++++++++++++++++++++----- src/Database/Record.php | 88 +++++++++++++++++++++++------------------ src/Database/Set.php | 10 ++--- 3 files changed, 119 insertions(+), 52 deletions(-) diff --git a/src/Database/Format.php b/src/Database/Format.php index 41a806c..7343258 100644 --- a/src/Database/Format.php +++ b/src/Database/Format.php @@ -63,7 +63,7 @@ class Format * * @var Collection */ - #[ORM\OneToMany(targetEntity: Record::class, mappedBy: 'format')] + #[ORM\OneToMany(targetEntity: Record::class, mappedBy: 'format', fetch: 'EXTRA_LAZY', orphanRemoval: true)] private Collection $records; /** @@ -121,15 +121,69 @@ class Format } /** - * Update bi-directional association with records. + * Set the format's namespace URI. * - * @param Record $record The record to remove from this metadata prefix + * @param string $namespace The namespace URI * * @return void + * + * @throws ValidationFailedException */ - public function removeRecord(Record $record): void + public function setNamespace(string $namespace): void { - $this->records->removeElement($record); + try { + $this->namespace = $this->validateUrl($namespace); + } catch (ValidationFailedException $exception) { + throw $exception; + } + } + + /** + * Set the format's schema URL. + * + * @param string $schema The schema URL + * + * @return void + * + * @throws ValidationFailedException + */ + public function setSchema(string $schema): void + { + try { + $this->xmlSchema = $this->validateUrl($schema); + } catch (ValidationFailedException $exception) { + throw $exception; + } + } + + /** + * Validate metadata prefix. + * + * @param string $prefix The metadata prefix + * + * @return string The validated prefix + * + * @throws ValidationFailedException + */ + protected function validatePrefix(string $prefix): string + { + $prefix = trim($prefix); + $validator = Validation::createValidator(); + $violations = $validator->validate( + $prefix, + [ + new Assert\Regex([ + 'pattern' => '/\s/', + 'match' => false, + 'message' => 'This value contains whitespaces.' + ]), + new Assert\NotBlank() + ] + ); + if ($violations->count() > 0) { + throw new ValidationFailedException(null, $violations); + } + return $prefix; } /** @@ -141,8 +195,9 @@ class Format * * @throws ValidationFailedException */ - protected function validate(string $url): string + protected function validateUrl(string $url): string { + $url = trim($url); $validator = Validation::createValidator(); $violations = $validator->validate($url, new Assert\Url()); if ($violations->count() > 0) { @@ -163,9 +218,9 @@ class Format public function __construct(string $prefix, string $namespace, string $schema) { try { - $this->prefix = $prefix; - $this->namespace = $this->validate($namespace); - $this->xmlSchema = $this->validate($schema); + $this->prefix = $this->validatePrefix($prefix); + $this->setNamespace($namespace); + $this->setSchema($schema); $this->records = new ArrayCollection(); } catch (ValidationFailedException $exception) { throw $exception; diff --git a/src/Database/Record.php b/src/Database/Record.php index 1d6af91..28f62e6 100644 --- a/src/Database/Record.php +++ b/src/Database/Record.php @@ -70,7 +70,7 @@ class Record /** * Collection of associated sets. * - * @var Collection + * @var Collection */ #[ORM\ManyToMany(targetEntity: Set::class, inversedBy: 'records', indexBy: 'spec')] #[ORM\JoinTable(name: 'records_sets')] @@ -79,39 +79,6 @@ class Record #[ORM\InverseJoinColumn(name: 'set_spec', referencedColumnName: 'spec')] private Collection $sets; - /** - * Get the record's content. - * - * @return string The record's content - */ - public function getContent(): string - { - return $this->content; - } - - /** - * Get the record identifier. - * - * @return string The record identifier - */ - public function getIdentifier(): string - { - return $this->identifier; - } - - /** - * Update bi-directional association with format. - * - * @param Format $format The metadata prefix - * - * @return void - */ - private function addFormat(Format $format): void - { - $this->format = $format; - $format->addRecord($this); - } - /** * Associate the record with a set. * @@ -127,6 +94,16 @@ class Record } } + /** + * Get the record's content. + * + * @return string The record's content + */ + public function getContent(): string + { + return $this->content; + } + /** * Get the associated format. * @@ -137,6 +114,16 @@ class Record return $this->format; } + /** + * Get the record identifier. + * + * @return string The record identifier + */ + public function getIdentifier(): string + { + return $this->identifier; + } + /** * Get the date and time of last change. * @@ -147,14 +134,26 @@ class Record return $this->lastChanged; } + /** + * Get a associated set. + * + * @param string $setSpec The set's spec + * + * @return ?Set The Set or NULL on failure + */ + public function getSet(string $setSpec): ?Set + { + return $this->sets->get($setSpec); + } + /** * Get a collection of associated sets. * - * @return Collection The associated sets + * @return array The associated sets */ - public function getSets(): Collection + public function getSets(): array { - return $this->sets; + return $this->sets->toArray(); } /** @@ -195,6 +194,19 @@ class Record $this->content = $data; } + /** + * Set format of the record. + * + * @param Format $format The record's format + * + * @return void + */ + protected function setFormat(Format $format): void + { + $this->format = $format; + $format->addRecord($this); + } + /** * Set date and time of last change. * @@ -242,7 +254,7 @@ class Record { try { $this->identifier = $identifier; - $this->addFormat($format); + $this->setFormat($format); $this->setContent($data); $this->setLastChanged(); $this->sets = new ArrayCollection(); diff --git a/src/Database/Set.php b/src/Database/Set.php index 6126082..666c046 100644 --- a/src/Database/Set.php +++ b/src/Database/Set.php @@ -60,7 +60,7 @@ class Set * * @var Collection */ - #[ORM\ManyToMany(targetEntity: Record::class, mappedBy: 'sets')] + #[ORM\ManyToMany(targetEntity: Record::class, mappedBy: 'sets', fetch: 'EXTRA_LAZY')] private Collection $records; /** @@ -110,11 +110,11 @@ class Set /** * Get a collection of associated records. * - * @return Collection The associated records + * @return array The associated records */ - public function getRecords(): Collection + public function getRecords(): array { - return $this->records; + return $this->records->toArray(); } /** @@ -138,7 +138,7 @@ class Set */ public function setDescription(string $description): void { - $this->description = $description; + $this->description = trim($description); } /**