You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
herbarium/src/Entity/Post.php

291 lines
6.8 KiB

<?php
namespace App\Entity;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use App\Repository\PostRepository;
use App\Validator\ImageSafety;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[ORM\Entity(repositoryClass: PostRepository::class)]
#[ORM\HasLifecycleCallbacks]
#[ApiResource(
operations: [new Metadata\Post(), new Metadata\Get(), new Metadata\Put(), new Metadata\Delete(), new Metadata\Patch()],
normalizationContext: ['groups' => ['post:collection:read', 'post:read']],
)]
#[GetCollection(normalizationContext: ['groups' => ['post:collection:read']])]
#[Metadata\ApiFilter(filterClass: SearchFilter::class, properties: ['species' => 'exact'])]
#[Vich\Uploadable]
class Post
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['post:collection:read'])]
private ?int $id = null;
#[ORM\Column]
#[Groups(['post:collection:read'])]
#[Assert\NotBlank]
private ?\DateTimeImmutable $foundDate = null;
#[ORM\Column]
#[ApiProperty(writable: false)]
#[Groups(['post:collection:read'])]
private ?\DateTimeImmutable $publicationDate = null;
#[ORM\Column]
private ?\DateTimeImmutable $updatedAt = null;
#[ORM\Column(nullable: true)]
#[Groups(['post:collection:read'])]
private ?float $latitude = null;
#[ORM\Column(nullable: true)]
#[Groups(['post:collection:read'])]
private ?float $longitude = null;
#[ORM\Column(nullable: true)]
#[Groups(['post:collection:read'])]
private ?float $altitude = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $image = null;
#[Vich\UploadableField(mapping: 'posts', fileNameProperty: 'image')]
#[Assert\Image]
#[ImageSafety]
private ?File $imageFile = null;
#[ORM\Column(type: Types::TEXT)]
#[Groups(['post:read'])]
#[Assert\NotBlank]
private ?string $commentary = null;
#[ORM\ManyToOne(inversedBy: 'posts')]
#[ApiProperty(readableLink: false)]
#[Groups(['post:collection:read'])]
private ?Species $species = null;
/**
* @var Collection<int, Comment>
*/
#[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'related_post', fetch: 'EXTRA_LAZY')]
private Collection $comments;
/**
* @var Collection<int, User>
*/
#[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'liked_post')]
private Collection $likes;
public function __construct()
{
$this->comments = new ArrayCollection();
$this->likes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFoundDate(): ?\DateTimeImmutable
{
return $this->foundDate;
}
public function setFoundDate(\DateTimeImmutable $foundDate): static
{
$this->foundDate = $foundDate;
return $this;
}
public function getPublicationDate(): ?\DateTimeImmutable
{
return $this->publicationDate;
}
public function setPublicationDate(\DateTimeImmutable $publicationDate): static
{
$this->publicationDate = $publicationDate;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeImmutable $updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getLatitude(): ?float
{
return $this->latitude;
}
public function setLatitude(?float $latitude): static
{
$this->latitude = $latitude;
return $this;
}
public function getLongitude(): ?float
{
return $this->longitude;
}
public function setLongitude(?float $longitude): static
{
$this->longitude = $longitude;
return $this;
}
public function getAltitude(): ?float
{
return $this->altitude;
}
public function setAltitude(?float $altitude): static
{
$this->altitude = $altitude;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): static
{
$this->image = $image;
return $this;
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageFile(?File $imageFile): static
{
$this->imageFile = $imageFile;
if ($imageFile !== null) {
$this->updatedAt = new \DateTimeImmutable();
}
return $this;
}
public function getCommentary(): ?string
{
return $this->commentary;
}
public function setCommentary(string $commentary): static
{
$this->commentary = $commentary;
return $this;
}
public function getSpecies(): ?Species
{
return $this->species;
}
public function setSpecies(?Species $species): static
{
$this->species = $species;
return $this;
}
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function setPublicationDateValue(): void
{
if ($this->publicationDate === null) {
$this->publicationDate = new \DateTimeImmutable();
}
$this->updatedAt = new \DateTimeImmutable();
}
/**
* @return Collection<int, Comment>
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): static
{
if (!$this->comments->contains($comment)) {
$this->comments->add($comment);
$comment->setRelatedPost($this);
}
return $this;
}
public function removeComment(Comment $comment): static
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getRelatedPost() === $this) {
$comment->setRelatedPost(null);
}
}
return $this;
}
/**
* @return Collection<int, User>
*/
public function getLikes(): Collection
{
return $this->likes;
}
public function addLike(User $user): static
{
if (!$this->likes->contains($user)) {
$this->likes->add($user);
}
return $this;
}
public function removeLike(User $user): static
{
$this->likes->removeElement($user);
return $this;
}
}