src/Flexy/ShopBundle/Entity/Product/CategoryProduct.php line 34
<?php
namespace App\Flexy\ShopBundle\Entity\Product;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Flexy\ShopBundle\Entity\Product\Product;
use App\Flexy\ShopBundle\Entity\Promotion\Promotion;
use App\Flexy\ShopBundle\Repository\Product\CategoryProductRepository;
use App\Flexy\ShopBundle\Service\FlexyShopStatisticProvider;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Table;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use Gedmo\Translatable\Translatable;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\Common\Collections\Criteria;
#[ApiResource(
normalizationContext: ['groups' => ['read','readDeep']],
denormalizationContext: ['groups' => ['write']],
)]
#[ApiFilter(SearchFilter::class, properties: ['parentCategory'])]
#[ApiFilter(ExistsFilter::class, properties: ['parentCategory'])]
#[Table(name: 'flexy_categoryproduct')]
#[ORM\Entity(repositoryClass: CategoryProductRepository::class)]
#[ORM\Cache(usage:"NONSTRICT_READ_WRITE",region:"append_depend")]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt', timeAware: false, hardDelete: true)]
class CategoryProduct implements \Stringable,Translatable
{
#[Groups("read")]
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[Groups("read")]
#[ORM\Column(type: 'string', length: 255)]
#[Gedmo\Translatable]
private ?string $name = null;
#[Groups("read")]
#[ORM\Column(type: 'text', nullable: true)]
private ?string $description = null;
#[Groups("read")]
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $image = null;
#[Groups("read")]
#[ORM\ManyToMany(targetEntity: Product::class, mappedBy: 'categoriesProduct', cascade: ['persist','remove'])]
private \Doctrine\Common\Collections\Collection|array $products;
#[Groups("readDeep")]
#[ORM\ManyToOne(targetEntity: CategoryProduct::class, inversedBy: 'subCategories')]
private ?\App\Flexy\ShopBundle\Entity\Product\CategoryProduct $parentCategory = null;
#[Groups("readDeep")]
#[ORM\OneToMany(targetEntity: CategoryProduct::class, mappedBy: 'parentCategory')]
private $subCategories;
#[ORM\ManyToMany(targetEntity: Attribut::class, inversedBy: 'categoriesProduct')]
private \Doctrine\Common\Collections\Collection|array $attributes;
/**
* #ignoredForAi
*/
#[ORM\Column(type: 'float', nullable: true)]
private ?float $commission = null;
#[ORM\OneToMany(targetEntity: Product::class, mappedBy: 'parentCategory')]
private \Doctrine\Common\Collections\Collection|array $productsChildrens;
/**
*
* @example [simple|variable|pack|subscription] default: simple
*/
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $forProductType = null;
/**
* #ignoredForAi
*/
#[ORM\Column(type: 'integer', nullable: true)]
private ?int $oldId = null;
#[Groups("read")]
#[ORM\Column(length: 255, nullable: true)]
private ?string $icon = null;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $deletedAt = null;
/**
* @Gedmo\Slug(fields={"title"})
*/
#[Gedmo\Slug(fields: ['name'],updatable:false)]
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups(['read','write'])]
private ?string $slug = null;
#[ORM\ManyToOne(inversedBy: 'categories')]
private ?Promotion $promotion = null;
#[ORM\Column(type: 'integer', nullable: true)]
private $position;
public function __construct(
)
{
$this->products = new ArrayCollection();
$this->attributes = new ArrayCollection();
$this->subCategories = new ArrayCollection();
$this->productsChildrens = new ArrayCollection();
}
public function __toString(): string
{
$name = $this->name;
if($this->getParentCategory()){
$name = $this->getParentCategory()->getName()." > ".$this->name;
}
return (string) $name;
}
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 getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function getProducts()
{
$data = [];
foreach( $this->products as $singleProduct ){
if($singleProduct->getIsPublished()){
$data[] = $singleProduct;
}
}
// Custom sorting by position
usort($data, function($a, $b) {
return $a->getPosition() <=> $b->getPosition();
});
return $data;
}
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
}
return $this;
}
public function removeProduct(Product $product): self
{
$this->products->removeElement($product);
return $this;
}
public function getParentCategory(): ?self
{
return $this->parentCategory;
}
public function setParentCategory(?self $parentCategory): self
{
$this->parentCategory = $parentCategory;
return $this;
}
/**
* Get the value of subCategories
*/
public function getSubCategories()
{
$data = $this->subCategories->toArray();
// Custom sorting by position
usort($data, function($a, $b) {
return $a->getPosition() <=> $b->getPosition();
});
return $data;
}
/**
* Set the value of subCategories
*
* @return self
*/
public function setSubCategories($subCategories)
{
$this->subCategories = $subCategories;
return $this;
}
/**
* @return Collection|Attribut[]
*/
public function getAttributes(): Collection
{
return $this->attributes;
}
public function addAttribute(Attribut $attribute): self
{
if (!$this->attributes->contains($attribute)) {
$this->attributes[] = $attribute;
}
return $this;
}
public function removeAttribute(Attribut $attribute): self
{
$this->attributes->removeElement($attribute);
return $this;
}
public function getCommission(): ?float
{
return $this->commission;
}
public function setCommission(?float $commission): self
{
$this->commission = $commission;
return $this;
}
public function addSubCategory(CategoryProduct $subCategory): self
{
if (!$this->subCategories->contains($subCategory)) {
$this->subCategories[] = $subCategory;
$subCategory->setParentCategory($this);
}
return $this;
}
public function removeSubCategory(CategoryProduct $subCategory): self
{
if ($this->subCategories->removeElement($subCategory)) {
// set the owning side to null (unless already changed)
if ($subCategory->getParentCategory() === $this) {
$subCategory->setParentCategory(null);
}
}
return $this;
}
/**
* @return Collection<int, Product>
*/
public function getProductsChildrens(): Collection
{
return $this->productsChildrens;
}
public function addProductsChildren(Product $productsChildren): self
{
if (!$this->productsChildrens->contains($productsChildren)) {
$this->productsChildrens[] = $productsChildren;
$productsChildren->setParentCategory($this);
}
return $this;
}
public function removeProductsChildren(Product $productsChildren): self
{
if ($this->productsChildrens->removeElement($productsChildren)) {
// set the owning side to null (unless already changed)
if ($productsChildren->getParentCategory() === $this) {
$productsChildren->setParentCategory(null);
}
}
return $this;
}
public function getForProductType(): ?string
{
return $this->forProductType;
}
public function setForProductType(?string $forProductType): self
{
$this->forProductType = $forProductType;
return $this;
}
public function getOldId(): ?int
{
return $this->oldId;
}
public function setOldId(?int $oldId): self
{
$this->oldId = $oldId;
return $this;
}
public function getIcon(): ?string
{
return $this->icon;
}
public function setIcon(?string $icon): static
{
$this->icon = $icon;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getPromotion(): ?Promotion
{
return $this->promotion;
}
public function setPromotion(?Promotion $promotion): static
{
$this->promotion = $promotion;
return $this;
}
/**
* Get the value of deletedAt
*/
public function getDeletedAt()
{
return $this->deletedAt;
}
/**
* Set the value of deletedAt
*
* @return self
*/
public function setDeletedAt($deletedAt)
{
$this->deletedAt = $deletedAt;
return $this;
}
/**
* Get the value of position
*/
public function getPosition()
{
return $this->position;
}
/**
* Set the value of position
*
* @return self
*/
public function setPosition($position)
{
$this->position = $position;
return $this;
}
}