src/Entity/Profile.php line 27

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Entity;
  3. use ApiPlatform\Core\{Annotation\ApiPropertyAnnotation\ApiResourceAnnotation\ApiSubresource};
  4. use App\Entity\Embed\Person;
  5. use App\Interfaces\{SluggableInterfaceTimestampableInterfaceUuidKeyInterface};
  6. use App\Traits\{SlugAsIdTraitTimestampableEntity};
  7. use Doctrine\Common\Collections\{ArrayCollectionCollection};
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Gedmo\Mapping\Annotation as Gedmo;
  10. use Symfony\Component\Serializer\Annotation\Groups;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. #[ApiResource(
  13.     collectionOperations: [
  14.         'get',
  15.         'post' => ['security' => "is_granted('ROLE_MANAGER')"],
  16.     ],
  17.     itemOperations: [
  18.         'get',
  19.         'put' => ['security' => "is_granted('ROLE_MANAGER') or user == object.user"],
  20.         'delete' => ['security' => "is_granted('ROLE_MANAGER') or user == object.user"],
  21.     ],
  22. )]
  23. #[ORM\Entity]
  24. class Profile implements UuidKeyInterfaceSluggableInterface
  25. {
  26.     use SlugAsIdTrait;
  27.     use TimestampableEntity;
  28.     #[Gedmo\Slug(fields: ['person.firstName''person.lastName'], updatabletrueseparator'-')]
  29.     #[ApiProperty(iri'https://schema.org/identifier'identifiertrue)]
  30.     #[ORM\Column(type'string'length255uniquetrue)]
  31.     protected ?string $slug null;
  32.     #[ApiProperty(
  33.         attributes: [
  34.             'openapi_context' => [
  35.                 'firstName' => ['type' => 'string''example' => 'Thomas'],
  36.                 'lastName' => ['type' => 'string''example' => 'Andersen'],
  37.                 'surname' => ['type' => 'string'],
  38.                 'gender' => ['type' => 'string''enum' => ['Male''Feemale'], 'example' => 'Feemale'],
  39.                 'birthDate' => ['type' => 'string''format' => 'date'],
  40.                 'jobTitle' => ['type' => 'string''example' => 'Programmer'],
  41.                 'city' => ['type' => 'string''example' => 'Moscow'],
  42.             ],
  43.         ],
  44.     )]
  45.     #[Groups(['Profile:read'])]
  46.     #[ORM\Embedded(class: Person::class)]
  47.     private Person $person;
  48.     #[Assert\Valid]
  49.     #[Groups(['Profile:read'])]
  50.     #[ORM\OneToOne(targetEntityUser::class, inversedBy'profile'cascade: ['persist''remove'])]
  51.     private ?User $user null;
  52.     /**
  53.      * @var Collection<int, Contact>
  54.      */
  55.     #[ApiSubresource]
  56.     #[Groups(['Profile:read'])]
  57.     #[ORM\OneToMany(targetEntity'App\Entity\Contact'mappedBy'profile'cascade: ['all'])]
  58.     private Collection $contacts;
  59.     public function __construct()
  60.     {
  61.         $this->contacts = new ArrayCollection();
  62.         $this->person = new Person();
  63.     }
  64.     public function getUser(): ?User
  65.     {
  66.         return $this->user;
  67.     }
  68.     public function setUser(?User $user): self
  69.     {
  70.         $this->user $user;
  71.         if ($user === null) {
  72.             return $this;
  73.         }
  74.         if ($this !== $user->getProfile()) {
  75.             $user->setProfile($this);
  76.         }
  77.         return $this;
  78.     }
  79.     public function getContacts(): Collection
  80.     {
  81.         return $this->contacts;
  82.     }
  83.     public function addContact(Contact $contact): self
  84.     {
  85.         if (!$this->contacts->contains($contact)) {
  86.             $this->contacts[] = $contact;
  87.             $contact->setProfile($this);
  88.         }
  89.         return $this;
  90.     }
  91.     public function removeContact(Contact $contact): self
  92.     {
  93.         if ($this->contacts->contains($contact)) {
  94.             $this->contacts->removeElement($contact);
  95.             if ($contact->getProfile() === $this) {
  96.                 $contact->setProfile(null);
  97.             }
  98.         }
  99.         return $this;
  100.     }
  101.     public function getPerson(): Person
  102.     {
  103.         return $this->person;
  104.     }
  105.     public function setPerson(Person $person): self
  106.     {
  107.         $this->person $person;
  108.         return $this;
  109.     }
  110. }