<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\AttributesRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: AttributesRepository::class)]
#[ApiResource(
normalizationContext: ['groups' => ['attributes:read']],
denormalizationContext: ['groups' => ['attributes:write']],
order: ['sort' => 'ASC', 'name' => 'ASC', 'attributeItems.sort' => 'ASC', 'attributeItems.name' => 'ASC'],
)]
#[ApiFilter(SearchFilter::class, properties: ['name' => 'ipartial', 'category.id' => 'exact'])]
class Attributes
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['attributes:read', 'attributes:write', 'cat:read', 'product:read', 'attributes_items:read', 'site_product:read'])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(['attributes:read', 'attributes:write', 'cat:read', 'product:read', 'attributes_items:read', 'site_product:read'])]
private ?string $name = null;
#[Groups(['attributes:read', 'attributes:write'])]
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $date_entered = null;
#[Groups(['attributes:read', 'attributes:write'])]
#[ORM\ManyToMany(targetEntity: Category::class, inversedBy: 'attributes')]
private Collection $category;
#[Groups(['attributes:read', 'attributes:write', 'cat:read', 'product:read', 'site_product:read'])]
#[ORM\Column(length: 255)]
private ?string $slug = null;
#[Groups(['cat:read', 'attributes:read', 'attributes:write'])]
#[ORM\OneToMany(mappedBy: 'attribute', targetEntity: AttributeItems::class, cascade:['persist', 'remove'])]
private Collection $attributeItems;
#[ORM\Column(nullable: true)]
private ?int $sort = null;
public function __construct()
{
$this->category = new ArrayCollection();
$this->attributeItems = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDateEntered(): ?\DateTimeInterface
{
return $this->date_entered;
}
public function setDateEntered(?\DateTimeInterface $date_entered): self
{
$this->date_entered = $date_entered;
return $this;
}
/**
* @return Collection<int, Category>
*/
public function getCategory(): Collection
{
return $this->category;
}
public function addCategory(Category $category): self
{
if (!$this->category->contains($category)) {
$this->category->add($category);
}
return $this;
}
public function removeCategory(Category $category): self
{
$this->category->removeElement($category);
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
/**
* @return Collection<int, AttributeItems>
*/
public function getAttributeItems(): Collection
{
return $this->attributeItems;
}
public function addAttributeItem(AttributeItems $attributeItem): self
{
if (!$this->attributeItems->contains($attributeItem)) {
$this->attributeItems->add($attributeItem);
$attributeItem->setAttribute($this);
}
return $this;
}
public function removeAttributeItem(AttributeItems $attributeItem): self
{
if ($this->attributeItems->removeElement($attributeItem)) {
// set the owning side to null (unless already changed)
if ($attributeItem->getAttribute() === $this) {
$attributeItem->setAttribute(null);
}
}
return $this;
}
public function getSort(): ?int
{
return $this->sort;
}
public function setSort(int $sort): self
{
$this->sort = $sort;
return $this;
}
}