<?php
namespace App\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use App\Helper\Enums\LanguageType;
enum SettingType: string
{
case FILE = 'FILE';
case STRING = 'STRING';
}
/**
* Settings
*
* @ORM\Table(name="settings", uniqueConstraints={
* @ORM\UniqueConstraint(name="name_lang_unique", columns={"name", "lang"})
* })
* @ORM\Entity(repositoryClass="App\Repository\SettingsRepository")
*/
class Settings
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var DateTime
*
* @ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt;
/**
* @var DateTime
*
* @ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
private $updatedAt;
/**
* @var string
*
* @ORM\Column(name="name", type="string", nullable=false, length=128)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="lang", type="string", length=3, options={"default" : LanguageType::UNIVERSAL})
*/
private $lang;
/**
* @var SettingType
*
* @ORM\Column(name="type", type="setting_type_enum_type", nullable=false)
*/
private $type;
/**
* @var string
*
* @ORM\Column(name="value", type="string", length=1024)
*/
private $value;
public function __construct()
{
$this->createdAt = new DateTime();
$this->updatedAt = new DateTime();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set name
*
* @param string $name
*
* @return Settings
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get lang
*
* @return string
*/
public function getLang()
{
return $this->lang;
}
/**
* Set lang
*
* @param string $lang
*
* @return Settings
*/
public function setLang($lang)
{
$this->lang = $lang;
return $this;
}
/**
* Get type
*
* @return SettingType
*/
public function getType()
{
return $this->type;
}
/**
* Set type
*
* @param SettingType $type
*
* @return Settings
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get value
*
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* Get serialized value
*
* @return array
*/
public function getSerializedValue(string $baseUrl = null)
{
$ret = [
"id" => $this->id,
"name" => $this->name,
"lang" => $this->lang
];
switch ($this->type) {
case SettingType::FILE:
$filePath = (string) $this->value;
if ($baseUrl !== null) {
$filePath = rtrim($baseUrl, '/') . '/' . ltrim($filePath, '/');
}
$ret["value"] = $filePath;
break;
default:
$ret["value"] = (string) $this->value;
break;
}
return $ret;
}
/**
* Set value
*
* @param string $value
*
* @return Settings
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get createdAt
*
* @return DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set createdAt
*
* @param DateTime $createdAt
*
* @return Settings
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get updatedAt
*
* @return DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set updatedAt
*
* @param DateTime $updatedAt
*
* @return Settings
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
public function serialize(string $baseUrl = null)
{
$value = null;
switch ($this->type) {
case SettingType::FILE:
$filePath = (string) $this->value;
if ($baseUrl !== null) {
$filePath = rtrim($baseUrl, '/') . '/' . ltrim($filePath, '/');
}
$value = $filePath;
break;
default:
$value = (string) $this->value;
break;
}
return [
'id' => $this->getId(),
'createdAt' => $this->getCreatedAt(),
'updatedAt' => $this->getUpdatedAt(),
'name' => $this->getName(),
'lang' => $this->getLang(),
'type' => $this->getType()->name,
'value' => $value,
];
}
}