$children */ #[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)] private Collection $children; /** * membership of the group. It can be free or paying. */ #[ORM\Column(name: 'membership', type: 'string', nullable: false, enumType: GroupMembership::class)] protected GroupMembership $membership = GroupMembership::FREE; /** * If true, administrators can send invitations. */ #[ORM\Column(type: 'boolean', nullable: false)] private bool $invitationByAdmin = false; /** * @var Collection */ #[ORM\OneToMany(mappedBy: 'group', targetEntity: UserGroup::class, orphanRemoval: true)] private Collection $userGroups; /** * @var Collection */ #[ORM\OneToMany(mappedBy: 'group', targetEntity: GroupOffer::class, orphanRemoval: true)] #[ORM\OrderBy(['price' => 'ASC'])] private Collection $offers; /** * List of visible product in the group. * * @var Collection */ #[ORM\ManyToMany(targetEntity: Product::class, mappedBy: 'groups')] private Collection $products; public function __construct() { $this->children = new ArrayCollection(); $this->userGroups = new ArrayCollection(); $this->offers = new ArrayCollection(); $this->products = new ArrayCollection(); } public function __toString(): string { return $this->name; } public function getId(): Uuid { return $this->id; } public function setId(Uuid $uuid): self { $this->id = $uuid; return $this; } public function getParent(): ?self { return $this->parent; } public function setParent(?self $parent): self { $this->parent = $parent; return $this; } public function getName(): string { return $this->name; } public function setName(string $name): self { $this->name = $name; return $this; } public function setSlug(string $slug): self { $this->slug = $slug; return $this; } public function getSlug(): string { return $this->slug; } public function getType(): GroupType { return $this->type; } public function setType(GroupType $type): Group { $this->type = $type; return $this; } public function getDescription(): ?string { return $this->description; } public function setDescription(?string $description): self { $this->description = $description; return $this; } public function getUrl(): ?string { return $this->url; } public function setUrl(?string $url): self { $this->url = $url; return $this; } /** * @return Collection */ public function getChildren(): Collection { return $this->children; } public function addChild(self $group): self { if (!$this->children->contains($group)) { $this->children->add($group); $group->setParent($this); } return $this; } public function removeChild(self $group): self { // set the owning side to null (unless already changed) if ($this->children->removeElement($group) && $group->getParent() === $this) { $group->setParent(null); } return $this; } public function getMembership(): GroupMembership { return $this->membership; } public function setMembership(GroupMembership $membership): void { $this->membership = $membership; } public function isInvitationByAdmin(): bool { return $this->invitationByAdmin; } public function setInvitationByAdmin(bool $invitationByAdmin): self { $this->invitationByAdmin = $invitationByAdmin; return $this; } /** * @return Collection */ public function getUserGroups(): Collection { return $this->userGroups; } public function addUserGroup(UserGroup $userGroup): self { if (!$this->userGroups->contains($userGroup)) { $this->userGroups->add($userGroup); $userGroup->setGroup($this); } return $this; } public function removeUserGroup(UserGroup $userGroup): self { $this->userGroups->removeElement($userGroup); return $this; } /** * @return Collection */ public function getOffers(): Collection { return $this->offers; } /** * @param Collection $offers */ public function setOffers(Collection $offers): Group { $this->offers = $offers; return $this; } /** * @return Collection */ public function getProducts(): Collection { return $this->products; } public function addProduct(Product $product): self { if (!$this->products->contains($product)) { $this->products->add($product); $product->addGroup($this); } return $this; } public function removeProduct(Product $product): self { if ($this->products->removeElement($product)) { $product->removeGroup($this); } return $this; } // End of basic 'etters ---------------------------------------------------- /** * @return array */ public function getRoutingParameters(): array { return [ 'id' => (string) $this->getId(), 'slug' => $this->getSlug(), ]; } /** * Test if a given user is main admin of the group. */ public function isMainAdmin(User $user): bool { $mainAdminUserGroups = $this->userGroups->filter( static fn (UserGroup $userGroup) => $userGroup->getUser() === $user && $userGroup->isMainAdminAccount() ); return !$mainAdminUserGroups->isEmpty(); } /** * Get active offers only. * * @return Collection */ public function getActiveOffers(): Collection { /** @var Collection $collection */ $collection = $this->offers->filter( static fn (GroupOffer $groupOffer) => $groupOffer->isActive() ); return $collection; } public function hasActiveOffers(): bool { return !$this->getActiveOffers()->isEmpty(); } }