Loading src/UnicaenUtilisateur/Entity/Db/HistoriqueAwareTrait.php +45 −160 Original line number Diff line number Diff line Loading @@ -3,201 +3,95 @@ namespace UnicaenUtilisateur\Entity\Db; use DateTime; use Exception; use UnicaenApp\Exception\RuntimeException; /** * Code commun aux entités possédant une gestion d'historique. * Adaptation de \UnicaenApp\Entity\Db\HistoriqueAwareTrait pour utiliser la UserInterface de UnicaenUtilisateur * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr> */ trait HistoriqueAwareTrait { /** * @var DateTime */ protected $histoCreation; protected ?DateTime $histoCreation = null; protected ?DateTime $histoModification = null; protected ?DateTime $histoDestruction = null; protected ?UserInterface $histoCreateur = null; protected ?UserInterface $histoModificateur = null; protected ?UserInterface $histoDestructeur = null; /** * @var DateTime */ protected $histoModification; /** * @var DateTime */ protected $histoDestruction; /** * @var UserInterface */ protected $histoCreateur; /** * @var UserInterface */ protected $histoModificateur; /** * @var UserInterface */ protected $histoDestructeur; /** * Set histoCreation * * @param DateTime $histoCreation * * @return self */ public function setHistoCreation($histoCreation) public function setHistoCreation(?DateTime $histoCreation): HistoriqueAwareInterface { $this->histoCreation = $histoCreation; return $this; } /** * Get histoCreation * * @return DateTime */ public function getHistoCreation() public function getHistoCreation(): ?DateTime { return $this->histoCreation; } /** * Set histoDestruction * * @param DateTime $histoDestruction * * @return self */ public function setHistoDestruction($histoDestruction) public function setHistoDestruction(?DateTime $histoDestruction): HistoriqueAwareInterface { $this->histoDestruction = $histoDestruction; return $this; } /** * Get histoDestruction * * @return DateTime */ public function getHistoDestruction() public function getHistoDestruction(): ?DateTime { return $this->histoDestruction; } /** * Set histoModification * * @param DateTime $histoModification * * @return self */ public function setHistoModification($histoModification) public function setHistoModification(?DateTime $histoModification): HistoriqueAwareInterface { $this->histoModification = $histoModification; return $this; } /** * Get histoModification * * @return DateTime */ public function getHistoModification() public function getHistoModification(): ?DateTime { return $this->histoModification; } /** * Set histoModificateur * * @param UserInterface $histoModificateur * * @return self */ public function setHistoModificateur(UserInterface $histoModificateur = null) public function setHistoModificateur(?UserInterface $histoModificateur = null): HistoriqueAwareInterface { $this->histoModificateur = $histoModificateur; return $this; } /** * Get histoModificateur * * @return UserInterface */ public function getHistoModificateur() public function getHistoModificateur(): ?UserInterface { return $this->histoModificateur; } /** * Set histoDestructeur * * @param UserInterface $histoDestructeur * * @return self */ public function setHistoDestructeur(UserInterface $histoDestructeur = null) public function setHistoDestructeur(?UserInterface $histoDestructeur = null): HistoriqueAwareInterface { $this->histoDestructeur = $histoDestructeur; return $this; } /** * Get histoDestructeur * * @return UserInterface */ public function getHistoDestructeur() public function getHistoDestructeur(): ?UserInterface { return $this->histoDestructeur; } /** * Set histoCreateur * * @param UserInterface $histoCreateur * * @return self */ public function setHistoCreateur(UserInterface $histoCreateur = null) public function setHistoCreateur(?UserInterface $histoCreateur = null): HistoriqueAwareInterface { $this->histoCreateur = $histoCreateur; return $this; } /** * Get histoCreateur * * @return UserInterface */ public function getHistoCreateur() public function getHistoCreateur(): ?UserInterface { return $this->histoCreateur; } /** Historisation / Restauration **********************************************************************************/ /** * Marque cet enregistrement comme historisé. * * @param UserInterface|null $destructeur Auteur de la suppression ; si null, peut être renseigné * automatiquement (cf. HistoriqueListener) si la classe implémente * HistoriqueAwareInterface * @param DateTime|null $dateDestruction Date éventuelle de la suppression * @return $this * @return HistoriqueAwareInterface * @see HistoriqueAwareInterface * */ public function historiser(UserInterface $destructeur = null, DateTime $dateDestruction = null) public function historiser(?UserInterface $destructeur = null, ?DateTime $dateDestruction = null): HistoriqueAwareInterface { if ($destructeur) { $this->setHistoDestructeur($destructeur); Loading @@ -206,7 +100,7 @@ trait HistoriqueAwareTrait if (empty($dateDestruction)) { try { $dateDestruction = new DateTime(); } catch (\Exception $e) { } catch (Exception $e) { throw new RuntimeException("Impossible d'instancier un DateTime!", null, $e); } } Loading @@ -216,50 +110,36 @@ trait HistoriqueAwareTrait return $this; } /** * Annule l'historisation de cet enregistrement. * * @return $this * @see HistoriqueAwareInterface */ public function dehistoriser() public function dehistoriser(): HistoriqueAwareInterface { $this->setHistoDestructeur(null); $this->setHistoDestructeur(); $this->setHistoDestruction(null); return $this; } /** * Retourne true si l'entité est historisée. * * @param DateTime|null $dateObs Date d'observation éventuelle * @return bool */ public function estHistorise(DateTime $dateObs = null) public function estHistorise(DateTime $dateObs = null): bool { return !$this->estNonHistorise($dateObs); } /** * Retourne true si l'entité n'est *pas* historisée. * * @param DateTime|null $dateObs Date d'observation éventuelle * @return bool */ public function estNonHistorise(DateTime $dateObs = null) public function estNonHistorise(DateTime $dateObs = null): bool { if (empty($dateObs)) { // try { $dateObs = new DateTime(); // } catch (\Exception $e) { // throw new RuntimeException("Impossible d'instancier un DateTime!", null, $e); // } } $dObs = $dateObs->format('Y-m-d'); $dDeb = $this->getHistoCreation() ? $this->getHistoCreation()->format('Y-m-d') : null; $dFin = $this->getHistoDestruction() ? $this->getHistoDestruction()->format('Y-m-d') : null; $dDeb = $this->getHistoCreation()?->format('Y-m-d'); $dFin = $this->getHistoDestruction()?->format('Y-m-d'); if ($dDeb && !($dDeb <= $dObs)) return false; if ($dFin && !($dObs < $dFin)) return false; Loading @@ -281,8 +161,13 @@ trait HistoriqueAwareTrait return "Aucune donnée d'historisation"; } public function getDate(): string public function getDateTime(): string { return $this->getHistoCreation()->format('d/m/Y à H:i'); } public function getDate(): string { return $this->getHistoCreation()->format('d/m/Y'); } } No newline at end of file Loading
src/UnicaenUtilisateur/Entity/Db/HistoriqueAwareTrait.php +45 −160 Original line number Diff line number Diff line Loading @@ -3,201 +3,95 @@ namespace UnicaenUtilisateur\Entity\Db; use DateTime; use Exception; use UnicaenApp\Exception\RuntimeException; /** * Code commun aux entités possédant une gestion d'historique. * Adaptation de \UnicaenApp\Entity\Db\HistoriqueAwareTrait pour utiliser la UserInterface de UnicaenUtilisateur * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr> */ trait HistoriqueAwareTrait { /** * @var DateTime */ protected $histoCreation; protected ?DateTime $histoCreation = null; protected ?DateTime $histoModification = null; protected ?DateTime $histoDestruction = null; protected ?UserInterface $histoCreateur = null; protected ?UserInterface $histoModificateur = null; protected ?UserInterface $histoDestructeur = null; /** * @var DateTime */ protected $histoModification; /** * @var DateTime */ protected $histoDestruction; /** * @var UserInterface */ protected $histoCreateur; /** * @var UserInterface */ protected $histoModificateur; /** * @var UserInterface */ protected $histoDestructeur; /** * Set histoCreation * * @param DateTime $histoCreation * * @return self */ public function setHistoCreation($histoCreation) public function setHistoCreation(?DateTime $histoCreation): HistoriqueAwareInterface { $this->histoCreation = $histoCreation; return $this; } /** * Get histoCreation * * @return DateTime */ public function getHistoCreation() public function getHistoCreation(): ?DateTime { return $this->histoCreation; } /** * Set histoDestruction * * @param DateTime $histoDestruction * * @return self */ public function setHistoDestruction($histoDestruction) public function setHistoDestruction(?DateTime $histoDestruction): HistoriqueAwareInterface { $this->histoDestruction = $histoDestruction; return $this; } /** * Get histoDestruction * * @return DateTime */ public function getHistoDestruction() public function getHistoDestruction(): ?DateTime { return $this->histoDestruction; } /** * Set histoModification * * @param DateTime $histoModification * * @return self */ public function setHistoModification($histoModification) public function setHistoModification(?DateTime $histoModification): HistoriqueAwareInterface { $this->histoModification = $histoModification; return $this; } /** * Get histoModification * * @return DateTime */ public function getHistoModification() public function getHistoModification(): ?DateTime { return $this->histoModification; } /** * Set histoModificateur * * @param UserInterface $histoModificateur * * @return self */ public function setHistoModificateur(UserInterface $histoModificateur = null) public function setHistoModificateur(?UserInterface $histoModificateur = null): HistoriqueAwareInterface { $this->histoModificateur = $histoModificateur; return $this; } /** * Get histoModificateur * * @return UserInterface */ public function getHistoModificateur() public function getHistoModificateur(): ?UserInterface { return $this->histoModificateur; } /** * Set histoDestructeur * * @param UserInterface $histoDestructeur * * @return self */ public function setHistoDestructeur(UserInterface $histoDestructeur = null) public function setHistoDestructeur(?UserInterface $histoDestructeur = null): HistoriqueAwareInterface { $this->histoDestructeur = $histoDestructeur; return $this; } /** * Get histoDestructeur * * @return UserInterface */ public function getHistoDestructeur() public function getHistoDestructeur(): ?UserInterface { return $this->histoDestructeur; } /** * Set histoCreateur * * @param UserInterface $histoCreateur * * @return self */ public function setHistoCreateur(UserInterface $histoCreateur = null) public function setHistoCreateur(?UserInterface $histoCreateur = null): HistoriqueAwareInterface { $this->histoCreateur = $histoCreateur; return $this; } /** * Get histoCreateur * * @return UserInterface */ public function getHistoCreateur() public function getHistoCreateur(): ?UserInterface { return $this->histoCreateur; } /** Historisation / Restauration **********************************************************************************/ /** * Marque cet enregistrement comme historisé. * * @param UserInterface|null $destructeur Auteur de la suppression ; si null, peut être renseigné * automatiquement (cf. HistoriqueListener) si la classe implémente * HistoriqueAwareInterface * @param DateTime|null $dateDestruction Date éventuelle de la suppression * @return $this * @return HistoriqueAwareInterface * @see HistoriqueAwareInterface * */ public function historiser(UserInterface $destructeur = null, DateTime $dateDestruction = null) public function historiser(?UserInterface $destructeur = null, ?DateTime $dateDestruction = null): HistoriqueAwareInterface { if ($destructeur) { $this->setHistoDestructeur($destructeur); Loading @@ -206,7 +100,7 @@ trait HistoriqueAwareTrait if (empty($dateDestruction)) { try { $dateDestruction = new DateTime(); } catch (\Exception $e) { } catch (Exception $e) { throw new RuntimeException("Impossible d'instancier un DateTime!", null, $e); } } Loading @@ -216,50 +110,36 @@ trait HistoriqueAwareTrait return $this; } /** * Annule l'historisation de cet enregistrement. * * @return $this * @see HistoriqueAwareInterface */ public function dehistoriser() public function dehistoriser(): HistoriqueAwareInterface { $this->setHistoDestructeur(null); $this->setHistoDestructeur(); $this->setHistoDestruction(null); return $this; } /** * Retourne true si l'entité est historisée. * * @param DateTime|null $dateObs Date d'observation éventuelle * @return bool */ public function estHistorise(DateTime $dateObs = null) public function estHistorise(DateTime $dateObs = null): bool { return !$this->estNonHistorise($dateObs); } /** * Retourne true si l'entité n'est *pas* historisée. * * @param DateTime|null $dateObs Date d'observation éventuelle * @return bool */ public function estNonHistorise(DateTime $dateObs = null) public function estNonHistorise(DateTime $dateObs = null): bool { if (empty($dateObs)) { // try { $dateObs = new DateTime(); // } catch (\Exception $e) { // throw new RuntimeException("Impossible d'instancier un DateTime!", null, $e); // } } $dObs = $dateObs->format('Y-m-d'); $dDeb = $this->getHistoCreation() ? $this->getHistoCreation()->format('Y-m-d') : null; $dFin = $this->getHistoDestruction() ? $this->getHistoDestruction()->format('Y-m-d') : null; $dDeb = $this->getHistoCreation()?->format('Y-m-d'); $dFin = $this->getHistoDestruction()?->format('Y-m-d'); if ($dDeb && !($dDeb <= $dObs)) return false; if ($dFin && !($dObs < $dFin)) return false; Loading @@ -281,8 +161,13 @@ trait HistoriqueAwareTrait return "Aucune donnée d'historisation"; } public function getDate(): string public function getDateTime(): string { return $this->getHistoCreation()->format('d/m/Y à H:i'); } public function getDate(): string { return $this->getHistoCreation()->format('d/m/Y'); } } No newline at end of file