<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Entity\File as EmbeddedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass="App\Repository\BadgeRepository")
* @Vich\Uploadable
* @ORM\HasLifecycleCallbacks()
*/
#[ApiResource(
attributes: ['security' => "is_granted('ROLE_USER')"],
collectionOperations: [
'get',
'post' => ['security' => "is_granted('ROLE_ADMIN')"],
],
itemOperations: [
'get',
'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
],
)]
#[ApiFilter(PropertyFilter::class)]
#[ApiFilter(SearchFilter::class, properties: ['id', 'label' => 'partial'])]
#[ApiFilter(OrderFilter::class, properties: ['id', 'label'], arguments: ['orderParameterName' => 'order'])]
class Badge
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=128)
* @Assert\NotBlank()
* @Assert\Length(max=128)
*/
private $label;
/**
* @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
*/
private $image;
/**
* @Vich\UploadableField(mapping="badge", fileNameProperty="image.name", size="image.size", mimeType="image.mimeType", originalName="image.originalName", dimensions="image.dimensions")
* @Assert\Image(maxSize="8M", mimeTypes={"image/jpeg", "image/png", "image/gif"})
* @Assert\Expression(expression="this.getImageFile() or this.getImage().getName()")
*/
private $imageFile;
/**
* Badge constructor.
*/
public function __construct()
{
$this->image = new EmbeddedFile();
}
public function getId(): ?int
{
return $this->id;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function __toString(): string
{
return $this->label;
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
/**
* @return Badge|User
*/
public function setImageFile(?File $image = null): self
{
$this->imageFile = $image;
if ($image) {
$this->updatedAt = new DateTime();
}
return $this;
}
public function getImage(): EmbeddedFile
{
return $this->image;
}
public function setImage(EmbeddedFile $image): self
{
$this->image = $image;
return $this;
}
}