Update ORM entity associations
This commit is contained in:
parent
31bf2a15a7
commit
a1e905ba93
|
@ -63,7 +63,7 @@ class Format
|
|||
*
|
||||
* @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;
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
|
|
@ -70,7 +70,7 @@ class Record
|
|||
/**
|
||||
* Collection of associated sets.
|
||||
*
|
||||
* @var Collection<int, Set>
|
||||
* @var Collection<string, Set>
|
||||
*/
|
||||
#[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<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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
|
|
@ -60,7 +60,7 @@ class Set
|
|||
*
|
||||
* @var Collection<int, Record>
|
||||
*/
|
||||
#[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<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
|
||||
{
|
||||
$this->description = $description;
|
||||
$this->description = trim($description);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue