src/Entity/App/DescubrirDestacado.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity\App;
  3. use App\Utils\CommonFunctions;
  4. use App\Utils\Constants;
  5. use App\Utils\Traits\ActivoTrait;
  6. use App\Utils\Traits\PosicionTrait;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Timestampable\Traits\TimestampableEntity;
  9. use Symfony\Component\HttpFoundation\File\File;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. /**
  12.  * @ORM\Table(name="descubrir_destacado", indexes={@ORM\Index(name="IDX_TITULO", columns={"titulo"})})
  13.  * @ORM\Entity(repositoryClass="App\Repository\App\DescubrirDestacadoRepository")
  14.  * @ORM\HasLifecycleCallbacks()
  15.  */
  16. class DescubrirDestacado
  17. {
  18.     /**
  19.      * @var integer|null
  20.      *
  21.      * @ORM\Column(name="id", type="integer")
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue
  24.      */
  25.     private $id;
  26.     /**
  27.      * @var string|null
  28.      *
  29.      * @ORM\Column(name="titulo", type="string", length=100)
  30.      * @Assert\NotBlank(message="El título es requerido")
  31.      * @Assert\Length(
  32.      *      max = 100,
  33.      *      maxMessage = "El título no puede tener más de {{ limit }} caracteres"
  34.      * )
  35.      */
  36.     private $titulo;
  37.     /**
  38.      * @var string|null
  39.      *
  40.      * @ORM\Column(name="descripcion", type="text", nullable=true)
  41.      * @Assert\NotBlank(message="La descripción es requerida")
  42.      * @Assert\Length(
  43.      *      max = 255,
  44.      *      maxMessage = "La descripción no puede tener más de {{ limit }} caracteres"
  45.      * )
  46.      */
  47.     private $descripcion;
  48.     /**
  49.      * @var string|null
  50.      *
  51.      * @ORM\Column(name="url", type="string", length=500, nullable=true)
  52.      * @Assert\NotBlank(message="La URL es requerida")
  53.      * @Assert\Regex(
  54.      *     pattern="/^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}[\/]?/",
  55.      *     message="Ingrese un link correcto"
  56.      * )
  57.      * @Assert\Length(
  58.      *      max = 500,
  59.      *      maxMessage = "La URL no puede tener más de {{ limit }} caracteres"
  60.      * )
  61.      */
  62.     private $url;
  63.     /**
  64.      * @var string|null
  65.      *
  66.      * @ORM\Column(name="color", type="string", length=255, nullable=true)
  67.      * @Assert\NotBlank(message="El color es requerido")
  68.      * @Assert\Length(
  69.      *      max = 255,
  70.      *      maxMessage = "El color no puede tener más de {{ limit }} caracteres"
  71.      * )
  72.      */
  73.     private $color;
  74.     /**
  75.      * @var File|null
  76.      *
  77.      * @Assert\Image(
  78.      *     mimeTypes={"image/jpg", "image/jpeg", "image/png", "image/webp",},
  79.      *     mimeTypesMessage="El tipo de imagen tiene que ser JPG, PNG, WEBP",
  80.      *     maxSize="400K"
  81.      * )
  82.      */
  83.     private $file;
  84.     /**
  85.      * @var string|null
  86.      *
  87.      * @ORM\Column(name="imagen", type="string", length=255)
  88.      * @Assert\Length(
  89.      *      max = 255,
  90.      *      maxMessage = "La cantidad maxima es {{ limit }} caracteres"
  91.      * )
  92.      */
  93.     private $imagen;
  94.     use ActivoTrait;
  95.     use TimestampableEntity;
  96.     use PosicionTrait;
  97.     /**
  98.      * @return int|null
  99.      */
  100.     public function getId(): ?int
  101.     {
  102.         return $this->id;
  103.     }
  104.     /**
  105.      * @return string|null
  106.      */
  107.     public function getTitulo(): ?string
  108.     {
  109.         return $this->titulo;
  110.     }
  111.     /**
  112.      * @param string|null $titulo
  113.      */
  114.     public function setTitulo(?string $titulo): void
  115.     {
  116.         $this->titulo $titulo;
  117.     }
  118.     /**
  119.      * @return string|null
  120.      */
  121.     public function getDescripcion(): ?string
  122.     {
  123.         return $this->descripcion;
  124.     }
  125.     /**
  126.      * @param string|null $descripcion
  127.      */
  128.     public function setDescripcion(?string $descripcion): void
  129.     {
  130.         $this->descripcion $descripcion;
  131.     }
  132.     /**
  133.      * @return string|null
  134.      */
  135.     public function getUrl(): ?string
  136.     {
  137.         return $this->url;
  138.     }
  139.     /**
  140.      * @param string|null $url
  141.      */
  142.     public function setUrl(?string $url): void
  143.     {
  144.         $this->url $url;
  145.     }
  146.     /**
  147.      * @return string|null
  148.      */
  149.     public function getColor(): ?string
  150.     {
  151.         return $this->color;
  152.     }
  153.     /**
  154.      * @param string|null $color
  155.      */
  156.     public function setColor(?string $color): void
  157.     {
  158.         $this->color $color;
  159.     }
  160.     /**
  161.      * @return File|null
  162.      */
  163.     public function getFile(): ?File
  164.     {
  165.         return $this->file;
  166.     }
  167.     /**
  168.      * @param File|null $file
  169.      */
  170.     public function setFile(?File $file): void
  171.     {
  172.         $this->file $file;
  173.     }
  174.     /**
  175.      * @return string|null
  176.      */
  177.     public function getImagen(): ?string
  178.     {
  179.         return $this->imagen;
  180.     }
  181.     /**
  182.      * @param string|null $imagen
  183.      */
  184.     public function setImagen(?string $imagen): void
  185.     {
  186.         $this->imagen $imagen;
  187.     }
  188.     private function getDirectoryFile()
  189.     {
  190.         $class CommonFunctions::getClassMethod(__CLASS__);
  191.         $class CommonFunctions::camelCaseToSnakeCase($class'-');
  192.         $dir CommonFunctions::getParameter('BASE_PATH_IMAGENES');
  193.         return CommonFunctions::getPathOrURLWithSlash($dir) . $class;
  194.     }
  195.     public function getAbsolutePath()
  196.     {
  197.         return CommonFunctions::getAbsolutePath($this->getDirectoryFile(), $this->getImagen());
  198.     }
  199.     public function guardarImagen()
  200.     {
  201.         $name 'imagen_destacado_' $this->getId();
  202.         $filename CommonFunctions::saveUploadedFile($this->getFile(), $this->getDirectoryFile(), $name);
  203.         if ($filename && $filename != $this->getImagen()) {
  204.             $this->setImagen($filename);
  205.         }
  206.     }
  207.     /**
  208.      * @ORM\PrePersist
  209.      */
  210.     public function prePersist()
  211.     {
  212.         $this->setImagen(Constants::NO_IMAGE_ICON);
  213.     }
  214.     /**
  215.      * @ORM\PreRemove
  216.      */
  217.     public function preRemove()
  218.     {
  219.         CommonFunctions::removeFile($this->getAbsolutePath());
  220.     }
  221.     public function getImagenBase64()
  222.     {
  223.         $image null;
  224.         if ($this->getId()) {
  225.             $image CommonFunctions::getBase64OfImage($this->getAbsolutePath(), Constants::DEFAULT_IMAGE);
  226.         }
  227.         return $image;
  228.     }
  229.     public function __toString()
  230.     {
  231.         return $this->getTitulo() ?: 'Nuevo Destacado';
  232.     }
  233. }