src/Submarine/PropertiesBundle/Entity/PropertyValue.php line 22

Open in your IDE?
  1. <?php
  2. /**
  3.  * (c) itmedia.by <info@itmedia.by>
  4.  */
  5. namespace Submarine\PropertiesBundle\Entity;
  6. use Submarine\CoreBundle\Entity\SubmarineEntityInterface;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Validator\Constraints as Asset;
  9. /**
  10.  * @ORM\Entity()
  11.  * @ORM\Table(
  12.  *      name="submarine_properties_values",
  13.  *      indexes={
  14.  *          @ORM\Index(name="entity", columns={"entity_name", "entity_id"})
  15.  *      }
  16.  *  )
  17.  */
  18. class PropertyValue implements SubmarineEntityInterface
  19. {
  20.     /**
  21.      * @var int
  22.      *
  23.      * @ORM\Id()
  24.      * @ORM\Column(name="id", type="integer", nullable=false, unique=true)
  25.      * @ORM\GeneratedValue(strategy="AUTO")
  26.      */
  27.     private $id;
  28.     /**
  29.      * @var Property
  30.      *
  31.      * @ORM\ManyToOne(targetEntity="Submarine\PropertiesBundle\Entity\Property")
  32.      * @ORM\JoinColumn(name="property_id", referencedColumnName="id")
  33.      */
  34.     private $property;
  35.     /**
  36.      * @var string
  37.      *
  38.      * @ORM\Column(name="entity_name", type="string", nullable=false)
  39.      */
  40.     private $entityName;
  41.     /**
  42.      * @var int
  43.      *
  44.      * @ORM\Column(name="entity_id", type="integer", nullable=false)
  45.      */
  46.     private $entityId;
  47.     /**
  48.      * @var string
  49.      *
  50.      * @ORM\Column(name="value", type="text", nullable=true)
  51.      */
  52.     private $value;
  53.     /**
  54.      * @var string
  55.      *
  56.      * @ORM\Column(name="value_number", type="float", nullable=true)
  57.      */
  58.     private $valueNumber;
  59.     /**
  60.      * Имя сущности
  61.      * @return string
  62.      */
  63.     public static function entityName()
  64.     {
  65.         return __CLASS__;
  66.     }
  67.     /**
  68.      * @return int
  69.      */
  70.     public function getId()
  71.     {
  72.         return $this->id;
  73.     }
  74.     /**
  75.      * @return Property
  76.      */
  77.     public function getProperty()
  78.     {
  79.         return $this->property;
  80.     }
  81.     /**
  82.      * @param Property $property
  83.      */
  84.     public function setProperty(Property $property)
  85.     {
  86.         $this->property $property;
  87.     }
  88.     /**
  89.      * @return string
  90.      */
  91.     public function getEntityName()
  92.     {
  93.         return $this->entityName;
  94.     }
  95.     /**
  96.      * @param SubmarineEntityInterface $entityInterface
  97.      */
  98.     public function setEntity(SubmarineEntityInterface $entityInterface)
  99.     {
  100.         $this->entityName $entityInterface->entityName();
  101.         $this->entityId $entityInterface->getId();
  102.     }
  103.     /**
  104.      * @return int
  105.      */
  106.     public function getEntityId()
  107.     {
  108.         return $this->entityId;
  109.     }
  110.     /**
  111.      * @return string
  112.      */
  113.     public function getValue()
  114.     {
  115.         if ($this->getProperty()->getValueType() === Property::TYPE_NUMBER) {
  116.             return $this->valueNumber;
  117.         }
  118.         return $this->value;
  119.     }
  120.     /**
  121.      * @param string $value
  122.      */
  123.     public function setValue($value)
  124.     {
  125.         if (!$value and $value != '0') {
  126.             $this->value null;
  127.             $this->valueNumber null;
  128.             return;
  129.         }
  130.         if ($this->property->getValueType() === Property::TYPE_NUMBER) {
  131.             $value str_replace(',''.'trim($value));
  132.             $this->valueNumber = (float)$value;
  133.             $this->value null;
  134.         } else {
  135.             $this->value trim($value);
  136.             $this->valueNumber null;
  137.         }
  138.     }
  139.     /**
  140.      * @return bool
  141.      */
  142.     public function isNumber()
  143.     {
  144.         return ($this->getProperty()->getValueType() === Property::TYPE_NUMBER);
  145.     }
  146. }