152 lines
3.7 KiB
PHP
152 lines
3.7 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Entity;
|
|
|
|
use App\Repository\FoodOrderRepository;
|
|
use DateInterval;
|
|
use DateTimeImmutable;
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Symfony\Bridge\Doctrine\Types\UlidType;
|
|
use Symfony\Component\Uid\Ulid;
|
|
|
|
use function iterator_to_array;
|
|
|
|
#[ORM\Entity(repositoryClass: FoodOrderRepository::class)]
|
|
class FoodOrder
|
|
{
|
|
#[ORM\Column(nullable: true)]
|
|
private DateTimeImmutable|null $closedAt = null;
|
|
|
|
#[ORM\ManyToOne(inversedBy: 'foodOrders')]
|
|
#[ORM\JoinColumn(nullable: false)]
|
|
private FoodVendor|null $foodVendor = null;
|
|
|
|
/**
|
|
* @var Collection<int, OrderItem>
|
|
*/
|
|
#[ORM\OneToMany(targetEntity: OrderItem::class, mappedBy: 'foodOrder', orphanRemoval: true)]
|
|
private Collection $orderItems;
|
|
|
|
#[ORM\Column(length: 255, options: [
|
|
'default' => 'nobody',
|
|
])]
|
|
private string|null $createdBy = 'nobody';
|
|
|
|
public function __construct(
|
|
#[ORM\Id]
|
|
#[ORM\Column(type: UlidType::NAME, unique: true)]
|
|
private Ulid|null $id = new Ulid
|
|
) {
|
|
$this->orderItems = new ArrayCollection;
|
|
$this->open();
|
|
}
|
|
|
|
public function getId(): Ulid|null
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getCreatedAt(): DateTimeImmutable
|
|
{
|
|
return $this->id->getDateTime();
|
|
}
|
|
|
|
public function getClosedAt(): DateTimeImmutable|null
|
|
{
|
|
return $this->closedAt;
|
|
}
|
|
|
|
public function setClosedAt(DateTimeImmutable|null $closedAt): static
|
|
{
|
|
$this->closedAt = $closedAt;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function isClosed(): bool
|
|
{
|
|
return $this->closedAt instanceof DateTimeImmutable && $this->closedAt->getTimestamp() <= (new DateTimeImmutable)->getTimestamp();
|
|
}
|
|
|
|
public function close(): static
|
|
{
|
|
return $this->setClosedAt(new DateTimeImmutable);
|
|
}
|
|
|
|
public function open(): static
|
|
{
|
|
$this->closedAt = (new DateTimeImmutable)->add(new DateInterval('PT1H'));
|
|
return $this;
|
|
}
|
|
|
|
public function getFoodVendor(): FoodVendor|null
|
|
{
|
|
return $this->foodVendor;
|
|
}
|
|
|
|
public function setFoodVendor(FoodVendor|null $foodVendor): static
|
|
{
|
|
$this->foodVendor = $foodVendor;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, OrderItem>
|
|
*/
|
|
public function getOrderItems(): Collection
|
|
{
|
|
return $this->orderItems;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, OrderItem>
|
|
*/
|
|
public function getOrderItemsSortedByName(): Collection
|
|
{
|
|
$iterator = $this->getOrderItems()
|
|
->getIterator();
|
|
$iterator->uasort(
|
|
static fn(OrderItem $a, OrderItem $b): int => $a->getName() <=> $b->getName()
|
|
);
|
|
return new ArrayCollection(
|
|
iterator_to_array(
|
|
$iterator
|
|
)
|
|
);
|
|
}
|
|
|
|
public function addOrderItem(OrderItem $orderItem): static
|
|
{
|
|
if (! $this->orderItems->contains($orderItem)) {
|
|
$this->orderItems->add($orderItem);
|
|
$orderItem->setFoodOrder($this);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeOrderItem(OrderItem $orderItem): static
|
|
{
|
|
// set the owning side to null (unless already changed)
|
|
if ($this->orderItems->removeElement($orderItem) && $orderItem->getFoodOrder() === $this) {
|
|
$orderItem->setFoodOrder(null);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getCreatedBy(): string|null
|
|
{
|
|
return $this->createdBy;
|
|
}
|
|
|
|
public function setCreatedBy(string $createdBy): static
|
|
{
|
|
$this->createdBy = $createdBy;
|
|
|
|
return $this;
|
|
}
|
|
}
|