2021-06-04 12:22:17 +02:00

237 lines
5.8 KiB
PHP

<?php
namespace Chill\PersonBundle\Entity\SocialWork;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
/**
* @ORM\Entity
* @ORM\Table(name="chill_person_social_issue")
* @DiscriminatorMap(typeProperty="type", mapping={
* "social_issue"=SocialIssue::class
* })
*/
class SocialIssue
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=SocialIssue::class, inversedBy="children")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity=SocialIssue::class, mappedBy="parent")
*/
private $children;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $desactivationDate;
/**
* @ORM\Column(type="json")
* @Groups({"read"})
*/
private $title = [];
/**
* @ORM\OneToMany(targetEntity=SocialAction::class, mappedBy="issue")
*/
private $socialActions;
public function __construct()
{
$this->children = new ArrayCollection();
$this->socialActions = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getParent(): ?self
{
return $this->parent;
}
public function hasParent(): bool
{
return $this->parent !== null;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection|self[]
*/
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(self $child): self
{
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}
return $this;
}
public function removeChild(self $child): self
{
if ($this->children->removeElement($child)) {
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
/**
* @return Collection|self[] All the descendants (children, children of children, ...)
*/
public function getDescendants(): Collection
{
$descendants = new ArrayCollection();
foreach ($this->getChildren() as $child) {
if(! $descendants->contains($child)) {
$descendants->add($child);
foreach($child->getDescendants() as $descendantsOfChild) {
if(! $descendants->contains($descendantsOfChild)) {
$descendants->add($descendantsOfChild);
}
}
}
}
return $descendants;
}
/**
* @return Collection|self[] All the descendants with the current entity (this)
*/
public function getDescendantsWithThis(): Collection
{
$descendants = $this->getDescendants();
if(! $descendants->contains($this)) {
$descendants->add($this);
}
return $descendants;
}
public function getDesactivationDate(): ?\DateTimeInterface
{
return $this->desactivationDate;
}
public function setDesactivationDate(?\DateTimeInterface $desactivationDate): self
{
$this->desactivationDate = $desactivationDate;
return $this;
}
public function getTitle(): array
{
return $this->title;
}
public function setTitle(array $title): self
{
$this->title = $title;
return $this;
}
/**
* @return Collection|SocialAction[]
*/
public function getSocialActions(): Collection
{
return $this->socialActions;
}
public function addSocialAction(SocialAction $socialAction): self
{
if (!$this->socialActions->contains($socialAction)) {
$this->socialActions[] = $socialAction;
$socialAction->setSocialIssue($this);
}
return $this;
}
public function removeSocialAction(SocialAction $socialAction): self
{
if ($this->socialActions->removeElement($socialAction)) {
// set the owning side to null (unless already changed)
if ($socialAction->getSocialIssue() === $this) {
$socialAction->setSocialIssue(null);
}
}
return $this;
}
/**
* @return Collection|SocialAction[] All the descendant social actions of the entity
*/
public function getDescendantsSocialActions(): Collection
{
$descendantsSocialActions = new ArrayCollection();
foreach ($this->getSocialActions() as $socialAction) {
foreach ($socialAction->getDescendantsWithThis() as $descendant) {
if(! $descendantsSocialActions->contains($descendant)) {
$descendantsSocialActions->add($descendant);
}
}
}
return $descendantsSocialActions;
}
/**
* @return Collection|SocialAction[] All the descendant social actions of all
* the descendants of the entity
*/
public function getRecursiveSocialActions(): Collection
{
$recursiveSocialActions = new ArrayCollection();
foreach ($this->getDescendantsWithThis() as $socialIssue) {
foreach ($socialIssue->getDescendantsSocialActions() as $descendant) {
if(! $recursiveSocialActions->contains($descendant)) {
$recursiveSocialActions->add($descendant);
}
}
}
return $recursiveSocialActions;
}
}