src/Entity/Category.php line 241

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\Common\Collections\Collection;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  7. use EADPlataforma\Validator\Constraints as EADAssert;
  8. use EADPlataforma\Services\FileService;
  9. use EADPlataforma\Util\StringUtil;
  10. use EADPlataforma\Enum\CategoryEnum;
  11. use \DateTime;
  12. /**
  13.  * Category
  14.  *
  15.  * @ORM\Table(name="category", indexes={
  16.  *      @ORM\Index(name="fk_category_user_delete_id", columns={"user_delete_id"})
  17.  *})
  18.  *
  19.  * @ORM\Entity(repositoryClass="EADPlataforma\Repository\CategoryRepository")
  20.  *
  21.  * @UniqueEntity(
  22.  *     fields={"slug"},
  23.  *     message="This slug is already in use"
  24.  * )
  25.  */
  26. class Category
  27. {
  28.     /**
  29.      * @var int
  30.      *
  31.      * @ORM\Column(name="id", type="integer", nullable=false)
  32.      * @ORM\Id
  33.      * @ORM\GeneratedValue(strategy="IDENTITY")
  34.      */
  35.     private $id;
  36.     /**
  37.      * @Assert\NotBlank(
  38.      *      message = "Deleted not informed"
  39.      * )
  40.      *
  41.      * @Assert\Choice(
  42.      *      choices = { 
  43.      *                      CategoryEnum::ITEM_NO_DELETED, 
  44.      *                      CategoryEnum::ITEM_ON_TRASH,
  45.      *                      CategoryEnum::ITEM_DELETED
  46.      *                },
  47.      *      message = "Delete Option Invalid"
  48.      * )
  49.      *
  50.      * @var int
  51.      *
  52.      * @ORM\Column(name="deleted", type="integer", nullable=false, options={"default"="0"})
  53.      */
  54.     private $deleted CategoryEnum::ITEM_NO_DELETED;
  55.     /**
  56.      * @Assert\NotBlank(
  57.      *      message = "Status not informed"
  58.      * )
  59.      *
  60.      * @Assert\Choice(
  61.      *      choices = { CategoryEnum::DRAFT, CategoryEnum::PUBLISHED },
  62.      *      message = "Status Invalid"
  63.      * )
  64.      *
  65.      * @var int
  66.      *
  67.      * @ORM\Column(name="status", type="integer", nullable=false, options={"default"="1"})
  68.      */
  69.     private $status CategoryEnum::PUBLISHED;
  70.     /**
  71.      * @Assert\NotBlank(
  72.      *      message = "Order not informed"
  73.      * )
  74.      *
  75.      * @var int
  76.      *
  77.      * @ORM\Column(name="`order`", type="integer", nullable=false)
  78.      */
  79.     private $order;
  80.     /**
  81.      * @Assert\NotBlank(
  82.      *      message = "Category not informed"
  83.      * )
  84.      * 
  85.      * @Assert\Length(
  86.      *      min = 0,
  87.      *      max = 75
  88.      * )
  89.      *
  90.      * @var string
  91.      *
  92.      * @ORM\Column(name="category", type="string", length=80, nullable=false)
  93.      */
  94.     private $category;
  95.     /**
  96.      * @Assert\NotBlank(
  97.      *      message = "Slug not informed"
  98.      * )
  99.      * 
  100.      * @Assert\Length(
  101.      *      min = 0,
  102.      *      max = 75
  103.      * )
  104.      *
  105.      * @var string
  106.      *
  107.      * @ORM\Column(name="slug", type="string", length=80, nullable=false)
  108.      */
  109.     private $slug;
  110.     /**
  111.      * @var string|null
  112.      * 
  113.      * @Assert\Length(
  114.      *      min = 0,
  115.      *      max = 255
  116.      * )
  117.      *
  118.      * @ORM\Column(name="image", type="string", length=255, nullable=true)
  119.      */
  120.     private $image;
  121.     /**
  122.      * @var string|null
  123.      * 
  124.      * @Assert\Length(
  125.      *      min = 0,
  126.      *      max = 255
  127.      * )
  128.      *
  129.      * @ORM\Column(name="icon", type="string", length=255, nullable=true)
  130.      */
  131.     private $icon;
  132.     /**
  133.      * @var string|null
  134.      *
  135.      * @ORM\Column(name="page_color_text", type="string", length=20, nullable=true)
  136.      */
  137.     private $pageColorText;
  138.     /**
  139.      * @var string|null
  140.      *
  141.      * @ORM\Column(name="description", type="text", length=65535, nullable=true)
  142.      */
  143.     private $description;
  144.     /**
  145.      * @var \Doctrine\Common\Collections\Collection
  146.      *
  147.      * @ORM\ManyToMany(targetEntity="Product", mappedBy="category")
  148.      */
  149.     private $product;
  150.     /**
  151.      * @Assert\Valid
  152.      *
  153.      * @var \EADPlataforma\Entity\User
  154.      *
  155.      * @ORM\ManyToOne(targetEntity="User")
  156.      * @ORM\JoinColumns({
  157.      *   @ORM\JoinColumn(name="user_delete_id", referencedColumnName="id", nullable=true)
  158.      * })
  159.      */
  160.     private $userDelete;
  161.     /**
  162.      * @Assert\Choice(
  163.      *      choices = { 
  164.      *                      CategoryEnum::INDIVIDUAL, 
  165.      *                      CategoryEnum::CASCADE
  166.      *                },
  167.      *      message = "Type Delete Invalid"
  168.      * )
  169.      *
  170.      * @var int
  171.      *
  172.      * @ORM\Column(name="type_delete", type="integer", nullable=true)
  173.      */
  174.     private $typeDelete;
  175.     /**
  176.      * @EADAssert\DateTimeEAD(
  177.      *      message = "Date Delete Invalid"
  178.      * )
  179.      *
  180.      * @var \DateTime|null
  181.      *
  182.      * @ORM\Column(name="date_delete", type="datetime", nullable=true)
  183.      */
  184.     private $dateDelete;
  185.     /**
  186.      * Constructor
  187.      */
  188.     public function __construct()
  189.     {
  190.         $this->product = new \Doctrine\Common\Collections\ArrayCollection();
  191.     }
  192.     public function getId(): ?int
  193.     {
  194.         return $this->id;
  195.     }
  196.     public function getStatus(): ?int
  197.     {
  198.         return $this->status;
  199.     }
  200.     public function setStatus(int $status): self
  201.     {
  202.         $this->status $status;
  203.         return $this;
  204.     }
  205.     public function getOrder(): ?int
  206.     {
  207.         return $this->order;
  208.     }
  209.     public function setOrder(int $order): self
  210.     {
  211.         $this->order $order;
  212.         return $this;
  213.     }
  214.     public function getCategory(): ?string
  215.     {
  216.         return StringUtil::fromUnicode(StringUtil::encodeStringStatic($this->category));
  217.     }
  218.     public function setCategory(string $category): self
  219.     {
  220.         $this->category StringUtil::toUnicode($category);
  221.         return $this;
  222.     }
  223.     public function getSlug(): ?string
  224.     {
  225.         return $this->slug;
  226.     }
  227.     public function setSlug(string $slug): self
  228.     {
  229.         $this->slug StringUtil::slugStatic($slug);
  230.         return $this;
  231.     }
  232.     public function getImage(): ?string
  233.     {
  234.         return $this->image;
  235.     }
  236.     public function setImage(?string $image): self
  237.     {
  238.         $this->image $image;
  239.         return $this;
  240.     }
  241.     public function getIcon(): ?string
  242.     {
  243.         return $this->icon;
  244.     }
  245.     public function setIcon(?string $icon): self
  246.     {
  247.         $this->icon $icon;
  248.         return $this;
  249.     }
  250.     public function getPageColorText(): ?string
  251.     {
  252.         return $this->pageColorText;
  253.     }
  254.     public function setPageColorText(?string $pageColorText): self
  255.     {
  256.         $this->pageColorText $pageColorText;
  257.         return $this;
  258.     }
  259.     public function getDescription(): ?string
  260.     {
  261.         return StringUtil::encodeStringStatic($this->description);
  262.     }
  263.     public function setDescription(string $description): self
  264.     {
  265.         $this->description $description;
  266.         return $this;
  267.     }
  268.     public function getUserDelete(): ?User
  269.     {
  270.         return $this->userDelete;
  271.     }
  272.     public function setUserDelete(?User $userDelete): self
  273.     {
  274.         $this->userDelete $userDelete;
  275.         return $this;
  276.     }
  277.     public function getDateDelete($dateFormat 'Y-m-d H:i:s')
  278.     {
  279.         if($this->dateDelete){
  280.             return $this->dateDelete->format($dateFormat);
  281.         }
  282.         return $this->dateDelete;
  283.     }
  284.     public function setDateDelete($dateDelete): self
  285.     {
  286.         if(!empty($dateDelete)){
  287.             $dateDelete DateTime::createFromFormat('Y-m-d H:i:s'$dateDelete);
  288.         }
  289.         
  290.         $this->dateDelete $dateDelete;
  291.         return $this;
  292.     }
  293.     /**
  294.      * @return Collection|Product[]
  295.      */
  296.     public function getProduct(): Collection
  297.     {
  298.         return $this->product;
  299.     }
  300.     public function hasProduct(): bool
  301.     {
  302.         return (count($this->product) > 0);
  303.     }
  304.     public function addProduct(Product $product): self
  305.     {
  306.         if (!$this->product->contains($product)) {
  307.             $this->product[] = $product;
  308.             $product->addCategory($this);
  309.         }
  310.         return $this;
  311.     }
  312.     public function removeProduct(Product $product): self
  313.     {
  314.         if ($this->product->contains($product)) {
  315.             $this->product->removeElement($product);
  316.             $product->removeCategory($this);
  317.         }
  318.         return $this;
  319.     }
  320.     public function individual(): self
  321.     {
  322.         $this->typeDelete CategoryEnum::INDIVIDUAL;
  323.         return $this;
  324.     }
  325.     public function cascade(): self
  326.     {
  327.         $this->typeDelete CategoryEnum::CASCADE;
  328.         return $this;
  329.     }
  330.     public function isOnTrash(): bool
  331.     {
  332.         return ($this->deleted == CategoryEnum::ITEM_ON_TRASH);
  333.     }
  334.     public function isDeleted(): bool
  335.     {
  336.         return ($this->deleted == CategoryEnum::ITEM_DELETED);
  337.     }
  338.     public function restore(): self
  339.     {
  340.         $this->deleted CategoryEnum::ITEM_NO_DELETED;
  341.         return $this;
  342.     }
  343.     public function trash(): self
  344.     {
  345.         $this->deleted CategoryEnum::ITEM_ON_TRASH;
  346.         return $this;
  347.     }
  348.     public function delete(): self
  349.     {
  350.         $this->deleted CategoryEnum::ITEM_DELETED;
  351.         return $this;
  352.     }
  353.     public function toReturn(?bool $addProduct true){
  354.         $data = [
  355.             "id" => $this->id,
  356.             "deleted" => $this->deleted,
  357.             "status" => $this->status,
  358.             "order" => $this->order,
  359.             "category" => $this->getCategory(),
  360.             "slug" => $this->slug,
  361.             "image" => FileService::getFilePathComplete(
  362.                 $this->image
  363.                 CategoryEnum::PATH_OTHERS
  364.                 true
  365.                 true
  366.             ),
  367.             "icon" => FileService::getFilePathComplete(
  368.                 $this->icon
  369.                 CategoryEnum::PATH_OTHERS
  370.                 true
  371.                 true
  372.             ),
  373.             "pageColorText" => $this->pageColorText,
  374.             "description" => $this->getDescription(),
  375.             "userDelete" => ( $this->userDelete $this->userDelete->getId() : null ),
  376.             "typeDelete" => $this->typeDelete,
  377.             "dateDelete" => $this->getDateDelete()
  378.         ];
  379.         if($addProduct){
  380.             $arrProduct = [];
  381.             foreach ($this->product as $key => $product) {
  382.                 $arrProduct[] = (object)[
  383.                     "id" => $product->getId(),
  384.                     "title" => $product->getTitle(),
  385.                 ];
  386.             }
  387.         
  388.             $data["product"] = $arrProduct;
  389.         }
  390.         return $data;
  391.     }
  392.     public function toReturnApi(){
  393.         $data = [
  394.             "id" => $this->id,
  395.             "status" => $this->status,
  396.             "ordem" => $this->order,
  397.             "categoria" => $this->category,
  398.             "slug" => $this->slug,
  399.             "descricao" => $this->description,
  400.             "imagem" => FileService::getFilePathComplete(
  401.                 $this->image
  402.                 CategoryEnum::PATH_OTHERS
  403.                 true
  404.                 true
  405.             ),
  406.             "icone" => FileService::getFilePathComplete(
  407.                 $this->icon
  408.                 CategoryEnum::PATH_OTHERS
  409.                 true
  410.                 true
  411.             ),
  412.             "corTexto" => $this->pageColorText
  413.         ];
  414.         return $data;
  415.     }
  416. }