src/Entity/ProductComment.php line 32

Open in your IDE?
  1. <?php
  2. // src/Acme/UserBundle/Entity/User.php
  3. namespace App\Entity;
  4. use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
  5. use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
  6. use Knp\DoctrineBehaviors\Model\Blameable\BlameableTrait;
  7. use Knp\DoctrineBehaviors\Contract\Entity\BlameableInterface;
  8. use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
  9. use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Symfony\Component\HttpFoundation\File\File;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  16. /**
  17.  * @Doctrine\ORM\Mapping\Entity
  18.  * @Doctrine\ORM\Mapping\Table(name="product_comment")
  19.  * @Doctrine\ORM\Mapping\Entity()
  20.  */
  21. class ProductComment implements BlameableInterfaceTimestampableInterfaceSoftDeletableInterface  {
  22.     use BlameableTrait;
  23.     use TimestampableTrait;
  24.     
  25.     use SoftDeletableTrait;
  26.     
  27. /**
  28.      * @var integer
  29.      *
  30.      * @ORM\Column(name="id", type="integer", nullable=false)
  31.      * @ORM\Id
  32.      * @ORM\GeneratedValue(strategy="IDENTITY")
  33.      */
  34.     private $id;
  35.     /**
  36.      * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="comments")
  37.      */
  38.     private $user;
  39.     /**
  40.      * @Doctrine\ORM\Mapping\ManyToMany(targetEntity="App\Entity\Product", inversedBy="comments")
  41.      * @Doctrine\ORM\Mapping\JoinTable(name="product_comment_associated_products")
  42.      */
  43.     private $products;
  44.     /**
  45.      * @ORM\ManyToOne(targetEntity="App\Entity\Order")
  46.      */
  47.     private $order;
  48.     /**
  49.      * @var bool
  50.      *
  51.      * @ORM\Column(name="visible", type="boolean", nullable=true)
  52.      */
  53.     private $visible;
  54.     /**
  55.      * @var bool
  56.      *
  57.      * @ORM\Column(name="viewed", type="boolean", nullable=true)
  58.      */
  59.     private $viewed;
  60.     /**
  61.      * @var string
  62.      *
  63.      * @ORM\Column(name="content", type="text", nullable=true)
  64.      */
  65.     private $content;
  66.     /**
  67.      * @var string
  68.      *
  69.      * @ORM\Column(name="opinion_rate", type="float", nullable=true)
  70.      */
  71.     private $opinionRate;
  72.     /**
  73.      * @var string
  74.      *
  75.      * @ORM\Column(name="code", type="string", length=255, nullable=true)
  76.      */
  77.     private $code;
  78.     /**
  79.      * @ORM\ManyToOne(targetEntity="App\Entity\Language")
  80.      * @ORM\JoinColumn(name="language", referencedColumnName="id")
  81.      */
  82.     protected $language;
  83.     protected $imageFile;
  84.     /**
  85.      * @ORM\Column(type="string", length=255, name="image_name", nullable=true)
  86.      * @var string $imageName
  87.      */
  88.     protected $imageName;
  89.     public function setImageFile(File $image null): self
  90.     {
  91.         $this->imageFile $image;
  92.         if ($image === null) {
  93.             return $this;
  94.         }
  95.         // --- validate -----------------------------------------------------------
  96.         if (!@is_array(getimagesize($image->getRealPath()))) {
  97.             throw new \RuntimeException('File is not a valid image.');
  98.         }
  99.         // --- build name parts ---------------------------------------------------
  100.         /** @var \App\Entity\Product|null $product */
  101.         $product   = ($this->getProducts()->count()) ? $this->getProducts()->first() : 'product';              // take the first product
  102.         $prodSlug  $this->slugify($product $product->getName() : 'product');
  103.         $userSlug  $this->slugify($this->getUser() ? $this->getUser()->getFirstname() : 'user');
  104.         $prodId    $product && $product->getId() ? $product->getId() : rand(0,1000);
  105.         $userId    $this->getUser()  && $this->getUser()->getId()   ? $this->getUser()->getId()   : 0;
  106.         $extension $image->guessExtension();
  107.         // e.g. galaxywatch6-johndoe-53-18.jpg
  108.         $fileName sprintf('%s-%s-%d-%d.%s'$prodSlug$userSlug$prodId$userId$extension);
  109.         // --- move ----------------------------------------------------------------
  110.         $uploadDir getcwd().'/images/important/';
  111.         $image->move($uploadDir$fileName);
  112.         $this->imageName $fileName;
  113.         $this->updatedAt = new \DateTimeImmutable();
  114.         return $this;
  115.     }
  116.     /**
  117.      * @return File
  118.      */
  119.     public function getImageFile()
  120.     {
  121.         return $this->imageFile;
  122.     }
  123.     /**
  124.      * @param string $imageName
  125.      */
  126.     public function setImageName($imageName)
  127.     {
  128.         $this->imageName $imageName;
  129.     }
  130.     /**
  131.      * @return string
  132.      */
  133.     public function getImageName()
  134.     {
  135.         return $this->imageName;
  136.     }
  137.     /**
  138.      * Constructor
  139.      */
  140.     public function __construct()
  141.     {
  142.         $this->products = new \Doctrine\Common\Collections\ArrayCollection();
  143.     }
  144.     /**
  145.      * Get id
  146.      *
  147.      * @return integer
  148.      */
  149.     public function getId()
  150.     {
  151.         return $this->id;
  152.     }
  153.     /**
  154.      * Set visible
  155.      *
  156.      * @param boolean $visible
  157.      *
  158.      * @return \App\Entity\ProductComment
  159.      */
  160.     public function setVisible($visible)
  161.     {
  162.         $this->visible $visible;
  163.         return $this;
  164.     }
  165.     /**
  166.      * Get visible
  167.      *
  168.      * @return boolean
  169.      */
  170.     public function getVisible()
  171.     {
  172.         return $this->visible;
  173.     }
  174.     /**
  175.      * Set content
  176.      *
  177.      * @param string $content
  178.      *
  179.      * @return \App\Entity\ProductComment
  180.      */
  181.     public function setContent($content)
  182.     {
  183.         $this->content $content;
  184.         return $this;
  185.     }
  186.     /**
  187.      * Get content
  188.      *
  189.      * @return string
  190.      */
  191.     public function getContent()
  192.     {
  193.         return $this->content;
  194.     }
  195.     /**
  196.      * Set opinionRate
  197.      *
  198.      * @param integer $opinionRate
  199.      *
  200.      * @return \App\Entity\ProductComment
  201.      */
  202.     public function setOpinionRate($opinionRate)
  203.     {
  204.         $this->opinionRate $opinionRate;
  205.         return $this;
  206.     }
  207.     /**
  208.      * Get opinionRate
  209.      *
  210.      * @return integer
  211.      */
  212.     public function getOpinionRate()
  213.     {
  214.         return $this->opinionRate;
  215.     }
  216.     /**
  217.      * Set code
  218.      *
  219.      * @param string $code
  220.      *
  221.      * @return \App\Entity\ProductComment
  222.      */
  223.     public function setCode($code)
  224.     {
  225.         $this->code $code;
  226.         return $this;
  227.     }
  228.     /**
  229.      * Get code
  230.      *
  231.      * @return string
  232.      */
  233.     public function getCode()
  234.     {
  235.         return $this->code;
  236.     }
  237.     /**
  238.      * Set user
  239.      *
  240.      * @param \App\Entity\User $user
  241.      *
  242.      * @return \App\Entity\ProductComment
  243.      */
  244.     public function setUser(\App\Entity\User $user null)
  245.     {
  246.         $this->user $user;
  247.         return $this;
  248.     }
  249.     /**
  250.      * Get user
  251.      *
  252.      * @return \App\Entity\User
  253.      */
  254.     public function getUser()
  255.     {
  256.         return $this->user;
  257.     }
  258.     /**
  259.      * Add product
  260.      *
  261.      * @param \App\Entity\Product $product
  262.      *
  263.      * @return \App\Entity\ProductComment
  264.      */
  265.     public function addProduct(\App\Entity\Product $product)
  266.     {
  267.         $this->products[] = $product;
  268.         return $this;
  269.     }
  270.     /**
  271.      * Remove product
  272.      *
  273.      * @param \App\Entity\Product $product
  274.      */
  275.     public function removeProduct(\App\Entity\Product $product)
  276.     {
  277.         $this->products->removeElement($product);
  278.     }
  279.     /**
  280.      * Get products
  281.      *
  282.      * @return \Doctrine\Common\Collections\Collection
  283.      */
  284.     public function getProducts()
  285.     {
  286.         return $this->products;
  287.     }
  288.     /**
  289.      * Set order
  290.      *
  291.      * @param \App\Entity\Order $order
  292.      *
  293.      * @return \App\Entity\ProductComment
  294.      */
  295.     public function setOrder(\App\Entity\Order $order null)
  296.     {
  297.         $this->order $order;
  298.         return $this;
  299.     }
  300.     /**
  301.      * Get order
  302.      *
  303.      * @return \App\Entity\Order
  304.      */
  305.     public function getOrder()
  306.     {
  307.         return $this->order;
  308.     }
  309.     /**
  310.      * Set language
  311.      *
  312.      * @param \App\Entity\Language $language
  313.      *
  314.      * @return \App\Entity\ProductComment
  315.      */
  316.     public function setLanguage(\App\Entity\Language $language null)
  317.     {
  318.         $this->language $language;
  319.         return $this;
  320.     }
  321.     /**
  322.      * Get language
  323.      *
  324.      * @return \App\Entity\Language
  325.      */
  326.     public function getLanguage()
  327.     {
  328.         return $this->language;
  329.     }
  330.     /**
  331.      * @return bool
  332.      */
  333.     public function isViewed()
  334.     {
  335.         return $this->viewed;
  336.     }
  337.     /**
  338.      * @param bool $viewed
  339.      */
  340.     public function setViewed($viewed)
  341.     {
  342.         $this->viewed $viewed;
  343.     }
  344.     public function getViewed(): ?bool
  345.     {
  346.         return $this->viewed;
  347.     }
  348.     private function slugify(string $value): string
  349.     {
  350.         // 1) Strip accents / diacritics → ASCII
  351.         //    iconv may return FALSE on some characters, so cast to string.
  352.         $value str_replace("ł""l"$value);
  353.         $value str_replace("ó""o"$value);
  354.         $value str_replace("ć""c"$value);
  355.         $value str_replace("ą""a"$value);
  356.         $value str_replace("ś""s"$value);
  357.         $ascii iconv('UTF-8''ASCII//TRANSLIT//IGNORE'$value);
  358.         $ascii strtolower((string) $ascii);
  359.         // 2) Remove everything except a–z and 0–9
  360.         $ascii preg_replace('/[^a-z0-9]/'''$ascii);
  361.         // 3) Fallback if the string became empty
  362.         return $ascii ?: 'file'.rand(0,1000);
  363.     }
  364. }