<?php
namespace App\Entity;
use App\Validator\MinimumPropertiesFilled;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\SearchAlertRepository")
*
* @ORM\Table(name="search_alert")
*
* @MinimumPropertiesFilled(min=3, excludeTerm="Label")
*/
class SearchAlert
{
public const TERMS_SEPARATOR_DISPLAY = ', ';
public const TERMS_SEPARATOR = ',';
/** @var string|null */
public $formattedName;
/**
* @ORM\Id
*
* @ORM\Column(type="bigint")
*
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="selections")
*
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
*/
protected $user;
/**
* @ORM\Column(type="array", nullable=true)
*/
protected $maker = [];
/**
* Store ids of MODNatCode column in order to bind to MODName,
* see model table in eurotax database.
*
* @ORM\Column(type="array", nullable=true)
*/
protected $modelGroup = [];
/**
* @ORM\Column(type="array", nullable=true)
*/
protected $segmentSize = [];
/**
* @ORM\Column(type="array", nullable=true)
*/
protected $energy;
/**
* @ORM\Column(type="array", nullable=true)
*/
protected $segmentTransmission;
/**
* @ORM\Column(type="array", nullable=true)
*/
protected $segmentTransmissionLabel;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @Assert\Regex(
* pattern="/[a-z]+/i",
* match=false,
* message="search_alert.constraint.kilometers.integer.min"
* )
*/
protected $kilometersMin;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @Assert\Regex(
* pattern="/[a-z]+/i",
* match=false,
* message="search_alert.constraint.kilometers.integer.max"
* )
*/
protected $kilometersMax;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @Assert\Regex(
* pattern="/[a-z]+/i",
* match=false,
* message="search_alert.constraint.millesime.integer.min"
* )
*/
protected $millesimeMin;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @Assert\Regex(
* pattern="/[a-z]+/i",
* match=false,
* message="search_alert.constraint.millesime.integer.max"
* )
*/
protected $millesimeMax;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @Assert\Regex(
* pattern="/[a-z]+/i",
* match=false,
* message="search_alert.constraint.price.integer.min"
* )
*/
protected $priceMin;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @Assert\Regex(
* pattern="/[a-z]+/i",
* match=false,
* message="search_alert.constraint.price.integer.max"
* )
*/
protected $priceMax;
/**
* @ORM\Column(type="array", nullable=true)
*/
protected $options = [];
/**
* @Gedmo\Timestampable(on="create")
*
* @ORM\Column(type="datetime", name="created_at")
*/
protected $createdAt;
/**
* @Gedmo\Timestampable(on="update")
*
* @ORM\Column(type="datetime", name="updated_at")
*/
protected $updatedAt;
/**
* @ORM\Column(type="array", nullable=true)
*/
protected $seatCount = [];
/**
* @ORM\Column(type="array", nullable=true)
*/
protected $co2 = [];
/**
* @ORM\Column(type="array", nullable=true)
*/
protected $euroEmissionStandard = [];
/**
* @ORM\Column(type="boolean", nullable=true)
*/
protected $vat;
/**
* @param ArrayCollection $category
*/
public function __construct(User $user, /**
* @ORM\ManyToMany(targetEntity="App\Entity\Category")
*/
protected $category = [])
{
$this->user = $user;
}
/**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context): void
{
foreach (['kilometers', 'millesime', 'price'] as $value) {
if ((intval($this->{$value.'Min'}) > 0 && intval($this->{$value.'Max'}) > 0) && (intval($this->{$value.'Min'}) > intval($this->{$value.'Max'}))) {
$context
->buildViolation(sprintf('search_alert.constraint.%s.range_error', $value))
->atPath(sprintf('%sMin', $value))
->addViolation();
}
}
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set user.
*
* @return VehicleAlert
*/
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user.
*
* @return User
*/
public function getUser()
{
return $this->user;
}
public function setCategory($category): void
{
$this->category = $category;
}
public function getCategory()
{
return $this->category;
}
public function setMaker($maker): void
{
$this->maker = $maker;
}
public function getMaker()
{
return $this->maker;
}
public function setModelGroup($modelGroup): void
{
$this->modelGroup = $modelGroup;
}
public function getModelGroup()
{
return $this->modelGroup;
}
public function setSegmentSize($segmentSize): void
{
$this->segmentSize = $segmentSize;
}
public function getSegmentSize()
{
return $this->segmentSize;
}
public function setEnergy($energy): void
{
$this->energy = $energy;
}
public function getEnergy()
{
return $this->energy;
}
public function setSegmentTransmission($segmentTransmission): void
{
$this->segmentTransmission = $segmentTransmission;
}
public function getSegmentTransmission()
{
return $this->segmentTransmission;
}
public function setSegmentTransmissionLabel($segmentTransmissionLabel)
{
if (null !== $segmentTransmissionLabel && !is_array($segmentTransmissionLabel)) {
$segmentTransmissionLabel = explode(',', (string) $segmentTransmissionLabel);
}
$this->segmentTransmissionLabel = $segmentTransmissionLabel;
return $this;
}
/**
* Get segmentTransmissionLabel.
*
* @return string
*/
public function getSegmentTransmissionLabel()
{
if (!is_array($this->segmentTransmissionLabel)) {
return '';
}
return implode(',', $this->segmentTransmissionLabel);
}
/**
* Set kilometersMin.
*
* @param int $kilometersMin
*
* @return VehicleAlert
*/
public function setKilometersMin($kilometersMin)
{
$this->kilometersMin = $kilometersMin;
return $this;
}
/**
* Get kilometersMin.
*
* @return int
*/
public function getKilometersMin()
{
return $this->kilometersMin;
}
/**
* Set kilometersMax.
*
* @param int $kilometersMax
*
* @return VehicleAlert
*/
public function setKilometersMax($kilometersMax)
{
$this->kilometersMax = $kilometersMax;
return $this;
}
/**
* Get kilometersMax.
*
* @return int
*/
public function getKilometersMax()
{
return $this->kilometersMax;
}
/**
* Set millesimeMin.
*
* @param int $millesimeMin
*
* @return VehicleAlert
*/
public function setMillesimeMin($millesimeMin)
{
$this->millesimeMin = $millesimeMin;
return $this;
}
/**
* Get millesimeMin.
*
* @return int
*/
public function getMillesimeMin()
{
return $this->millesimeMin;
}
/**
* Set millesimeMax.
*
* @param int $millesimeMax
*
* @return VehicleAlert
*/
public function setMillesimeMax($millesimeMax)
{
$this->millesimeMax = $millesimeMax;
return $this;
}
/**
* Get millesimeMax.
*
* @return int
*/
public function getMillesimeMax()
{
return $this->millesimeMax;
}
/**
* Set priceMin.
*
* @param int $priceMin
*
* @return VehicleAlert
*/
public function setPriceMin($priceMin)
{
$this->priceMin = $priceMin;
return $this;
}
/**
* Get priceMin.
*
* @return int
*/
public function getPriceMin()
{
return $this->priceMin;
}
/**
* Set priceMax.
*
* @param int $priceMax
*
* @return VehicleAlert
*/
public function setPriceMax($priceMax)
{
$this->priceMax = $priceMax;
return $this;
}
/**
* Get priceMax.
*
* @return int
*/
public function getPriceMax()
{
return $this->priceMax;
}
/**
* Set options.
*
* @param array $options
*
* @return VehicleAlert
*/
public function setOptions($options)
{
$this->options = $options;
return $this;
}
/**
* Get options.
*
* @return array
*/
public function getOptions()
{
return $this->options;
}
/**
* Set createdAt.
*
* @param \DateTime $createdAt
*
* @return VehicleAlert
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt.
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt.
*
* @param \DateTime $updatedAt
*
* @return VehicleAlert
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt.
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
public function getSeatCount()
{
return $this->seatCount;
}
public function setSeatCount($seatCount): void
{
$this->seatCount = $seatCount;
}
public function getCo2()
{
return $this->co2;
}
public function setCo2($co2): void
{
$this->co2 = $co2;
}
public function getEuroEmissionStandard()
{
return $this->euroEmissionStandard;
}
public function setEuroEmissionStandard($euroEmissionStandard): void
{
$this->euroEmissionStandard = $euroEmissionStandard;
}
public function getVat()
{
return $this->vat;
}
public function setVat($vat): void
{
$this->vat = $vat;
}
public function getName()
{
$name = '';
if (count($this->segmentTransmission) > 0) {
$name .= (implode('|', $this->segmentTransmission).', ');
}
if (null !== $this->kilometersMin && null !== $this->kilometersMax) {
$name .= ('Km de '.$this->kilometersMin.' à '.$this->kilometersMax.', ');
} elseif (null !== $this->kilometersMin) {
$name .= ('Km min '.$this->kilometersMin.', ');
} elseif (null !== $this->kilometersMax) {
$name .= ('Km max '.$this->kilometersMax.', ');
}
if (null !== $this->millesimeMin && null !== $this->millesimeMax) {
$name .= ('Année de '.$this->millesimeMin.' à '.$this->millesimeMax.', ');
} elseif (null !== $this->millesimeMin) {
$name .= ('Année min '.$this->millesimeMin.', ');
} elseif (null !== $this->millesimeMax) {
$name .= ('Année max '.$this->millesimeMax.', ');
}
if (null !== $this->priceMin && null !== $this->priceMax) {
$name .= ('Prix de '.$this->priceMin.'€ à '.$this->priceMax.'€, ');
} elseif (null !== $this->priceMin) {
$name .= ('Prix min '.$this->priceMin.'€, ');
} elseif (null !== $this->priceMax) {
$name .= ('Prix max '.$this->priceMax.'€, ');
}
if (is_array($this->options) && count($this->options)) {
$name .= (implode('|', $this->options).', ');
}
$name = substr($name, 0, -2);
return $name;
}
public function getCategoriesNames(): string
{
$names = [];
$categories = $this->getCategory();
/** @var \Entity\Category $category */
foreach ($categories as $category) {
$names[] = $category->getName();
}
return implode(self::TERMS_SEPARATOR_DISPLAY, $names);
}
public function getEnergiesNames(): string
{
$names = [];
$energies = $this->getEnergy();
if (!is_array($energies)) {
$energies = explode(self::TERMS_SEPARATOR, (string) $energies);
}
foreach ($energies as $energy) {
$names[] = $energy;
}
return implode(self::TERMS_SEPARATOR_DISPLAY, $names);
}
}