| 1: | <?php |
| 2: | namespace TIC\DormBundle\Traits; |
| 3: | |
| 4: | use Doctrine\ORM\Mapping as ORM; |
| 5: | |
| 6: | /** |
| 7: | * Gestion automatique des champs createdAt et updatedAt d'une entité Doctrine. |
| 8: | * |
| 9: | * Alternative sans nécessiter ni extension Doctrine, ni bundle tiers, |
| 10: | * tels que Gedmo et StofDoctrineExtensionsBundle. |
| 11: | * @see Gedmo\Timestampable\Traits\TimestampableEntity; |
| 12: | * |
| 13: | * ATTENTION: nécesite d'activer sur la classe de l'entité "@ORM\HasLifecycleCallbacks" |
| 14: | */ |
| 15: | trait EntityTimestampable |
| 16: | { |
| 17: | /** |
| 18: | * Flag permettant de désactiver les actions des méthodes en prePersist et preSave |
| 19: | */ |
| 20: | private $isTimestampable = true; |
| 21: | |
| 22: | /** |
| 23: | * @ORM\Column(type="datetime") |
| 24: | */ |
| 25: | private $createdAt; |
| 26: | |
| 27: | /** |
| 28: | * @ORM\Column(type="datetime", nullable=true) |
| 29: | */ |
| 30: | private $updatedAt; |
| 31: | |
| 32: | // --------------------------------------------------------------------- Custom methods |
| 33: | |
| 34: | /** |
| 35: | * Gestion de l'activation des actions en prePersist et preSave |
| 36: | * |
| 37: | * @param bool $state Nouvel état d'activation (true=actif / false=inactif) |
| 38: | * @return bool Retourne l'état d'activation en cours |
| 39: | */ |
| 40: | public function doTimestampable(?bool $state = null): bool |
| 41: | { |
| 42: | if (null !== $state) $this->isTimestampable = $state; |
| 43: | return $this->isTimestampable; |
| 44: | } |
| 45: | |
| 46: | // --------------------------------------------------------------------- Accessors (getters) |
| 47: | |
| 48: | /** |
| 49: | * Get the creation date. |
| 50: | */ |
| 51: | public function getCreatedAt(): ?\DateTimeInterface |
| 52: | { |
| 53: | return $this->createdAt; |
| 54: | } |
| 55: | |
| 56: | /** |
| 57: | * Get the modification date. |
| 58: | */ |
| 59: | public function getUpdatedAt(): ?\DateTimeInterface |
| 60: | { |
| 61: | return $this->updatedAt; |
| 62: | } |
| 63: | |
| 64: | // --------------------------------------------------------------------- Mutators (setters) |
| 65: | |
| 66: | /** |
| 67: | * Set the creation date. |
| 68: | */ |
| 69: | public function setCreatedAt(?\DateTimeInterface $createdAt): self |
| 70: | { |
| 71: | $this->createdAt = $createdAt; |
| 72: | return $this; |
| 73: | } |
| 74: | |
| 75: | /** |
| 76: | * Set the modification date. |
| 77: | */ |
| 78: | public function setUpdatedAt(?\DateTimeInterface $updatedAt): self |
| 79: | { |
| 80: | $this->updatedAt = $updatedAt; |
| 81: | return $this; |
| 82: | } |
| 83: | |
| 84: | // --------------------------------------------------------------------- Lifecycle callbacks |
| 85: | |
| 86: | /** |
| 87: | * Actions automatiques si l'entité dispose de "@ORM\HasLifecycleCallbacks" |
| 88: | * @ORM\PrePersist |
| 89: | */ |
| 90: | public function createTimestampableValues(): void |
| 91: | { |
| 92: | if (! $this->isTimestampable) return; |
| 93: | $this->createdAt = new \DateTimeImmutable(); |
| 94: | $this->updatedAt = new \DateTimeImmutable(); |
| 95: | } |
| 96: | |
| 97: | /** |
| 98: | * Actions automatiques si l'entité dispose de "@ORM\HasLifecycleCallbacks" |
| 99: | * @ORM\PreUpdate |
| 100: | */ |
| 101: | public function updateTimestampableValues(): void |
| 102: | { |
| 103: | if (! $this->isTimestampable) return; |
| 104: | $this->updatedAt = new \DateTimeImmutable(); |
| 105: | } |
| 106: | |
| 107: | } |
| 108: |