src/Entity/Parametro.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. /**
  7.  * @ORM\Entity(repositoryClass="App\Repository\ParametroRepository")
  8.  * @UniqueEntity("name")
  9.  */
  10. class Parametro
  11. {
  12.     const TYPE_STRING 'string';
  13.     const TYPE_PASSWORD 'password';
  14.     const TYPE_EMAIL 'email';
  15.     const TYPE_PHONE 'phone';
  16.     const TYPE_BOOLEAN 'boolean';
  17.     const TYPE_INTEGER 'integer';
  18.     const TYPE_DATE 'date';
  19.     const TYPE_DATETIME 'datetime';
  20.     const TYPE_DAYTIME 'daytime';
  21.     const TYPE_ARRAY 'array';
  22.     const TYPE_BLOCK 'block';
  23.     CONST DATA_TYPES = [
  24.         self::TYPE_STRING => "Texto",
  25.         self::TYPE_PASSWORD => "Senha",
  26.         self::TYPE_EMAIL => "E-mail",
  27.         self::TYPE_PHONE => "Telefone",
  28.         self::TYPE_BOOLEAN => "Sim/Não",
  29.         self::TYPE_INTEGER => "Número",
  30.         self::TYPE_DATE => "Data",
  31.         self::TYPE_DATETIME => "Data/Hora",
  32.         self::TYPE_DAYTIME => "Horário",
  33.         self::TYPE_ARRAY => "Lista de Opções",
  34.         self::TYPE_BLOCK => "Bloco com Título e Corpo",
  35.     ];
  36.     public function getValue() {
  37.         if ($this->dataType == self::TYPE_STRING) {
  38.             return $this->valueStr;
  39.         }
  40.         if ($this->dataType == self::TYPE_PASSWORD) {
  41.             return $this->valueStr;
  42.         }
  43.         if ($this->dataType == self::TYPE_EMAIL) {
  44.             return $this->valueStr;
  45.         }
  46.         if ($this->dataType == self::TYPE_PHONE) {
  47.             return $this->valueStr;
  48.         }
  49.         if ($this->dataType == self::TYPE_BOOLEAN) {
  50.             return $this->valueBool;
  51.         }
  52.         if ($this->dataType == self::TYPE_INTEGER) {
  53.             return $this->valueInt;
  54.         }
  55.         if ($this->dataType == self::TYPE_DATE) {
  56.             return $this->valueTimestamp;
  57.         }
  58.         if ($this->dataType == self::TYPE_DATETIME) {
  59.             return $this->valueTimestamp;
  60.         }
  61.         if ($this->dataType == self::TYPE_DAYTIME) {
  62.             return $this->valueTimestamp $this->valueTimestamp->format('H:i') : null;
  63.         }
  64.         if ($this->dataType == self::TYPE_ARRAY) {
  65.             return $this->valueJson;
  66.         }
  67.         if ($this->dataType == self::TYPE_BLOCK) {
  68.             return $this->valueJson;
  69.         }
  70.     }
  71.     public function setValue($value) {
  72.         if ($this->dataType == self::TYPE_STRING) {
  73.             $this->valueStr strval($value);
  74.         }
  75.         if ($this->dataType == self::TYPE_PASSWORD) {
  76.             $this->valueStr strval($value);
  77.         }
  78.         if ($this->dataType == self::TYPE_EMAIL) {
  79.             $this->valueStr strval($value);
  80.         }
  81.         if ($this->dataType == self::TYPE_PHONE) {
  82.             $this->valueStr strval($value);
  83.         }
  84.         if ($this->dataType == self::TYPE_BOOLEAN) {
  85.             $this->valueBool = (bool)$value;
  86.         }
  87.         if ($this->dataType == self::TYPE_INTEGER) {
  88.             $this->valueInt $value === null null intval($value);
  89.         }
  90.         if ($this->dataType == self::TYPE_DATE) {
  91.             $this->valueTimestamp $value === null null date('Y-m-d'$value);
  92.         }
  93.         if ($this->dataType == self::TYPE_DATETIME) {
  94.             $this->valueTimestamp $value === null null date('Y-m-d H:i:s'$value);
  95.         }
  96.         if ($this->dataType == self::TYPE_DAYTIME) {
  97.             $this->valueTimestamp $value === null null date('Y-m-d').$value;
  98.         }
  99.         if ($this->dataType == self::TYPE_ARRAY) {
  100.             $this->valueJson $value;
  101.         }
  102.         if ($this->dataType == self::TYPE_BLOCK) {
  103.             $this->valueJson $value;
  104.         }
  105.     }
  106.     public function getHumanValue(): string {
  107.         $value $this->getValue();
  108.         if ($value === null) return '';
  109.         if ($this->dataType == self::TYPE_PASSWORD) {
  110.             return str_repeat('*'strlen($value));
  111.         }
  112.         if ($this->dataType == self::TYPE_BOOLEAN) {
  113.             return $value "sim" "não";
  114.         }
  115.         if ($this->dataType == self::TYPE_INTEGER) {
  116.             return number_format($value0',''.');
  117.         }
  118.         if ($this->dataType == self::TYPE_DATE) {
  119.             return $value->format('d/m/Y');
  120.         }
  121.         if ($this->dataType == self::TYPE_DATETIME) {
  122.             return $value->format('d/m/Y H:i');
  123.         }
  124.         if ($this->dataType == self::TYPE_DAYTIME) {
  125.             return $value;
  126.         }
  127.         if ($this->dataType == self::TYPE_ARRAY) {
  128.             if (count($value) > 3) {
  129.                 return implode(', ', [$value[0], $value[1], $value[2], "e outros ".(count($value)-3)]);
  130.             }
  131.             else {
  132.                 return implode(', '$value);
  133.             }
  134.         }
  135.         if ($this->dataType == self::TYPE_BLOCK) {
  136.             return $value['title'];
  137.         }
  138.         return $value;
  139.     }
  140.     public function getDataTypeShortName(): string {
  141.         switch ($this->dataType) {
  142.             case self::TYPE_STRING: return "texto";
  143.             case self::TYPE_PASSWORD: return "senha";
  144.             case self::TYPE_EMAIL: return "email";
  145.             case self::TYPE_PHONE: return "telefone";
  146.             case self::TYPE_BOOLEAN: return "lógico";
  147.             case self::TYPE_INTEGER: return "núm";
  148.             case self::TYPE_DATE: return "data";
  149.             case self::TYPE_DATETIME: return "data/hora";
  150.             case self::TYPE_DAYTIME: return  "hora";
  151.             case self::TYPE_ARRAY: return "lista";
  152.             case self::TYPE_BLOCK: return "bloco";
  153.             default: return $this->dataType;
  154.         }
  155.     }
  156.     public function getDataTypeName(): string {
  157.         return self::DATA_TYPES[$this->dataType];
  158.     }
  159.     /**
  160.      * @ORM\Id()
  161.      * @ORM\GeneratedValue()
  162.      * @ORM\Column(type="integer")
  163.      */
  164.     private ?int $id=null;
  165.     /**
  166.      * @ORM\Column(type="string", length=255, unique=true)
  167.      * @Assert\Length(max=255)
  168.      */
  169.     private string $name;
  170.     /**
  171.      * @ORM\Column(type="string", length=40)
  172.      * @Assert\Length(max=40)
  173.      */
  174.     private string $dataType;
  175.     /**
  176.      * @ORM\Column(type="string", length=255, nullable=true)
  177.      * @Assert\Length(max=255)
  178.      */
  179.     private ?string $valueStr=null;
  180.     /**
  181.      * @ORM\Column(type="boolean", nullable=true)
  182.      */
  183.     private ?bool $valueBool=null;
  184.     /**
  185.      * @ORM\Column(type="integer", nullable=true)
  186.      */
  187.     private ?int $valueInt=null;
  188.     /**
  189.      * @ORM\Column(type="datetime", nullable=true)
  190.      */
  191.     private ?\DateTimeInterface $valueTimestamp=null;
  192.     /**
  193.      * @ORM\Column(type="json", nullable=true)
  194.      */
  195.     private ?array $valueJson=null;
  196.     /**
  197.      * @ORM\Column(type="string", length=255)
  198.      * @Assert\Length(max=255)
  199.      */
  200.     private string $description;
  201.     /**
  202.      * @ORM\Column(type="boolean")
  203.      */
  204.     private bool $isAdmin;
  205.     public function getId(): ?int
  206.     {
  207.         return $this->id;
  208.     }
  209.     public function getName(): string
  210.     {
  211.         return $this->name;
  212.     }
  213.     public function setName(string $name): self
  214.     {
  215.         $this->name $name;
  216.         return $this;
  217.     }
  218.     public function getDataType(): string
  219.     {
  220.         return $this->dataType;
  221.     }
  222.     public function setDataType(string $dataType): self
  223.     {
  224.         $this->dataType $dataType;
  225.         return $this;
  226.     }
  227.     public function getValueStr(): ?string
  228.     {
  229.         return $this->valueStr;
  230.     }
  231.     public function setValueStr(?string $valueStr): self
  232.     {
  233.         $this->valueStr $valueStr;
  234.         return $this;
  235.     }
  236.     public function getValueBool(): ?bool
  237.     {
  238.         return $this->valueBool;
  239.     }
  240.     public function setValueBool(?bool $valueBool): self
  241.     {
  242.         $this->valueBool $valueBool;
  243.         return $this;
  244.     }
  245.     public function getValueInt(): ?int
  246.     {
  247.         return $this->valueInt;
  248.     }
  249.     public function setValueInt(?int $valueInt): self
  250.     {
  251.         $this->valueInt $valueInt;
  252.         return $this;
  253.     }
  254.     public function getValueTimestamp(): ?\DateTimeInterface
  255.     {
  256.         return $this->valueTimestamp;
  257.     }
  258.     public function setValueTimestamp(?\DateTimeInterface $valueTimestamp): self
  259.     {
  260.         $this->valueTimestamp $valueTimestamp;
  261.         return $this;
  262.     }
  263.     public function getValueJson(): ?array
  264.     {
  265.         return $this->valueJson;
  266.     }
  267.     public function setValueJson(?array $valueJson): self
  268.     {
  269.         $this->valueJson $valueJson;
  270.         return $this;
  271.     }
  272.     public function getDescription(): string
  273.     {
  274.         return $this->description;
  275.     }
  276.     public function setDescription(string $description): self
  277.     {
  278.         $this->description $description;
  279.         return $this;
  280.     }
  281.     public function getIsAdmin(): bool
  282.     {
  283.         return $this->isAdmin;
  284.     }
  285.     public function setIsAdmin(bool $isAdmin): self
  286.     {
  287.         $this->isAdmin $isAdmin;
  288.         return $this;
  289.     }
  290. }