<?php declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\{Annotation\ApiProperty, Annotation\ApiResource, Annotation\ApiSubresource};
use App\Entity\Embed\Person;
use App\Interfaces\{SluggableInterface, TimestampableInterface, UuidKeyInterface};
use App\Traits\{SlugAsIdTrait, TimestampableEntity};
use Doctrine\Common\Collections\{ArrayCollection, Collection};
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ApiResource(
collectionOperations: [
'get',
'post' => ['security' => "is_granted('ROLE_MANAGER')"],
],
itemOperations: [
'get',
'put' => ['security' => "is_granted('ROLE_MANAGER') or user == object.user"],
'delete' => ['security' => "is_granted('ROLE_MANAGER') or user == object.user"],
],
)]
#[ORM\Entity]
class Profile implements UuidKeyInterface, SluggableInterface
{
use SlugAsIdTrait;
use TimestampableEntity;
#[Gedmo\Slug(fields: ['person.firstName', 'person.lastName'], updatable: true, separator: '-')]
#[ApiProperty(iri: 'https://schema.org/identifier', identifier: true)]
#[ORM\Column(type: 'string', length: 255, unique: true)]
protected ?string $slug = null;
#[ApiProperty(
attributes: [
'openapi_context' => [
'firstName' => ['type' => 'string', 'example' => 'Thomas'],
'lastName' => ['type' => 'string', 'example' => 'Andersen'],
'surname' => ['type' => 'string'],
'gender' => ['type' => 'string', 'enum' => ['Male', 'Feemale'], 'example' => 'Feemale'],
'birthDate' => ['type' => 'string', 'format' => 'date'],
'jobTitle' => ['type' => 'string', 'example' => 'Programmer'],
'city' => ['type' => 'string', 'example' => 'Moscow'],
],
],
)]
#[Groups(['Profile:read'])]
#[ORM\Embedded(class: Person::class)]
private Person $person;
#[Assert\Valid]
#[Groups(['Profile:read'])]
#[ORM\OneToOne(targetEntity: User::class, inversedBy: 'profile', cascade: ['persist', 'remove'])]
private ?User $user = null;
/**
* @var Collection<int, Contact>
*/
#[ApiSubresource]
#[Groups(['Profile:read'])]
#[ORM\OneToMany(targetEntity: 'App\Entity\Contact', mappedBy: 'profile', cascade: ['all'])]
private Collection $contacts;
public function __construct()
{
$this->contacts = new ArrayCollection();
$this->person = new Person();
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
if ($user === null) {
return $this;
}
if ($this !== $user->getProfile()) {
$user->setProfile($this);
}
return $this;
}
public function getContacts(): Collection
{
return $this->contacts;
}
public function addContact(Contact $contact): self
{
if (!$this->contacts->contains($contact)) {
$this->contacts[] = $contact;
$contact->setProfile($this);
}
return $this;
}
public function removeContact(Contact $contact): self
{
if ($this->contacts->contains($contact)) {
$this->contacts->removeElement($contact);
if ($contact->getProfile() === $this) {
$contact->setProfile(null);
}
}
return $this;
}
public function getPerson(): Person
{
return $this->person;
}
public function setPerson(Person $person): self
{
$this->person = $person;
return $this;
}
}