<?phpnamespace App\Entity;use App\Entity\Traits\ToArray;use App\Enum\Conversation\Type;use App\Repository\ConversationRepository;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\ORM\Mapping as ORM;/** * @ORM\Entity(repositoryClass=ConversationRepository::class) */class Conversation{ use ToArray; /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="datetime") */ private $createdAt; /** * @ORM\Column(type="enum", options={"values"="conversation_enum"}) */ private $type; /** * @ORM\OneToMany(targetEntity=Member::class, mappedBy="conversation") */ private $members; /** * @ORM\OneToMany(targetEntity=Message::class, mappedBy="conversation", orphanRemoval=true) */ private $messages; /** * @ORM\Column(type="guid", unique=true) */ private $conversationId; public function __construct() { $this->createdAt = new \DateTime(); $this->members = new ArrayCollection(); $this->messages = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getCreatedAt(): ?\DateTimeInterface { return $this->createdAt; } public function setCreatedAt(\DateTimeInterface $createdAt): self { $this->createdAt = $createdAt; return $this; } public function getType() { return $this->type; } public function setType($type): self { $this->type = $type; return $this; } /** * @return Collection<int, Member> */ public function getMembers(): Collection { return $this->members; } public function addMember(Member $member): self { if (!$this->members->contains($member)) { $this->members[] = $member; $member->setConversation($this); } return $this; } public function removeMember(Member $member): self { if ($this->members->removeElement($member)) { // set the owning side to null (unless already changed) if ($member->getConversation() === $this) { $member->setConversation(null); } } return $this; } /** * @return Collection<int, Message> */ public function getMessages(): Collection { return $this->messages; } public function addMessage(Message $message): self { if (!$this->messages->contains($message)) { $this->messages[] = $message; $message->setConversation($this); } return $this; } public function removeMessage(Message $message): self { if ($this->messages->removeElement($message)) { // set the owning side to null (unless already changed) if ($message->getConversation() === $this) { $message->setConversation(null); } } return $this; } public function hasMember(User $user): bool { foreach ($this->members as $member) { if ($member->getUser()->getId() === $user->getId()) { return true; } } return false; } public function getOppositeUser(User $user): ?User { if ($this->getType() !== Type::TYPE_PRIVATE) { return null; } if ($this->members->count() == 1) { return $this->members->first()->getUser(); } foreach ($this->members as $member) { if ($member->getUser()->getId() !== $user->getId()) { return $member->getUser(); } } return null; } public function getTitle(User $sender = null): ?string { if ($this->getType() == Type::TYPE_PRIVATE && $sender && $this->getMembers()->count() > 1) { foreach ($this->getMembers() as $member) { if ($member->getUser()->getId() != $sender->getId()) { return $member->getUser()->getName(); } } return null; } elseif ($this->getType() == Type::TYPE_PRIVATE && $this->getMembers()->count() ==1) { return "Заметки"; } else { return "Группа"; } } public function getConversationId(): ?string { return $this->conversationId; } public function setConversationId(string $conversationId): self { $this->conversationId = $conversationId; return $this; }}