futtern/src/Entity/FoodVendor.php
lubiana 98feafa3fc
All checks were successful
/ ls (pull_request) Successful in 1m35s
/ ls (push) Successful in 1m31s
ammend
2025-06-21 15:20:08 +02:00

185 lines
4.7 KiB
PHP

<?php declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\FoodVendorRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
use Symfony\Bridge\Doctrine\Types\UlidType;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Validator\Constraints\Length;
use function mb_strlen;
#[ORM\Entity(repositoryClass: FoodVendorRepository::class)]
#[ApiResource]
class FoodVendor
{
#[ORM\Column(length: 50)]
#[Groups(['food_order:latest', 'food_vendor:read'])]
private string|null $name = null;
#[ORM\Column(length: 50, nullable: true, options: [
'default' => '',
])]
#[Groups(['food_order:latest', 'food_vendor:read'])]
private string|null $phone = null;
/**
* @var Collection<int, FoodOrder>
*/
#[ORM\OneToMany(targetEntity: FoodOrder::class, mappedBy: 'foodVendor', orphanRemoval: true)]
private Collection $foodOrders;
/**
* @var Collection<int, MenuItem>
*/
#[ORM\OneToMany(targetEntity: MenuItem::class, mappedBy: 'foodVendor', orphanRemoval: true)]
private Collection $menuItems;
#[ORM\Column(length: 255, nullable: true)]
#[Groups(['food_order:latest'])]
private string|null $menuLink = null;
/**
* String of emojis (max 30 characters)
*/
#[ORM\Column(length: 30, nullable: true)]
#[Groups(['food_order:latest', 'food_vendor:read'])]
#[Length(max: 10)]
private string|null $emojis = null;
public function __construct(
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\Column(type: UlidType::NAME, unique: true)]
#[ORM\CustomIdGenerator(class: UlidGenerator::class)]
#[Groups(['food_order:latest'])]
private Ulid|null $id = new Ulid
) {
$this->id ??= new Ulid;
$this->foodOrders = new ArrayCollection;
$this->menuItems = new ArrayCollection;
}
public function getId(): Ulid|null
{
return $this->id;
}
public function getName(): string|null
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
/**
* @return Collection<int, FoodOrder>
*/
public function getFoodOrders(): Collection
{
return $this->foodOrders;
}
public function addFoodOrder(FoodOrder $foodOrder): static
{
if (! $this->foodOrders->contains($foodOrder)) {
$this->foodOrders->add($foodOrder);
$foodOrder->setFoodVendor($this);
}
return $this;
}
public function removeFoodOrder(FoodOrder $foodOrder): static
{
// set the owning side to null (unless already changed)
if ($this->foodOrders->removeElement($foodOrder)) {
$foodOrder->setFoodVendor(null);
}
return $this;
}
/**
* @return Collection<int, MenuItem>
*/
public function getMenuItems(bool $withDeleted = false): Collection
{
if ($withDeleted) {
return $this->menuItems;
}
return $this->menuItems->filter(
static fn(MenuItem $item): bool => $item->isDeleted() === false
);
}
public function addMenuItem(MenuItem $menuItem): static
{
if (! $this->menuItems->contains($menuItem)) {
$this->menuItems->add($menuItem);
$menuItem->setFoodVendor($this);
}
return $this;
}
public function removeMenuItem(MenuItem $menuItem): static
{
// set the owning side to null (unless already changed)
if ($this->menuItems->removeElement($menuItem)) {
$menuItem->setFoodVendor(null);
}
return $this;
}
public function getMenuLink(): string|null
{
return $this->menuLink;
}
public function setMenuLink(string|null $menuLink): static
{
$this->menuLink = $menuLink;
return $this;
}
public function getPhone(): string|null
{
return $this->phone;
}
public function setPhone(string|null $phone): static
{
$this->phone = $phone;
return $this;
}
public function getEmojis(): string|null
{
return $this->emojis;
}
public function setEmojis(string|null $emojis): static
{
if ($emojis !== null && mb_strlen($emojis) > 30) {
throw new InvalidArgumentException('A maximum of 30 characters is allowed for emojis');
}
$this->emojis = $emojis;
return $this;
}
}