<?php
// src/Acme/UserBundle/Entity/User.php
namespace App\Entity;
use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
use Knp\DoctrineBehaviors\Model\Blameable\BlameableTrait;
use Knp\DoctrineBehaviors\Contract\Entity\BlameableInterface;
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @Doctrine\ORM\Mapping\Entity
* @Doctrine\ORM\Mapping\Table(name="product_comment")
* @Doctrine\ORM\Mapping\Entity()
*/
class ProductComment implements BlameableInterface, TimestampableInterface, SoftDeletableInterface {
use BlameableTrait;
use TimestampableTrait;
use SoftDeletableTrait;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="comments")
*/
private $user;
/**
* @Doctrine\ORM\Mapping\ManyToMany(targetEntity="App\Entity\Product", inversedBy="comments")
* @Doctrine\ORM\Mapping\JoinTable(name="product_comment_associated_products")
*/
private $products;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Order")
*/
private $order;
/**
* @var bool
*
* @ORM\Column(name="visible", type="boolean", nullable=true)
*/
private $visible;
/**
* @var bool
*
* @ORM\Column(name="viewed", type="boolean", nullable=true)
*/
private $viewed;
/**
* @var string
*
* @ORM\Column(name="content", type="text", nullable=true)
*/
private $content;
/**
* @var string
*
* @ORM\Column(name="opinion_rate", type="float", nullable=true)
*/
private $opinionRate;
/**
* @var string
*
* @ORM\Column(name="code", type="string", length=255, nullable=true)
*/
private $code;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Language")
* @ORM\JoinColumn(name="language", referencedColumnName="id")
*/
protected $language;
protected $imageFile;
/**
* @ORM\Column(type="string", length=255, name="image_name", nullable=true)
* @var string $imageName
*/
protected $imageName;
public function setImageFile(File $image = null): self
{
$this->imageFile = $image;
if ($image === null) {
return $this;
}
// --- validate -----------------------------------------------------------
if (!@is_array(getimagesize($image->getRealPath()))) {
throw new \RuntimeException('File is not a valid image.');
}
// --- build name parts ---------------------------------------------------
/** @var \App\Entity\Product|null $product */
$product = ($this->getProducts()->count()) ? $this->getProducts()->first() : 'product'; // take the first product
$prodSlug = $this->slugify($product ? $product->getName() : 'product');
$userSlug = $this->slugify($this->getUser() ? $this->getUser()->getFirstname() : 'user');
$prodId = $product && $product->getId() ? $product->getId() : rand(0,1000);
$userId = $this->getUser() && $this->getUser()->getId() ? $this->getUser()->getId() : 0;
$extension = $image->guessExtension();
// e.g. galaxywatch6-johndoe-53-18.jpg
$fileName = sprintf('%s-%s-%d-%d.%s', $prodSlug, $userSlug, $prodId, $userId, $extension);
// --- move ----------------------------------------------------------------
$uploadDir = getcwd().'/images/important/';
$image->move($uploadDir, $fileName);
$this->imageName = $fileName;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param string $imageName
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
}
/**
* @return string
*/
public function getImageName()
{
return $this->imageName;
}
/**
* Constructor
*/
public function __construct()
{
$this->products = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set visible
*
* @param boolean $visible
*
* @return \App\Entity\ProductComment
*/
public function setVisible($visible)
{
$this->visible = $visible;
return $this;
}
/**
* Get visible
*
* @return boolean
*/
public function getVisible()
{
return $this->visible;
}
/**
* Set content
*
* @param string $content
*
* @return \App\Entity\ProductComment
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set opinionRate
*
* @param integer $opinionRate
*
* @return \App\Entity\ProductComment
*/
public function setOpinionRate($opinionRate)
{
$this->opinionRate = $opinionRate;
return $this;
}
/**
* Get opinionRate
*
* @return integer
*/
public function getOpinionRate()
{
return $this->opinionRate;
}
/**
* Set code
*
* @param string $code
*
* @return \App\Entity\ProductComment
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* Set user
*
* @param \App\Entity\User $user
*
* @return \App\Entity\ProductComment
*/
public function setUser(\App\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \App\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Add product
*
* @param \App\Entity\Product $product
*
* @return \App\Entity\ProductComment
*/
public function addProduct(\App\Entity\Product $product)
{
$this->products[] = $product;
return $this;
}
/**
* Remove product
*
* @param \App\Entity\Product $product
*/
public function removeProduct(\App\Entity\Product $product)
{
$this->products->removeElement($product);
}
/**
* Get products
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
/**
* Set order
*
* @param \App\Entity\Order $order
*
* @return \App\Entity\ProductComment
*/
public function setOrder(\App\Entity\Order $order = null)
{
$this->order = $order;
return $this;
}
/**
* Get order
*
* @return \App\Entity\Order
*/
public function getOrder()
{
return $this->order;
}
/**
* Set language
*
* @param \App\Entity\Language $language
*
* @return \App\Entity\ProductComment
*/
public function setLanguage(\App\Entity\Language $language = null)
{
$this->language = $language;
return $this;
}
/**
* Get language
*
* @return \App\Entity\Language
*/
public function getLanguage()
{
return $this->language;
}
/**
* @return bool
*/
public function isViewed()
{
return $this->viewed;
}
/**
* @param bool $viewed
*/
public function setViewed($viewed)
{
$this->viewed = $viewed;
}
public function getViewed(): ?bool
{
return $this->viewed;
}
private function slugify(string $value): string
{
// 1) Strip accents / diacritics → ASCII
// iconv may return FALSE on some characters, so cast to string.
$value = str_replace("ł", "l", $value);
$value = str_replace("ó", "o", $value);
$value = str_replace("ć", "c", $value);
$value = str_replace("ą", "a", $value);
$value = str_replace("ś", "s", $value);
$ascii = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
$ascii = strtolower((string) $ascii);
// 2) Remove everything except a–z and 0–9
$ascii = preg_replace('/[^a-z0-9]/', '', $ascii);
// 3) Fallback if the string became empty
return $ascii ?: 'file'.rand(0,1000);
}
}