<?php
namespace EADPlataforma\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use EADPlataforma\Validator\Constraints as EADAssert;
use EADPlataforma\Services\FileService;
use EADPlataforma\Util\StringUtil;
use EADPlataforma\Enum\CategoryEnum;
use \DateTime;
/**
* Category
*
* @ORM\Table(name="category", indexes={
* @ORM\Index(name="fk_category_user_delete_id", columns={"user_delete_id"})
*})
*
* @ORM\Entity(repositoryClass="EADPlataforma\Repository\CategoryRepository")
*
* @UniqueEntity(
* fields={"slug"},
* message="This slug is already in use"
* )
*/
class Category
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @Assert\NotBlank(
* message = "Deleted not informed"
* )
*
* @Assert\Choice(
* choices = {
* CategoryEnum::ITEM_NO_DELETED,
* CategoryEnum::ITEM_ON_TRASH,
* CategoryEnum::ITEM_DELETED
* },
* message = "Delete Option Invalid"
* )
*
* @var int
*
* @ORM\Column(name="deleted", type="integer", nullable=false, options={"default"="0"})
*/
private $deleted = CategoryEnum::ITEM_NO_DELETED;
/**
* @Assert\NotBlank(
* message = "Status not informed"
* )
*
* @Assert\Choice(
* choices = { CategoryEnum::DRAFT, CategoryEnum::PUBLISHED },
* message = "Status Invalid"
* )
*
* @var int
*
* @ORM\Column(name="status", type="integer", nullable=false, options={"default"="1"})
*/
private $status = CategoryEnum::PUBLISHED;
/**
* @Assert\NotBlank(
* message = "Order not informed"
* )
*
* @var int
*
* @ORM\Column(name="`order`", type="integer", nullable=false)
*/
private $order;
/**
* @Assert\NotBlank(
* message = "Category not informed"
* )
*
* @Assert\Length(
* min = 0,
* max = 75
* )
*
* @var string
*
* @ORM\Column(name="category", type="string", length=80, nullable=false)
*/
private $category;
/**
* @Assert\NotBlank(
* message = "Slug not informed"
* )
*
* @Assert\Length(
* min = 0,
* max = 75
* )
*
* @var string
*
* @ORM\Column(name="slug", type="string", length=80, nullable=false)
*/
private $slug;
/**
* @var string|null
*
* @Assert\Length(
* min = 0,
* max = 255
* )
*
* @ORM\Column(name="image", type="string", length=255, nullable=true)
*/
private $image;
/**
* @var string|null
*
* @Assert\Length(
* min = 0,
* max = 255
* )
*
* @ORM\Column(name="icon", type="string", length=255, nullable=true)
*/
private $icon;
/**
* @var string|null
*
* @ORM\Column(name="page_color_text", type="string", length=20, nullable=true)
*/
private $pageColorText;
/**
* @var string|null
*
* @ORM\Column(name="description", type="text", length=65535, nullable=true)
*/
private $description;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Product", mappedBy="category")
*/
private $product;
/**
* @Assert\Valid
*
* @var \EADPlataforma\Entity\User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_delete_id", referencedColumnName="id", nullable=true)
* })
*/
private $userDelete;
/**
* @Assert\Choice(
* choices = {
* CategoryEnum::INDIVIDUAL,
* CategoryEnum::CASCADE
* },
* message = "Type Delete Invalid"
* )
*
* @var int
*
* @ORM\Column(name="type_delete", type="integer", nullable=true)
*/
private $typeDelete;
/**
* @EADAssert\DateTimeEAD(
* message = "Date Delete Invalid"
* )
*
* @var \DateTime|null
*
* @ORM\Column(name="date_delete", type="datetime", nullable=true)
*/
private $dateDelete;
/**
* Constructor
*/
public function __construct()
{
$this->product = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
public function getOrder(): ?int
{
return $this->order;
}
public function setOrder(int $order): self
{
$this->order = $order;
return $this;
}
public function getCategory(): ?string
{
return StringUtil::fromUnicode(StringUtil::encodeStringStatic($this->category));
}
public function setCategory(string $category): self
{
$this->category = StringUtil::toUnicode($category);
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = StringUtil::slugStatic($slug);
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function getIcon(): ?string
{
return $this->icon;
}
public function setIcon(?string $icon): self
{
$this->icon = $icon;
return $this;
}
public function getPageColorText(): ?string
{
return $this->pageColorText;
}
public function setPageColorText(?string $pageColorText): self
{
$this->pageColorText = $pageColorText;
return $this;
}
public function getDescription(): ?string
{
return StringUtil::encodeStringStatic($this->description);
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getUserDelete(): ?User
{
return $this->userDelete;
}
public function setUserDelete(?User $userDelete): self
{
$this->userDelete = $userDelete;
return $this;
}
public function getDateDelete($dateFormat = 'Y-m-d H:i:s')
{
if($this->dateDelete){
return $this->dateDelete->format($dateFormat);
}
return $this->dateDelete;
}
public function setDateDelete($dateDelete): self
{
if(!empty($dateDelete)){
$dateDelete = DateTime::createFromFormat('Y-m-d H:i:s', $dateDelete);
}
$this->dateDelete = $dateDelete;
return $this;
}
/**
* @return Collection|Product[]
*/
public function getProduct(): Collection
{
return $this->product;
}
public function hasProduct(): bool
{
return (count($this->product) > 0);
}
public function addProduct(Product $product): self
{
if (!$this->product->contains($product)) {
$this->product[] = $product;
$product->addCategory($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->product->contains($product)) {
$this->product->removeElement($product);
$product->removeCategory($this);
}
return $this;
}
public function individual(): self
{
$this->typeDelete = CategoryEnum::INDIVIDUAL;
return $this;
}
public function cascade(): self
{
$this->typeDelete = CategoryEnum::CASCADE;
return $this;
}
public function isOnTrash(): bool
{
return ($this->deleted == CategoryEnum::ITEM_ON_TRASH);
}
public function isDeleted(): bool
{
return ($this->deleted == CategoryEnum::ITEM_DELETED);
}
public function restore(): self
{
$this->deleted = CategoryEnum::ITEM_NO_DELETED;
return $this;
}
public function trash(): self
{
$this->deleted = CategoryEnum::ITEM_ON_TRASH;
return $this;
}
public function delete(): self
{
$this->deleted = CategoryEnum::ITEM_DELETED;
return $this;
}
public function toReturn(?bool $addProduct = true){
$data = [
"id" => $this->id,
"deleted" => $this->deleted,
"status" => $this->status,
"order" => $this->order,
"category" => $this->getCategory(),
"slug" => $this->slug,
"image" => FileService::getFilePathComplete(
$this->image,
CategoryEnum::PATH_OTHERS,
true,
true
),
"icon" => FileService::getFilePathComplete(
$this->icon,
CategoryEnum::PATH_OTHERS,
true,
true
),
"pageColorText" => $this->pageColorText,
"description" => $this->getDescription(),
"userDelete" => ( $this->userDelete ? $this->userDelete->getId() : null ),
"typeDelete" => $this->typeDelete,
"dateDelete" => $this->getDateDelete()
];
if($addProduct){
$arrProduct = [];
foreach ($this->product as $key => $product) {
$arrProduct[] = (object)[
"id" => $product->getId(),
"title" => $product->getTitle(),
];
}
$data["product"] = $arrProduct;
}
return $data;
}
public function toReturnApi(){
$data = [
"id" => $this->id,
"status" => $this->status,
"ordem" => $this->order,
"categoria" => $this->category,
"slug" => $this->slug,
"descricao" => $this->description,
"imagem" => FileService::getFilePathComplete(
$this->image,
CategoryEnum::PATH_OTHERS,
true,
true
),
"icone" => FileService::getFilePathComplete(
$this->icon,
CategoryEnum::PATH_OTHERS,
true,
true
),
"corTexto" => $this->pageColorText
];
return $data;
}
}