<?php
declare(strict_types=1);
namespace App\Shop\Doctrine\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Shop\Doctrine\Repository\BrandRepository;
use App\Shop\Filter\BrandFilter;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\Table;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints\NotBlank;
use Vich\UploaderBundle\Mapping\Annotation\Uploadable;
use Vich\UploaderBundle\Mapping\Annotation\UploadableField;
#[ApiResource(
collectionOperations: ['get'],
itemOperations: ['get'],
attributes: ['pagination_enabled' => false],
normalizationContext: ['groups' => ['read']]
)]
#[Uploadable]
#[ApiFilter(BrandFilter::class)]
#[Entity(repositoryClass: BrandRepository::class)]
#[Table(name: '`shop_brand`')]
class Brand
{
#[Id]
#[Column(type: 'integer')]
#[GeneratedValue]
#[Groups(['read', 'product'])]
private ?int $id = null;
#[Column]
#[NotBlank]
#[Groups(['read', 'product'])]
private string $name;
#[Column(nullable: true)]
private ?string $logo = null;
#[UploadableField(mapping: 'images', fileNameProperty: 'logo')]
private ?File $logoFile = null;
#[Column(type: 'datetime_immutable')]
private DateTimeImmutable $updatedAt;
/**
* @var Collection<int, Product>
*/
#[OneToMany(mappedBy: 'brand', targetEntity: Product::class, fetch: 'EXTRA_LAZY')]
private Collection $products;
public function __construct()
{
$this->updatedAt = new DateTimeImmutable();
$this->products = new ArrayCollection();
}
public function __toString(): string
{
return $this->getName();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
$this->updatedAt = new DateTimeImmutable();
}
public function getLogo(): ?string
{
return $this->logo;
}
public function setLogo(?string $logo): void
{
$this->logo = $logo;
}
public function getLogoFile(): ?File
{
return $this->logoFile;
}
public function setLogoFile(?File $logoFile): void
{
$this->logoFile = $logoFile;
if (null !== $logoFile) {
$this->updatedAt = new DateTimeImmutable();
}
}
public function getUpdatedAt(): DateTimeImmutable
{
return $this->updatedAt;
}
/**
* @return Collection<int, Product>
*/
public function getProducts(): Collection
{
return $this->products;
}
}