<?php
namespace Boab\CmsBundle\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Sonata\BlockBundle\Model\BlockInterface;
use Ramsey\Uuid\Rfc4122\UuidInterface ;
use Ramsey\Uuid\Uuid;
/**
* @ORM\Entity (repositoryClass="Boab\CmsBundle\Repository\BlockRepository")
* @ORM\Table(name="blocks")
*/
class Block implements BlockInterface, UuidGeneratedInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Uuid
* @ORM\Column(type="uuid", unique=true)
*/
protected $uuid;
/**
* @var string
*
* @ORM\Column(name="name", type="string",nullable=false, unique=true)
*/
private $name;
/**
* @var array
* @ORM\Column(type="array", name="settings")
*/
private $settings;
/**
* @var bool
*
* @ORM\Column(name="enabled", type="boolean", nullable=true)
*/
private $enabled;
/**
* @var integer
*
* @ORM\Column(name="position", type="integer", nullable=true)
*/
private $position;
/**
* @var BlockInterface|null
* @ORM\Column(name="parent", type="integer", nullable=true)
*/
private $parent;
/**
* @var BlockInterface[]
*/
private $children;
/**
* @var \DateTime|null
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var \DateTime|null
* @ORM\Column(name="updated_at", type="datetime")
*/
private $updatedAt;
/**
* @var string|null
*
* @ORM\Column(name="type", type="string")
*/
private $type;
/**
* @var int|null
*
* @ORM\Column(name="ttl", type="integer")*
*/
private $ttl;
public function __construct()
{
$this->settings = [];
$this->enabled = false;
$this->children = [];
$this->uuid = Uuid::uuid4();
}
public function setId($id):void
{
}
/**
* Get id.
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Set name.
*
* @param string $name
*
* @return Block
*/
public function setName(string $name):void
{
$this->name = $name;
}
/**
* Get name.
*
* @return string
*/
public function getName():?string
{
return $this->name;
}
/**
* Set settings.
*
* @param array $settings
*
* @return Block
*/
public function setSettings(array $settings = []):void
{
$this->settings = $settings;
}
/**
* Get settings.
*
* @return array
*/
public function getSettings():array
{
return $this->settings;
}
/**
* Set enabled.
*
* @param bool|null $enabled
*
* @return Block
*/
public function setEnabled(bool $enabled):void
{
$this->enabled = $enabled;
}
/**
* Get enabled.
*
* @return bool|null
*/
public function getEnabled():bool
{
return $this->enabled;
}
/**
* Set position.
*
* @param int|null $position
*
* @return Block
*/
public function setPosition(int $position):void
{
$this->position = $position;
}
/**
* Get position.
*
* @return int|null
*/
public function getPosition():?int
{
return $this->position;
}
/**
* Set parent.
*
* @param int|null $parent
*
* @return Block
*/
public function setParent(?BLockInterface $parent = null):void
{
$this->parent = $parent;
}
/**
* Get parent.
*
* @return int|null
*/
public function getParent():?BLockInterface
{
return $this->parent;
}
/**
* {@inheritdoc}
*/
public function hasParent():bool
{
return $this->getParent() instanceof self;
}
/**
* {@inheritdoc}
*/
public function addChildren(BlockInterface $child):void
{
$this->children[] = $child;
$child->setParent($this);
}
/**
* Set children.
*
* @param int|null $children
*
* @return Block
*/
public function setChildren($children = null)
{
$this->children = [];
return $this;
}
/**
* Get children.
*
* @return int|null
*/
public function getChildren():array
{
return [];
}
/**
* {@inheritdoc}
*/
public function hasChildren():bool
{
return \count($this->getChildren()) > 0;
}
/**
* Set createdAt.
*
* @param bool $createdAt
*
* @return Block
*/
public function setCreatedAt(\DateTime $createdAt = null):void
{
$this->createdAt = $createdAt;
}
/**
* Get createdAt.
*
* @return bool
*/
public function getCreatedAt():?DateTime
{
return $this->createdAt;
}
/**
* Set updatedAt.
*
* @param bool $updatedAt
*
* @return Block
*/
public function setUpdatedAt(\DateTime $updatedAt = null):void
{
$this->updatedAt = $updatedAt;
}
/**
* Get updatedAt.
*
* @return bool
*/
public function getUpdatedAt():?DateTime
{
return $this->updatedAt;
}
/**
* Set type.
*
* @param string $type
*
* @return Block
*/
public function setType(string $type):void
{
$this->type = $type;
}
/**
* Get type.
*
* @return string
*/
public function getType():?string
{
return $this->type;
}
/**
* Set ttl.
*
* @param int $ttl
*
* @return Block
*/
public function setTtl($ttl)
{
$this->ttl = $ttl;
return $this;
}
/**
* Get ttl.
*
* @return int
*/
/**
* {@inheritdoc}
*/
public function getTtl():int
{
if (!$this->getSetting('use_cache', true)) {
return 0;
}
$ttl = $this->getSetting('ttl', 86400);
foreach ($this->getChildren() as $block) {
$blockTtl = $block->getTtl();
$ttl = ($blockTtl < $ttl) ? $blockTtl : $ttl;
}
$this->ttl = $ttl;
return $this->ttl;
}
public function setSetting(string $name, $value):void
{
$this->settings[$name] = $value;
}
/**
* {@inheritdoc}
*/
public function getSetting($name, $default = null):?string
{
return isset($this->settings[$name]) ? $this->settings[$name] : $default;
}
/**
* Get the value of uuid
*
* @return Uuid
*/
public function getUuid()
{
return $this->uuid;
}
/**
* Set the value of uuid
*
* @param Uuid $uuid
*
* @return self
*/
public function setUuid(UuidInterface $uuid)
{
$this->uuid = $uuid;
return $this;
}
}