comment = new CommentEmbeddable(); $this->privateComment = new PrivateCommentEmbeddable(); $this->persons = new ArrayCollection(); $this->professionals = new ArrayCollection(); $this->invites = new ArrayCollection(); } /** * @internal Use {@link (Calendar::addUser)} instead */ public function addInvite(Invite $invite): self { if ($invite->getCalendar() instanceof Calendar && $invite->getCalendar() !== $this) { throw new LogicException('Not allowed to move an invitation to another Calendar'); } $this->invites[] = $invite; $this->newInvites[] = $invite; $invite->setCalendar($this); return $this; } public function addPerson(Person $person): self { $this->persons[] = $person; return $this; } public function addProfessional(ThirdParty $professional): self { $this->professionals[] = $professional; return $this; } public function addUser(User $user): self { if (!$this->getUsers()->contains($user) && $this->getMainUser() !== $user) { $this->addInvite((new Invite())->setUser($user)); } return $this; } public function getAccompanyingPeriod(): ?AccompanyingPeriod { return $this->accompanyingPeriod; } public function getActivity(): ?Activity { return $this->activity; } public function getCalendarRange(): ?CalendarRange { return $this->calendarRange; } public function getCancelReason(): ?CancelReason { return $this->cancelReason; } public function getComment(): CommentEmbeddable { return $this->comment; } public function getDuration(): ?DateInterval { if ($this->getStartDate() === null || $this->getEndDate() === null) { return null; } return $this->getStartDate()->diff($this->getEndDate()); } public function getEndDate(): ?DateTimeImmutable { return $this->endDate; } public function getId(): ?int { return $this->id; } public function getInviteForUser(User $user): ?Invite { $criteria = Criteria::create(); $criteria->where(Criteria::expr()->eq('user', $user)); $matchings = $this->invites ->matching($criteria); if (1 === $matchings->count()) { return $matchings->first(); } return null; } /** * @return Collection|Invite[] */ public function getInvites(): Collection { return $this->invites; } public function getLocation(): ?Location { return $this->location; } public function getMainUser(): ?User { return $this->mainUser; } /** * @return Collection|Person[] */ public function getPersons(): Collection { return $this->persons; } public function getPersonsAssociated(): array { if (null !== $this->accompanyingPeriod) { $personsAssociated = []; foreach ($this->accompanyingPeriod->getParticipations() as $participation) { if ($this->persons->contains($participation->getPerson())) { $personsAssociated[] = $participation->getPerson(); } } return $personsAssociated; } return []; } public function getPersonsNotAssociated(): array { if (null !== $this->accompanyingPeriod) { $personsNotAssociated = []; foreach ($this->persons as $person) { if (!in_array($person, $this->getPersonsAssociated(), true)) { $personsNotAssociated[] = $person; } } return $personsNotAssociated; } return []; } public function getPrivateComment(): PrivateCommentEmbeddable { return $this->privateComment; } /** * @return Collection|ThirdParty[] */ public function getProfessionals(): Collection { return $this->professionals; } public function getSendSMS(): ?bool { return $this->sendSMS; } public function getStartDate(): ?DateTimeImmutable { return $this->startDate; } public function getStatus(): ?string { return $this->status; } public function getThirdParties(): Collection { return $this->getProfessionals(); } /** * @return Collection|User[] * @Serializer\Groups({"calendar:read", "read"}) */ public function getUsers(): Collection { return $this->getInvites()->map(static function (Invite $i) { return $i->getUser(); }); } public function hasCalendarRange(): bool { return null !== $this->calendarRange; } /** * return true if the user is invited. */ public function isInvited(User $user): bool { if ($this->getMainUser() === $user) { return false; } return $this->getUsers()->contains($user); } public static function loadValidatorMetadata(ClassMetadata $metadata): void { $metadata->addPropertyConstraint('startDate', new NotBlank()); $metadata->addPropertyConstraint('startDate', new Range([ 'min' => '2 years ago', 'max' => '+ 2 years', ])); $metadata->addPropertyConstraint('endDate', new NotBlank()); $metadata->addPropertyConstraint('endDate', new Range([ 'min' => '2 years ago', 'max' => '+ 2 years', ])); } /** * @internal Use {@link (Calendar::removeUser)} instead */ public function removeInvite(Invite $invite): self { if ($this->invites->removeElement($invite)) { $invite->setCalendar(null); $this->oldInvites[] = $invite; } return $this; } public function removePerson(Person $person): self { $this->persons->removeElement($person); return $this; } public function removeProfessional(ThirdParty $professional): self { $this->professionals->removeElement($professional); return $this; } public function removeUser(User $user): self { if (!$this->getUsers()->contains($user)) { return $this; } $invite = $this->invites ->filter(static function (Invite $invite) use ($user) { return $invite->getUser() === $user; }) ->first(); $this->removeInvite($invite); return $this; } public function setAccompanyingPeriod(?AccompanyingPeriod $accompanyingPeriod): self { $this->accompanyingPeriod = $accompanyingPeriod; return $this; } public function setActivity(?Activity $activity): self { $this->activity = $activity; return $this; } public function setCalendarRange(?CalendarRange $calendarRange): self { if ($this->calendarRange !== $calendarRange) { $this->previousCalendarRange = $this->calendarRange; if (null !== $this->previousCalendarRange) { $this->previousCalendarRange->setCalendar(null); } } $this->calendarRange = $calendarRange; if ($this->calendarRange instanceof CalendarRange) { $this->calendarRange->setCalendar($this); } return $this; } public function setCancelReason(?CancelReason $cancelReason): self { $this->cancelReason = $cancelReason; return $this; } public function setComment(CommentEmbeddable $comment): self { $this->comment = $comment; return $this; } public function setEndDate(DateTimeImmutable $endDate): self { $this->endDate = $endDate; return $this; } public function setLocation(?Location $location): Calendar { $this->location = $location; return $this; } public function setMainUser(?User $mainUser): self { if ($this->mainUser !== $mainUser) { $this->previousMainUser = $this->mainUser; } $this->mainUser = $mainUser; $this->removeUser($mainUser); return $this; } public function setPrivateComment(PrivateCommentEmbeddable $privateComment): self { $this->privateComment = $privateComment; return $this; } public function setSendSMS(?bool $sendSMS): self { $this->sendSMS = $sendSMS; return $this; } public function setStartDate(DateTimeImmutable $startDate): self { $this->startDate = $startDate; return $this; } public function setStatus(string $status): self { $this->status = $status; return $this; } }