<?php
|
declare(strict_types=1);
|
|
namespace App\Entity;
|
|
use App\Enum\LabelType;
|
use Doctrine\ORM\Mapping as ORM;
|
|
#[ORM\MappedSuperclass]
|
#[ORM\HasLifecycleCallbacks]
|
abstract class AbstractLabel {
|
#[ORM\Id]
|
#[ORM\GeneratedValue]
|
#[ORM\Column]
|
private ?int $id = null;
|
|
#[ORM\Column(length: 16, enumType: LabelType::class)]
|
private LabelType $type;
|
|
#[ORM\Column(length: 255)]
|
private string $label;
|
|
public function __construct(string $label, LabelType $type) {
|
$this->label = $label;
|
$this->type = $type;
|
}
|
|
/**
|
* Last line of defence: forms validate first, but no path may store a malformed number.
|
* Labels are immutable, so checking them once is enough.
|
*/
|
#[ORM\PrePersist]
|
public function assertValid(): void {
|
if (!$this->type->isValid($this->label)) {
|
throw new \DomainException(\sprintf('"%s" is not a valid %s label.', $this->label, $this->type->value));
|
}
|
}
|
|
public function getId(): ?int {
|
return $this->id;
|
}
|
|
public function getType(): LabelType {
|
return $this->type;
|
}
|
|
public function getLabel(): string {
|
return $this->label;
|
}
|
}
|