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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
 
namespace App\Entity;
 
use App\Enum\LabelType;
use App\Repository\OustedBookRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
 
/**
 * A book taken out of the collection: given away, lost, destroyed.
 */
#[ORM\Entity(repositoryClass: OustedBookRepository::class)]
class OustedBook extends AbstractBook {
    #[ORM\Column(length: 255)]
    #[Assert\NotBlank]
    #[Assert\Length(max: 255)]
    private string $reason;
 
    /** @var Collection<int, OustedLabel> */
    #[ORM\OneToMany(targetEntity: OustedLabel::class, mappedBy: 'book', cascade: ['persist', 'remove'], orphanRemoval: true)]
    #[ORM\OrderBy(['label' => 'ASC'])]
    private Collection $labels;
 
    /** @var Collection<int, OustedBorrowHistory> */
    #[ORM\OneToMany(targetEntity: OustedBorrowHistory::class, mappedBy: 'book', cascade: ['persist', 'remove'], orphanRemoval: true)]
    #[ORM\OrderBy(['at' => 'DESC'])]
    private Collection $borrowHistory;
 
    public function __construct(string $reason) {
        $this->reason = $reason;
        $this->labels = new ArrayCollection();
        $this->borrowHistory = new ArrayCollection();
    }
 
    public static function fromBook(Book $book, string $reason): self {
        $ousted = new self($reason);
        $ousted->copyDataFrom($book);
 
        return $ousted;
    }
 
    public function getReason(): string {
        return $this->reason;
    }
 
    /**
     * @return Collection<int, OustedLabel>
     */
    public function getLabels(): Collection {
        return $this->labels;
    }
 
    protected function createLabel(string $label, LabelType $type): OustedLabel {
        return new OustedLabel($this, $label, $type);
    }
 
    /**
     * @return Collection<int, OustedBorrowHistory>
     */
    public function getBorrowHistory(): Collection {
        return $this->borrowHistory;
    }
 
    public function addBorrowHistory(string $who, \DateTimeImmutable $at, \DateTimeImmutable $till): void {
        $this->borrowHistory->add(new OustedBorrowHistory($this, $who, $at, $till));
    }
}