src/Entity/Badge.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  8. use DateTime;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Gedmo\Timestampable\Traits\TimestampableEntity;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use Vich\UploaderBundle\Entity\File as EmbeddedFile;
  14. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  15. /**
  16.  * @ORM\Entity(repositoryClass="App\Repository\BadgeRepository")
  17.  * @Vich\Uploadable
  18.  * @ORM\HasLifecycleCallbacks()
  19.  */
  20. #[ApiResource(
  21.     attributes: ['security' => "is_granted('ROLE_USER')"],
  22.     collectionOperations: [
  23.         'get',
  24.         'post' => ['security' => "is_granted('ROLE_ADMIN')"],
  25.     ],
  26.     itemOperations: [
  27.         'get',
  28.         'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
  29.     ],
  30. )]
  31. #[ApiFilter(PropertyFilter::class)]
  32. #[ApiFilter(SearchFilter::class, properties: ['id''label' => 'partial'])]
  33. #[ApiFilter(OrderFilter::class, properties: ['id''label'], arguments: ['orderParameterName' => 'order'])]
  34. class Badge
  35. {
  36.     use TimestampableEntity;
  37.     /**
  38.      * @ORM\Id()
  39.      * @ORM\GeneratedValue()
  40.      * @ORM\Column(type="integer")
  41.      */
  42.     private $id;
  43.     /**
  44.      * @ORM\Column(type="string", length=128)
  45.      * @Assert\NotBlank()
  46.      * @Assert\Length(max=128)
  47.      */
  48.     private $label;
  49.     /**
  50.      * @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
  51.      */
  52.     private $image;
  53.     /**
  54.      * @Vich\UploadableField(mapping="badge", fileNameProperty="image.name", size="image.size", mimeType="image.mimeType", originalName="image.originalName", dimensions="image.dimensions")
  55.      * @Assert\Image(maxSize="8M", mimeTypes={"image/jpeg", "image/png", "image/gif"})
  56.      * @Assert\Expression(expression="this.getImageFile() or this.getImage().getName()")
  57.      */
  58.     private $imageFile;
  59.     /**
  60.      * Badge constructor.
  61.      */
  62.     public function __construct()
  63.     {
  64.         $this->image = new EmbeddedFile();
  65.     }
  66.     public function getId(): ?int
  67.     {
  68.         return $this->id;
  69.     }
  70.     public function getLabel(): ?string
  71.     {
  72.         return $this->label;
  73.     }
  74.     public function setLabel(string $label): self
  75.     {
  76.         $this->label $label;
  77.         return $this;
  78.     }
  79.     public function __toString(): string
  80.     {
  81.         return $this->label;
  82.     }
  83.     public function getImageFile(): ?File
  84.     {
  85.         return $this->imageFile;
  86.     }
  87.     /**
  88.      * @return Badge|User
  89.      */
  90.     public function setImageFile(?File $image null): self
  91.     {
  92.         $this->imageFile $image;
  93.         if ($image) {
  94.             $this->updatedAt = new DateTime();
  95.         }
  96.         return $this;
  97.     }
  98.     public function getImage(): EmbeddedFile
  99.     {
  100.         return $this->image;
  101.     }
  102.     public function setImage(EmbeddedFile $image): self
  103.     {
  104.         $this->image $image;
  105.         return $this;
  106.     }
  107. }