X-Git-Url: https://git.rapsys.eu/airbundle/blobdiff_plain/05b55f89bc508b0d6c43c4d854fb5e9ffdf6484c..362c4f90cf3eb7731bfdfc23cb9652db0430c058:/Entity/Dance.php diff --git a/Entity/Dance.php b/Entity/Dance.php index c269f32..888a568 100644 --- a/Entity/Dance.php +++ b/Entity/Dance.php @@ -1,16 +1,17 @@ + * (c) Raphaël Gertz * - * for the full copyright and license information, please view the license + * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Rapsys\AirBundle\Entity; +use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Event\PreUpdateEventArgs; @@ -19,42 +20,40 @@ use Doctrine\ORM\Event\PreUpdateEventArgs; */ class Dance { /** - * @var integer + * Primary key */ - private $id; + private ?int $id = null; /** - * @var string + * Create datetime */ - protected $title; + private \DateTimeImmutable $created; /** - * @var \DateTime + * Update datetime */ - private $created; + private \DateTimeImmutable $updated; /** - * @var \DateTime + * Applications collection */ - private $updated; + private Collection $applications; /** - * @var ArrayCollection + * Users collection */ - private $applications; - - /** - * @var ArrayCollection - */ - private $users; + private Collection $users; /** * Constructor + * + * @param string $name The dance name + * @param string $type The dance type */ - public function __construct() { + public function __construct(private string $name, private string $type) { //Set defaults - $this->created = new \DateTime('now'); - $this->updated = new \DateTime('now'); + $this->created = new \DateTimeImmutable('now'); + $this->updated = new \DateTimeImmutable('now'); //Set collections $this->applications = new ArrayCollection(); @@ -66,40 +65,62 @@ class Dance { * * @return integer */ - public function getId(): int { + public function getId(): ?int { return $this->id; } /** - * Set title + * Set name * - * @param string $title + * @param string $name * * @return Dance */ - public function setTitle(string $title): Dance { - $this->title = $title; + public function setName(string $name): Dance { + $this->name = $name; return $this; } /** - * Get title + * Get name * * @return string */ - public function getTitle(): string { - return $this->title; + public function getName(): string { + return $this->name; + } + + /** + * Set type + * + * @param string $type + * + * @return Dance + */ + public function setType(string $type): Dance { + $this->type = $type; + + return $this; + } + + /** + * Get type + * + * @return string + */ + public function getType(): string { + return $this->type; } /** * Set created * - * @param \DateTime $created + * @param \DateTimeImmutable $created * * @return Dance */ - public function setCreated(\DateTime $created): Dance { + public function setCreated(\DateTimeImmutable $created): Dance { $this->created = $created; return $this; @@ -108,20 +129,20 @@ class Dance { /** * Get created * - * @return \DateTime + * @return \DateTimeImmutable */ - public function getCreated(): \DateTime { + public function getCreated(): \DateTimeImmutable { return $this->created; } /** * Set updated * - * @param \DateTime $updated + * @param \DateTimeImmutable $updated * * @return Dance */ - public function setUpdated(\DateTime $updated): Dance { + public function setUpdated(\DateTimeImmutable $updated): Dance { $this->updated = $updated; return $this; @@ -130,9 +151,9 @@ class Dance { /** * Get updated * - * @return \DateTime + * @return \DateTimeImmutable */ - public function getUpdated(): \DateTime { + public function getUpdated(): \DateTimeImmutable { return $this->updated; } @@ -177,6 +198,9 @@ class Dance { * @return Dance */ public function addUser(User $user): Dance { + //Add from owning side + $user->addDance($this); + $this->users[] = $user; return $this; @@ -190,6 +214,13 @@ class Dance { * @return bool */ public function removeUser(User $user): bool { + if (!$this->dances->contains($user)) { + return true; + } + + //Remove from owning side + $user->removeDance($this); + return $this->users->removeElement($user); } @@ -207,9 +238,9 @@ class Dance { */ public function preUpdate(PreUpdateEventArgs $eventArgs) { //Check that we have a dance instance - if (($dance = $eventArgs->getEntity()) instanceof Dance) { + if (($dance = $eventArgs->getObject()) instanceof Dance) { //Set updated value - $dance->setUpdated(new \DateTime('now')); + $dance->setUpdated(new \DateTimeImmutable('now')); } } @@ -219,6 +250,6 @@ class Dance { * @return string */ public function __toString(): string { - return $this->title; + return $this->name.' '.lcfirst($this->type); } }