Update ORM entity associations

This commit is contained in:
Sebastian Meyer 2024-01-04 05:20:55 +01:00
parent 31bf2a15a7
commit a1e905ba93
3 changed files with 119 additions and 52 deletions

View File

@ -63,7 +63,7 @@ class Format
* *
* @var Collection<int, Record> * @var Collection<int, Record>
*/ */
#[ORM\OneToMany(targetEntity: Record::class, mappedBy: 'format')] #[ORM\OneToMany(targetEntity: Record::class, mappedBy: 'format', fetch: 'EXTRA_LAZY', orphanRemoval: true)]
private Collection $records; 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 * @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 * @throws ValidationFailedException
*/ */
protected function validate(string $url): string protected function validateUrl(string $url): string
{ {
$url = trim($url);
$validator = Validation::createValidator(); $validator = Validation::createValidator();
$violations = $validator->validate($url, new Assert\Url()); $violations = $validator->validate($url, new Assert\Url());
if ($violations->count() > 0) { if ($violations->count() > 0) {
@ -163,9 +218,9 @@ class Format
public function __construct(string $prefix, string $namespace, string $schema) public function __construct(string $prefix, string $namespace, string $schema)
{ {
try { try {
$this->prefix = $prefix; $this->prefix = $this->validatePrefix($prefix);
$this->namespace = $this->validate($namespace); $this->setNamespace($namespace);
$this->xmlSchema = $this->validate($schema); $this->setSchema($schema);
$this->records = new ArrayCollection(); $this->records = new ArrayCollection();
} catch (ValidationFailedException $exception) { } catch (ValidationFailedException $exception) {
throw $exception; throw $exception;

View File

@ -70,7 +70,7 @@ class Record
/** /**
* Collection of associated sets. * Collection of associated sets.
* *
* @var Collection<int, Set> * @var Collection<string, Set>
*/ */
#[ORM\ManyToMany(targetEntity: Set::class, inversedBy: 'records', indexBy: 'spec')] #[ORM\ManyToMany(targetEntity: Set::class, inversedBy: 'records', indexBy: 'spec')]
#[ORM\JoinTable(name: 'records_sets')] #[ORM\JoinTable(name: 'records_sets')]
@ -79,39 +79,6 @@ class Record
#[ORM\InverseJoinColumn(name: 'set_spec', referencedColumnName: 'spec')] #[ORM\InverseJoinColumn(name: 'set_spec', referencedColumnName: 'spec')]
private Collection $sets; 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. * 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. * Get the associated format.
* *
@ -137,6 +114,16 @@ class Record
return $this->format; 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. * Get the date and time of last change.
* *
@ -147,14 +134,26 @@ class Record
return $this->lastChanged; 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. * Get a collection of associated sets.
* *
* @return Collection<int, Set> The associated sets * @return array<string, Set> 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; $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. * Set date and time of last change.
* *
@ -242,7 +254,7 @@ class Record
{ {
try { try {
$this->identifier = $identifier; $this->identifier = $identifier;
$this->addFormat($format); $this->setFormat($format);
$this->setContent($data); $this->setContent($data);
$this->setLastChanged(); $this->setLastChanged();
$this->sets = new ArrayCollection(); $this->sets = new ArrayCollection();

View File

@ -60,7 +60,7 @@ class Set
* *
* @var Collection<int, Record> * @var Collection<int, Record>
*/ */
#[ORM\ManyToMany(targetEntity: Record::class, mappedBy: 'sets')] #[ORM\ManyToMany(targetEntity: Record::class, mappedBy: 'sets', fetch: 'EXTRA_LAZY')]
private Collection $records; private Collection $records;
/** /**
@ -110,11 +110,11 @@ class Set
/** /**
* Get a collection of associated records. * Get a collection of associated records.
* *
* @return Collection<int, Record> The associated records * @return array<int, Record> 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 public function setDescription(string $description): void
{ {
$this->description = $description; $this->description = trim($description);
} }
/** /**