<?php
/**
* (c) itmedia.by <info@itmedia.by>
*/
namespace Submarine\PropertiesBundle\Entity;
use Submarine\CoreBundle\Entity\SubmarineEntityInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Asset;
/**
* @ORM\Entity()
* @ORM\Table(
* name="submarine_properties_values",
* indexes={
* @ORM\Index(name="entity", columns={"entity_name", "entity_id"})
* }
* )
*/
class PropertyValue implements SubmarineEntityInterface
{
/**
* @var int
*
* @ORM\Id()
* @ORM\Column(name="id", type="integer", nullable=false, unique=true)
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Property
*
* @ORM\ManyToOne(targetEntity="Submarine\PropertiesBundle\Entity\Property")
* @ORM\JoinColumn(name="property_id", referencedColumnName="id")
*/
private $property;
/**
* @var string
*
* @ORM\Column(name="entity_name", type="string", nullable=false)
*/
private $entityName;
/**
* @var int
*
* @ORM\Column(name="entity_id", type="integer", nullable=false)
*/
private $entityId;
/**
* @var string
*
* @ORM\Column(name="value", type="text", nullable=true)
*/
private $value;
/**
* @var string
*
* @ORM\Column(name="value_number", type="float", nullable=true)
*/
private $valueNumber;
/**
* Имя сущности
* @return string
*/
public static function entityName()
{
return __CLASS__;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return Property
*/
public function getProperty()
{
return $this->property;
}
/**
* @param Property $property
*/
public function setProperty(Property $property)
{
$this->property = $property;
}
/**
* @return string
*/
public function getEntityName()
{
return $this->entityName;
}
/**
* @param SubmarineEntityInterface $entityInterface
*/
public function setEntity(SubmarineEntityInterface $entityInterface)
{
$this->entityName = $entityInterface->entityName();
$this->entityId = $entityInterface->getId();
}
/**
* @return int
*/
public function getEntityId()
{
return $this->entityId;
}
/**
* @return string
*/
public function getValue()
{
if ($this->getProperty()->getValueType() === Property::TYPE_NUMBER) {
return $this->valueNumber;
}
return $this->value;
}
/**
* @param string $value
*/
public function setValue($value)
{
if (!$value and $value != '0') {
$this->value = null;
$this->valueNumber = null;
return;
}
if ($this->property->getValueType() === Property::TYPE_NUMBER) {
$value = str_replace(',', '.', trim($value));
$this->valueNumber = (float)$value;
$this->value = null;
} else {
$this->value = trim($value);
$this->valueNumber = null;
}
}
/**
* @return bool
*/
public function isNumber()
{
return ($this->getProperty()->getValueType() === Property::TYPE_NUMBER);
}
}