src/Submarine/PagesBundle/Entity/Type/PageValue.php line 23

Open in your IDE?
  1. <?php
  2. /**
  3.  * (c) itmedia.by <info@itmedia.by>
  4.  */
  5. namespace Submarine\PagesBundle\Entity\Type;
  6. use Postroyka\AppBundle\Redis\RedisBaseProvider;
  7. use Submarine\CoreBundle\Entity\SubmarineEntityInterface;
  8. use Submarine\CoreBundle\Exception\InvalidArgumentException;
  9. use Submarine\PagesBundle\Entity\Page;
  10. use Doctrine\ORM\Mapping as ORM;
  11. /**
  12.  * Class PageTypeValue
  13.  * @package Submarine\PagesBundle
  14.  *
  15.  * @ORM\Entity()
  16.  * @ORM\Table(name="submarine_pages_values")
  17.  *
  18.  * @ORM\HasLifecycleCallbacks()
  19.  */
  20. class PageValue implements SubmarineEntityInterface
  21. {
  22.     /**
  23.      * ID
  24.      * @var int
  25.      *
  26.      * @ORM\Column(name="id", type="integer", nullable=false, unique=true)
  27.      * @ORM\Id()
  28.      * @ORM\GeneratedValue(strategy="AUTO")
  29.      */
  30.     private $id;
  31.     /**
  32.      * @var Page
  33.      *
  34.      * @ORM\ManyToOne(targetEntity="Submarine\PagesBundle\Entity\Page", inversedBy="fields")
  35.      * @ORM\JoinColumn(name="page_id", referencedColumnName="id")
  36.      */
  37.     private $page;
  38.     /**
  39.      * Поле типа
  40.      * @var PageTypeField
  41.      *
  42.      * @ORM\ManyToOne(targetEntity="Submarine\PagesBundle\Entity\Type\PageTypeField", inversedBy="values")
  43.      * @ORM\JoinColumns({
  44.      *     @ORM\JoinColumn(name="field_id", referencedColumnName="id"),
  45.      *     @ORM\JoinColumn(name="type_id", referencedColumnName="type_id")
  46.      * })
  47.      */
  48.     private $field;
  49.     /**
  50.      * Значение
  51.      * @var string
  52.      *
  53.      * @ORM\Column(name="value", type="text", nullable=true)
  54.      */
  55.     private $value;
  56.     public function __construct($field$page$value null)
  57.     {
  58.         $this->field $field;
  59.         $this->page $page;
  60.         $this->setValue($value);
  61.     }
  62.     /**
  63.      * Имя сущности
  64.      * @return string
  65.      */
  66.     public static function entityName()
  67.     {
  68.         return __CLASS__;
  69.     }
  70.     /**
  71.      * @return int
  72.      */
  73.     public function getId()
  74.     {
  75.         return $this->id;
  76.     }
  77.     public function __toString()
  78.     {
  79.         return (string)$this->value;
  80.     }
  81.     /**
  82.      * @return Page
  83.      */
  84.     public function getPage()
  85.     {
  86.         return $this->page;
  87.     }
  88.     /**
  89.      * @return PageTypeField
  90.      */
  91.     public function getField()
  92.     {
  93.         return $this->field;
  94.     }
  95.     /**
  96.      * @return string
  97.      */
  98.     public function getValue()
  99.     {
  100.         if ($this->getField()->getValueType() === PageTypeField::TYPE_ITEM) {
  101.             return is_array(json_decode($this->value)) ? json_decode($this->value) : [$this->value];
  102.         }
  103.         if ($this->getField()->getValueType() === PageTypeField::TYPE_CHECKBOX) {
  104.             return (bool)$this->value;
  105.         }
  106.         return $this->value;
  107.     }
  108.     /**
  109.      * @param PageTypeField $field
  110.      */
  111.     public function setField($field)
  112.     {
  113.         $this->field $field;
  114.     }
  115.     /**
  116.      * @param string|array|null $value
  117.      *
  118.      * @throws InvalidArgumentException
  119.      */
  120.     public function setValue($value)
  121.     {
  122.         if ($value instanceof Page) {
  123.             $value $value->getId();
  124.         }
  125.         if (is_array($value)) {
  126.             $value json_encode($value);
  127.         }
  128.         if (!(is_scalar($value) || is_array($value) || empty($value))) {
  129.             throw new InvalidArgumentException('Page Field Value must be string or array');
  130.         }
  131.         $this->value $value;
  132.     }
  133.     /**
  134.      * @ORM\PreUpdate()
  135.      */
  136.     public function preUpdate()
  137.     {
  138.         RedisBaseProvider::clearKeysByPattern('*' RedisBaseProvider::REDIS_HOMEPAGE_KEY);
  139.         RedisBaseProvider::clearKeysByPattern('*' RedisBaseProvider::CATALOG_PAGE_REDIS_KEY '*');
  140.     }
  141. }