Jacek Kowalski
4 hours ago 6b9278a5d57091fce636dbb640b380cfb990db6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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;
    }
}