<?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 Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Contracts\Translation\TranslatorInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
/**
* @Doctrine\ORM\Mapping\Entity
* @Doctrine\ORM\Mapping\Table(name="product_parameter_value_group")
*/
class ProductParameterValueGroup implements TranslatableInterface, BlameableInterface, TimestampableInterface, SoftDeletableInterface {
use BlameableTrait;
use TimestampableTrait;
use TranslatableTrait;
use SoftDeletableTrait;
/**
* @Doctrine\ORM\Mapping\Id
* @Doctrine\ORM\Mapping\Column(type="integer")
* @Doctrine\ORM\Mapping\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Doctrine\ORM\Mapping\OneToMany(targetEntity="App\Entity\ProductParameterValue", mappedBy="group", cascade={"all"})
* @Doctrine\ORM\Mapping\OrderBy({"id" = "DESC"})
*/
protected $values;
/**
* @Doctrine\ORM\Mapping\ManyToOne(targetEntity="App\Entity\ProductParameter", inversedBy="groupValues", fetch="EAGER")
*/
private $parameter;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=128, nullable=true)
*/
private $path;
/**
* @var boolean
*
* @Doctrine\ORM\Mapping\Column(name="cart", type="boolean", nullable=true)
*/
private $cart;
/**
* @var boolean
*
* @Doctrine\ORM\Mapping\Column(name="visible", type="boolean", nullable=true)
*/
private $visible;
/**
* Obsługa tłumaczeń
* @param $method
* @param $arguments
* @return mixed
*/
public function getValuesByParameter($parameterId) {
$values = new ArrayCollection();
foreach ($this->getVisibleValues() as $value) {
if ($value->getParameter()->getId() == $parameterId) {
$values->add($value);
}
}
return $values;
}
public function customTrans($params, $idCompare) {
return array_filter($params, function($e) use ($idCompare) {
return (isset($e['id'])) && $e['id'] == $idCompare;
});
}
public function __call($method, $arguments)
{
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
public function getName()
{
return $this->translate()->getName();
}
public function getDescription() {
return $this->translate()->getDescription();
}
public function __construct()
{
$this->values = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getVisibleValues() {
$c = new ArrayCollection();
/** @var $value ProductParameterValue */
foreach ($this->getValues() as $value) {
if ($value->getVisible()) {
$c->add($value);
}
}
return $c;
}
/**
* @return Collection|ProductParameterValue[]
*/
public function getValues(): Collection
{
return $this->values;
}
public function addValue(ProductParameterValue $value): self
{
if (!$this->values->contains($value)) {
$this->values[] = $value;
$value->setGroup($this);
}
return $this;
}
public function removeValue(ProductParameterValue $value): self
{
if ($this->values->contains($value)) {
$this->values->removeElement($value);
// set the owning side to null (unless already changed)
if ($value->getGroup() === $this) {
$value->setGroup(null);
}
}
return $this;
}
public function getParameter(): ?ProductParameter
{
return $this->parameter;
}
public function setParameter(?ProductParameter $parameter): self
{
$this->parameter = $parameter;
return $this;
}
/**
*
* note This is not a mapped field of entity metadata, just a simple property.
*
* @var File $imageFile
*/
protected $imageFile;
/**
* @ORM\Column(type="string", length=255, name="image_name", nullable=true)
* @var string $imageName
*/
protected $imageName;
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
$fileName = md5(uniqid()).'.'.$image->guessExtension();
// Move the file to the directory where brochures are stored
$brochuresDir = getcwd().'/images/parameter_groups/';
$image->move($brochuresDir, $fileName);
// Update the 'brochure' property to store the PDF file name
// instead of its contents
$this->setImageName($fileName);
$this->updatedAt = new \DateTime('now');
}
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;
}
public function getCart(): ?bool
{
return $this->cart;
}
public function setCart(?bool $cart): self
{
$this->cart = $cart;
return $this;
}
public function getVisible(): ?bool
{
return $this->visible;
}
public function setVisible(?bool $visible): self
{
$this->visible = $visible;
return $this;
}
public function getPath(): ?string
{
return $this->path;
}
public function setPath(?string $path): self
{
$this->path = $path;
return $this;
}
}