<?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));
|
}
|
}
|