<?php
declare(strict_types=1);
namespace App\Entity\Product;
use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\ReferenceableInterface;
use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\ReferenceableTrait;
use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\RichSnippetProductSubjectTrait;
use Dedi\SyliusSEOPlugin\Domain\SEO\Adapter\RichSnippetSubjectInterface;
use Dedi\SyliusSEOPlugin\Entity\SEOContent;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\PersistentCollection;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Model\Product as BaseProduct;
use Sylius\Component\Product\Model\ProductTranslationInterface;
use Mezcalito\SyliusFileUploadPlugin\Model\FilesAwareInterface;
use Mezcalito\SyliusFileUploadPlugin\Model\FilesAwareTrait;
/**
* @ORM\Entity
* @ORM\Table(name="sylius_product")
*/
class Product extends BaseProduct implements FilesAwareInterface, ReferenceableInterface, RichSnippetSubjectInterface
{
use ReferenceableTrait {
getMetadataTitle as getBaseMetadataTitle;
getMetadataDescription as getBaseMetadataDescription;
}
use RichSnippetProductSubjectTrait;
/**
* @ORM\OneToMany(targetEntity="ProductFile", mappedBy="owner", orphanRemoval="true", cascade={"all"})
*/
protected $files;
// /**
// * @ORM\OrderBy({"position"="ASC"})
// */
// protected $images;
/**
* @var Collection|ImageInterface[]
* @ORM\OrderBy({"position" = "ASC"})
* @psalm-var Collection<array-key, ImageInterface>
*/
protected $images;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
protected ?bool $buildingPermitNotRequired;
use FilesAwareTrait {
__construct as private initializeFilesCollection;
}
public function __construct()
{
parent::__construct();
$this->files = new ArrayCollection();
$this->addFile(new ProductFile());
$this->images = new ArrayCollection();
}
/**
* {@inheritDoc}
*/
public function getFiles(): Collection
{
if (count($this->files) == 0) {
$this->addFile(new ProductFile());
}
return $this->files;
}
public function getImages(): Collection
{
/** @var PersistentCollection $collection */
$collection = $this->images;
$values = $collection->getValues();
uasort($values, function($a, $b) {
return $a->getPosition() < $b->getPosition() ? -1 : 1;
});
$collection->clear();
foreach ($values as $key => $value) {
$collection->set($key, $value);
}
return $collection;
}
public function getBuildingPermitNotRequired(): ?bool
{
return $this->buildingPermitNotRequired;
}
public function setBuildingPermitNotRequired(?bool $buildingPermitNotRequired): void
{
$this->buildingPermitNotRequired = $buildingPermitNotRequired;
}
public function getChannels(): Collection
{
return parent::getChannels(); // TODO: Change the autogenerated stub
}
public function getRichSnippetSubjectParent(): ?RichSnippetSubjectInterface
{
return $this->getMainTaxon();
}
public function getRichSnippetSubjectType(): string
{
return 'product';
}
public function getMetadataTitle(): ?string
{
if (is_null($this->getReferenceableContent()->getMetadataTitle())) {
return $this->getName();
}
return $this->getBaseMetadataTitle();
}
public function getMetadataDescription(): ?string
{
if (is_null($this->getReferenceableContent()->getMetadataDescription())) {
return $this->getShortDescription();
}
return $this->getBaseMetadataDescription();
}
protected function createTranslation(): ProductTranslationInterface
{
return new ProductTranslation();
}
protected function createReferenceableContent(): ReferenceableInterface
{
return new SEOContent();
}
}