<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\ParametroRepository")
* @UniqueEntity("name")
*/
class Parametro
{
const TYPE_STRING = 'string';
const TYPE_PASSWORD = 'password';
const TYPE_EMAIL = 'email';
const TYPE_PHONE = 'phone';
const TYPE_BOOLEAN = 'boolean';
const TYPE_INTEGER = 'integer';
const TYPE_DATE = 'date';
const TYPE_DATETIME = 'datetime';
const TYPE_DAYTIME = 'daytime';
const TYPE_ARRAY = 'array';
const TYPE_BLOCK = 'block';
CONST DATA_TYPES = [
self::TYPE_STRING => "Texto",
self::TYPE_PASSWORD => "Senha",
self::TYPE_EMAIL => "E-mail",
self::TYPE_PHONE => "Telefone",
self::TYPE_BOOLEAN => "Sim/Não",
self::TYPE_INTEGER => "Número",
self::TYPE_DATE => "Data",
self::TYPE_DATETIME => "Data/Hora",
self::TYPE_DAYTIME => "Horário",
self::TYPE_ARRAY => "Lista de Opções",
self::TYPE_BLOCK => "Bloco com Título e Corpo",
];
public function getValue() {
if ($this->dataType == self::TYPE_STRING) {
return $this->valueStr;
}
if ($this->dataType == self::TYPE_PASSWORD) {
return $this->valueStr;
}
if ($this->dataType == self::TYPE_EMAIL) {
return $this->valueStr;
}
if ($this->dataType == self::TYPE_PHONE) {
return $this->valueStr;
}
if ($this->dataType == self::TYPE_BOOLEAN) {
return $this->valueBool;
}
if ($this->dataType == self::TYPE_INTEGER) {
return $this->valueInt;
}
if ($this->dataType == self::TYPE_DATE) {
return $this->valueTimestamp;
}
if ($this->dataType == self::TYPE_DATETIME) {
return $this->valueTimestamp;
}
if ($this->dataType == self::TYPE_DAYTIME) {
return $this->valueTimestamp ? $this->valueTimestamp->format('H:i') : null;
}
if ($this->dataType == self::TYPE_ARRAY) {
return $this->valueJson;
}
if ($this->dataType == self::TYPE_BLOCK) {
return $this->valueJson;
}
}
public function setValue($value) {
if ($this->dataType == self::TYPE_STRING) {
$this->valueStr = strval($value);
}
if ($this->dataType == self::TYPE_PASSWORD) {
$this->valueStr = strval($value);
}
if ($this->dataType == self::TYPE_EMAIL) {
$this->valueStr = strval($value);
}
if ($this->dataType == self::TYPE_PHONE) {
$this->valueStr = strval($value);
}
if ($this->dataType == self::TYPE_BOOLEAN) {
$this->valueBool = (bool)$value;
}
if ($this->dataType == self::TYPE_INTEGER) {
$this->valueInt = $value === null ? null : intval($value);
}
if ($this->dataType == self::TYPE_DATE) {
$this->valueTimestamp = $value === null ? null : date('Y-m-d', $value);
}
if ($this->dataType == self::TYPE_DATETIME) {
$this->valueTimestamp = $value === null ? null : date('Y-m-d H:i:s', $value);
}
if ($this->dataType == self::TYPE_DAYTIME) {
$this->valueTimestamp = $value === null ? null : date('Y-m-d').$value;
}
if ($this->dataType == self::TYPE_ARRAY) {
$this->valueJson = $value;
}
if ($this->dataType == self::TYPE_BLOCK) {
$this->valueJson = $value;
}
}
public function getHumanValue(): string {
$value = $this->getValue();
if ($value === null) return '';
if ($this->dataType == self::TYPE_PASSWORD) {
return str_repeat('*', strlen($value));
}
if ($this->dataType == self::TYPE_BOOLEAN) {
return $value ? "sim" : "não";
}
if ($this->dataType == self::TYPE_INTEGER) {
return number_format($value, 0, ',', '.');
}
if ($this->dataType == self::TYPE_DATE) {
return $value->format('d/m/Y');
}
if ($this->dataType == self::TYPE_DATETIME) {
return $value->format('d/m/Y H:i');
}
if ($this->dataType == self::TYPE_DAYTIME) {
return $value;
}
if ($this->dataType == self::TYPE_ARRAY) {
if (count($value) > 3) {
return implode(', ', [$value[0], $value[1], $value[2], "e outros ".(count($value)-3)]);
}
else {
return implode(', ', $value);
}
}
if ($this->dataType == self::TYPE_BLOCK) {
return $value['title'];
}
return $value;
}
public function getDataTypeShortName(): string {
switch ($this->dataType) {
case self::TYPE_STRING: return "texto";
case self::TYPE_PASSWORD: return "senha";
case self::TYPE_EMAIL: return "email";
case self::TYPE_PHONE: return "telefone";
case self::TYPE_BOOLEAN: return "lógico";
case self::TYPE_INTEGER: return "núm";
case self::TYPE_DATE: return "data";
case self::TYPE_DATETIME: return "data/hora";
case self::TYPE_DAYTIME: return "hora";
case self::TYPE_ARRAY: return "lista";
case self::TYPE_BLOCK: return "bloco";
default: return $this->dataType;
}
}
public function getDataTypeName(): string {
return self::DATA_TYPES[$this->dataType];
}
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private ?int $id=null;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\Length(max=255)
*/
private string $name;
/**
* @ORM\Column(type="string", length=40)
* @Assert\Length(max=40)
*/
private string $dataType;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Length(max=255)
*/
private ?string $valueStr=null;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $valueBool=null;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private ?int $valueInt=null;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private ?\DateTimeInterface $valueTimestamp=null;
/**
* @ORM\Column(type="json", nullable=true)
*/
private ?array $valueJson=null;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(max=255)
*/
private string $description;
/**
* @ORM\Column(type="boolean")
*/
private bool $isAdmin;
public function getId(): ?int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDataType(): string
{
return $this->dataType;
}
public function setDataType(string $dataType): self
{
$this->dataType = $dataType;
return $this;
}
public function getValueStr(): ?string
{
return $this->valueStr;
}
public function setValueStr(?string $valueStr): self
{
$this->valueStr = $valueStr;
return $this;
}
public function getValueBool(): ?bool
{
return $this->valueBool;
}
public function setValueBool(?bool $valueBool): self
{
$this->valueBool = $valueBool;
return $this;
}
public function getValueInt(): ?int
{
return $this->valueInt;
}
public function setValueInt(?int $valueInt): self
{
$this->valueInt = $valueInt;
return $this;
}
public function getValueTimestamp(): ?\DateTimeInterface
{
return $this->valueTimestamp;
}
public function setValueTimestamp(?\DateTimeInterface $valueTimestamp): self
{
$this->valueTimestamp = $valueTimestamp;
return $this;
}
public function getValueJson(): ?array
{
return $this->valueJson;
}
public function setValueJson(?array $valueJson): self
{
$this->valueJson = $valueJson;
return $this;
}
public function getDescription(): string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getIsAdmin(): bool
{
return $this->isAdmin;
}
public function setIsAdmin(bool $isAdmin): self
{
$this->isAdmin = $isAdmin;
return $this;
}
}