centers = new ArrayCollection(); $this->categories = new ArrayCollection(); $this->children = new ArrayCollection(); } /** * @return string */ public function __toString() { return $this->getName(); } /** * Add a child and set the child as active. * * Method used in conjonction with getActiveChildren in form. * * @internal use the method addChild * * @return $this */ public function addActiveChild(ThirdParty $child): self { $child->setActive(true); return $this->addChild($child); } /** * @return $this */ public function addCategory(ThirdPartyCategory $category): self { if (!$this->categories->contains($category)) { $this->categories[] = $category; } foreach ($this->children as $child) { $child->addCategory($category); } return $this; } public function addCenter(Center $center) { if (false === $this->centers->contains($center)) { $this->centers->add($center); } } /** * @return $this */ public function addChild(ThirdParty $child): self { $this->children[] = $child; $child->setParent($this)->setKind(ThirdParty::KIND_CHILD); return $this; } public function addThirdPartyTypes(?string $type): self { if (null === $type) { return $this; } if (!in_array($type, $this->thirdPartyTypes ?? [], true)) { $this->thirdPartyTypes[] = $type; } foreach ($this->children as $child) { $child->addThirdPartyTypes($type); } return $this; } public function addTypesAndCategories($typeAndCategory): self { if ($typeAndCategory instanceof ThirdPartyCategory) { $this->addCategory($typeAndCategory); return $this; } if (is_string($typeAndCategory)) { $this->addThirdPartyTypes($typeAndCategory); return $this; } throw new UnexpectedValueException(sprintf( 'typeAndCategory should be a string or a %s', ThirdPartyCategory::class )); } /** * @return string */ public function getAcronym(): ?string { return $this->acronym; } public function getActive(): bool { return $this->active; } /** * Get the children where active = true. */ public function getActiveChildren(): Collection { return $this->children->filter(static fn (ThirdParty $tp) => $tp->getActive()); } public function getAddress(): ?Address { if ($this->isChild()) { return $this->getParent()->getAddress(); } return $this->address; } public function getCategories(): Collection { return $this->categories; } public function getCenters(): Collection { return $this->centers; } public function getChildren(): Collection { return $this->children; } public function getCivility(): ?Civility { return $this->civility; } /** * Get comment. * * @return string|null */ public function getComment() { return $this->comment; } public function getCreatedAt(): DateTimeImmutable { return $this->createdAt; } /** * Get email. */ public function getEmail(): ?string { return $this->email; } public function getFirstname(): string { return $this->firstname; } public function getId(): ?int { return $this->id; } public function getKind(): ?string { return $this->kind; } public function getName(): string { return $this->name; } public function getNameCompany(): ?string { return $this->nameCompany; } public function getParent(): ?ThirdParty { return $this->parent; } /** * @return ThirdPartyProfession */ public function getProfession(): ?ThirdPartyProfession { return $this->profession; } /** * Get telephone. */ public function getTelephone(): ?PhoneNumber { return $this->telephone; } /** * Get type. * * @return array|null */ public function getThirdPartyTypes() { return $this->thirdPartyTypes ?? []; } public function getTypesAndCategories(): array { return array_merge( $this->getCategories()->toArray(), $this->getThirdPartyTypes() ?? [] ); } /** * @return DateTime|null */ public function getUpdatedAt(): ?DateTimeImmutable { return $this->updatedAt; } public function getUpdatedBy(): ?User { return $this->updatedBy; } /** * @Groups({"read", "docgen:read", "docgen:read:3party:parent"}) */ public function isChild(): bool { return null !== $this->parent; } public function isContactDataAnonymous(): bool { return $this->contactDataAnonymous; } /** * Mechanism to differentiate children and parents. */ public function isLeaf(): bool { return $this->children->count() !== 0; } public function isParent(): bool { return !$this->isChild(); } /** * mark the child as unactive, but keep the child existing in the * database. To effectively remove the child, use removeChild instead. * * @return $this */ public function removeActiveChild(ThirdParty $child): self { $child->setActive(false); return $this; } /** * @return $this */ public function removeCategory(ThirdPartyCategory $category): self { $this->categories->removeElement($category); foreach ($this->children as $child) { $child->removeCategory($category); } return $this; } public function removeCenter(Center $center) { if ($this->centers->contains($center)) { $this->centers->removeElement($center); } } /** * Remove the child from the database. * * If you want to keep the child into the database * but desactivate it, use removeActiveChildren instead. * * @return $this */ public function removeChild(ThirdParty $child): self { $this->children->removeElement($child); return $this; } public function removeThirdPartyTypes(?string $type): self { if (null === $type) { return $this; } if (in_array($type, $this->thirdPartyTypes ?? [], true)) { $this->thirdPartyTypes = array_filter($this->thirdPartyTypes, fn ($e) => !in_array($e, $this->thirdPartyTypes, true)); } foreach ($this->children as $child) { $child->removeThirdPartyTypes($type); } return $this; } public function removeTypesAndCategories($typeAndCategory): self { if ($typeAndCategory instanceof ThirdPartyCategory) { $this->removeCategory($typeAndCategory); return $this; } if (is_string($typeAndCategory)) { $this->removeThirdPartyTypes($typeAndCategory); return $this; } throw new UnexpectedValueException(sprintf( 'typeAndCategory should be a string or a %s', ThirdPartyCategory::class )); } /** * @param string $acronym * * @return $this */ public function setAcronym(?string $acronym = null): ThirdParty { $this->acronym = (string) $acronym; return $this; } /** * @return $this */ public function setActive(bool $active) { $this->active = $active; foreach ($this->children as $child) { $child->setActive($active); } return $this; } /** * @return $this */ public function setAddress(?Address $address = null) { $this->address = $address; return $this; } /** * @return $this */ public function setCenters(Collection $centers) { foreach ($centers as $center) { $this->addCenter($center); } foreach ($this->centers as $center) { if (false === $centers->contains($center)) { $this->removeCenter($center); } } return $this; } /** * @return $this */ public function setCivility(?Civility $civility): ThirdParty { $this->civility = $civility; return $this; } /** * Set comment. * * @param string|null $comment * * @return ThirdParty */ public function setComment($comment = null) { $this->comment = $comment; return $this; } public function setContactDataAnonymous(bool $contactDataAnonymous): ThirdParty { $this->contactDataAnonymous = $contactDataAnonymous; return $this; } /** * @param DateTimeImmutable $createdAt * * @return $this */ public function setCreatedAt(DateTimeInterface $createdAt): ThirdParty { $this->createdAt = $createdAt; return $this; } public function setCreatedBy(User $user): TrackCreationInterface { $this->createdBy = $user; return $this; } /** * Set email. * * @param string|null $email * * @return ThirdParty */ public function setEmail($email = null) { $this->email = trim((string) $email); return $this; } public function setFirstname(?string $firstname): self { $this->firstname = trim((string) $firstname); return $this; } public function setKind(?string $kind): ThirdParty { $this->kind = $kind; return $this; } public function setName(?string $name): self { $this->name = (string) $name; return $this; } /** * @param string $nameCompany */ public function setNameCompany(?string $nameCompany): ThirdParty { $this->nameCompany = (string) $nameCompany; return $this; } /** * @return $this */ public function setParent(?ThirdParty $parent): ThirdParty { $this->parent = $parent; return $this; } /** * @return $this */ public function setProfession(?ThirdPartyProfession $profession): ThirdParty { $this->profession = $profession; return $this; } /** * Set telephone. */ public function setTelephone(?PhoneNumber $telephone = null): self { $this->telephone = $telephone; return $this; } /** * Set type. * * @return ThirdParty */ public function setThirdPartyTypes(?array $type = []) { // remove all keys from the input data $this->thirdPartyTypes = array_values($type); foreach ($this->children as $child) { $child->setThirdPartyTypes($type); } return $this; } public function setTypesAndCategories(array $typesAndCategories): self { $types = array_filter($typesAndCategories, static fn ($item) => !$item instanceof ThirdPartyCategory); $this->setThirdPartyTypes($types); // handle categories foreach ($typesAndCategories as $t) { $this->addTypesAndCategories($t); } $categories = array_filter($typesAndCategories, static fn ($item) => $item instanceof ThirdPartyCategory); $categoriesHashes = array_map(static fn (ThirdPartyCategory $c) => spl_object_hash($c), $categories); foreach ($categories as $c) { $this->addCategory($c); } foreach ($this->getCategories() as $t) { if (!in_array(spl_object_hash($t), $categoriesHashes, true)) { $this->removeCategory($t); } } return $this; } /** * @param DateTimeImmutable $updatedAt * * @return $this */ public function setUpdatedAt(DateTimeInterface $updatedAt): ThirdParty { $this->updatedAt = $updatedAt; return $this; } /** * @return $this */ public function setUpdatedBy(User $updatedBy): ThirdParty { $this->updatedBy = $updatedBy; return $this; } }