From 56698f14b8a4697653776edfaf7dce7ec0a7b992 Mon Sep 17 00:00:00 2001 From: Mathieu Ducrot Date: Mon, 23 Sep 2024 11:57:42 +0200 Subject: [PATCH] CU-8695wmnkj Add more generic entity traits & interfaces --- CHANGELOG.md | 16 ++++++-- src/Entity/ArchivableInterface.php | 17 ++++++++ src/Entity/ArchivableTrait.php | 32 +++++++++++++++ src/Entity/CanonicalInterface.php | 33 +++++++++++++++ src/Entity/CanonicalTrait.php | 32 +++++++++++++++ src/Entity/CodableInterface.php | 12 ++++++ src/Entity/CodableTrait.php | 32 +++++++++++++++ src/Entity/EnableInterface.php | 10 +++++ src/Entity/EnableTrait.php | 26 ++++++++++++ src/Entity/PositionableInterface.php | 13 ++++++ src/Entity/PositionableTrait.php | 29 +++++++++++++ src/Entity/SearchableInterface.php | 21 ++++++++++ src/Entity/SearchableTrait.php | 26 ++++++++++++ src/Entity/UpdatableInterface.php | 18 ++++++++ src/Entity/UpdatableTrait.php | 61 ++++++++++++++++++++++++++++ 15 files changed, 375 insertions(+), 3 deletions(-) create mode 100644 src/Entity/ArchivableInterface.php create mode 100644 src/Entity/ArchivableTrait.php create mode 100644 src/Entity/CanonicalInterface.php create mode 100644 src/Entity/CanonicalTrait.php create mode 100644 src/Entity/CodableInterface.php create mode 100644 src/Entity/CodableTrait.php create mode 100644 src/Entity/EnableInterface.php create mode 100644 src/Entity/EnableTrait.php create mode 100644 src/Entity/PositionableInterface.php create mode 100644 src/Entity/PositionableTrait.php create mode 100644 src/Entity/SearchableInterface.php create mode 100644 src/Entity/SearchableTrait.php create mode 100644 src/Entity/UpdatableInterface.php create mode 100644 src/Entity/UpdatableTrait.php diff --git a/CHANGELOG.md b/CHANGELOG.md index cde9098..25e23cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,22 @@ CHANGELOG for 1.x =================== +## v1.12.0 - (2024-09-23) +### Added +- `ArchivableInterface` & trait +- `CanonicalInterface` & trait +- `CodableInterface` & trait +- `EnableInterface` & trait +- `PositionableInterface` & trait +- `SearchableInterface` & trait +- `UpdatableInterface` & trait + ## v1.11.0 - (2024-09-19) ### Added -`CreatableTrait` Trait & interface with creation date -`MonthYearTrait` Trait & interface with numeric representation of a month and his numeric year. Useful for statistics purposes. +- `CreatableTrait` Trait & interface with creation date +- `MonthYearTrait` Trait & interface with numeric representation of a month and his numeric year. Useful for statistics purposes. ### Changed -`README.md` update : Add missing nelmio security settings configuration (@lfortunier) +- `README.md` update : Add missing nelmio security settings configuration (@lfortunier) ### Fixed - `HistoryLogger` add missing STATUS_PROPERTY check on **log** update skip diff --git a/src/Entity/ArchivableInterface.php b/src/Entity/ArchivableInterface.php new file mode 100644 index 0000000..53308f2 --- /dev/null +++ b/src/Entity/ArchivableInterface.php @@ -0,0 +1,17 @@ + + */ +interface ArchivableInterface extends \Stringable +{ + public function getArchivedAt(): ?\DateTimeInterface; + + public function setArchivedAt(?\DateTimeInterface $archivedAt): self; + + public function isArchived(): bool; +} diff --git a/src/Entity/ArchivableTrait.php b/src/Entity/ArchivableTrait.php new file mode 100644 index 0000000..947c214 --- /dev/null +++ b/src/Entity/ArchivableTrait.php @@ -0,0 +1,32 @@ +archivedAt; + } + + public function setArchivedAt(?\DateTimeInterface $archivedAt): self + { + $this->archivedAt = $archivedAt; + + return $this; + } + + public function isArchived(): bool + { + return $this->getArchivedAt() != null; + } +} diff --git a/src/Entity/CanonicalInterface.php b/src/Entity/CanonicalInterface.php new file mode 100644 index 0000000..704edc2 --- /dev/null +++ b/src/Entity/CanonicalInterface.php @@ -0,0 +1,33 @@ + + */ +interface CanonicalInterface +{ + public function getCanonical(): ?string; + + public function setCanonical(?string $canonical): static; + + /** + * Generates the canonical from the entity, the content must be defined by the developer integrating the interface. + * Can also be used in fixtures to simplify value init + */ + public function generateCanonical(): string; +} diff --git a/src/Entity/CanonicalTrait.php b/src/Entity/CanonicalTrait.php new file mode 100644 index 0000000..aebeef5 --- /dev/null +++ b/src/Entity/CanonicalTrait.php @@ -0,0 +1,32 @@ + + */ +trait CanonicalTrait +{ + /** + * @ORM\Column(type="string", length=500, unique=true, nullable=true) + * @Assert\Length(max=500) + */ + #[ORM\Column(length: 500, unique: true, nullable: true)] + #[Assert\Length(max: 500)] + protected ?string $canonical = null; + + public function getCanonical(): ?string + { + return $this->canonical; + } + + public function setCanonical(?string $canonical): static + { + $this->canonical = $canonical; + + return $this; + } +} diff --git a/src/Entity/CodableInterface.php b/src/Entity/CodableInterface.php new file mode 100644 index 0000000..7af2955 --- /dev/null +++ b/src/Entity/CodableInterface.php @@ -0,0 +1,12 @@ +code; + } + + public function setCode(?string $code): self + { + $this->code = $code; + + return $this; + } + + public function hasCode(): bool + { + return $this->getCode() != null; + } +} diff --git a/src/Entity/EnableInterface.php b/src/Entity/EnableInterface.php new file mode 100644 index 0000000..b853ab2 --- /dev/null +++ b/src/Entity/EnableInterface.php @@ -0,0 +1,10 @@ + 1])] + protected ?bool $enabled = true; + + public function isEnabled(): ?bool + { + return $this->enabled; + } + + public function setEnabled(bool $enabled): self + { + $this->enabled = $enabled; + + return $this; + } +} diff --git a/src/Entity/PositionableInterface.php b/src/Entity/PositionableInterface.php new file mode 100644 index 0000000..183a486 --- /dev/null +++ b/src/Entity/PositionableInterface.php @@ -0,0 +1,13 @@ + + */ +interface PositionableInterface +{ + public function getPosition(): ?int; + + public function setPosition(?int $position): self; +} diff --git a/src/Entity/PositionableTrait.php b/src/Entity/PositionableTrait.php new file mode 100644 index 0000000..f928d5e --- /dev/null +++ b/src/Entity/PositionableTrait.php @@ -0,0 +1,29 @@ + + */ +trait PositionableTrait +{ + /** + * @ORM\Column(type="integer", nullable=true) + */ + #[ORM\Column(nullable: true)] + protected ?int $position = null; + + public function getPosition(): ?int + { + return $this->position; + } + + public function setPosition(?int $position): self + { + $this->position = $position; + + return $this; + } +} diff --git a/src/Entity/SearchableInterface.php b/src/Entity/SearchableInterface.php new file mode 100644 index 0000000..60a04c4 --- /dev/null +++ b/src/Entity/SearchableInterface.php @@ -0,0 +1,21 @@ + + */ +interface SearchableInterface +{ + public function getSearch(): ?string; + + public function setSearch(?string $search): self; + + /** + * returns the calculated value of the search + * method not provided by the SearchableTrait to be defined + * also used in fixtures to simplify the init of the search value + */ + public function getComputedSearch(): string; +} diff --git a/src/Entity/SearchableTrait.php b/src/Entity/SearchableTrait.php new file mode 100644 index 0000000..daffbea --- /dev/null +++ b/src/Entity/SearchableTrait.php @@ -0,0 +1,26 @@ +search; + } + + public function setSearch(?string $search): self + { + $this->search = substr(strtolower($search), 0, 500); + + return $this; + } +} diff --git a/src/Entity/UpdatableInterface.php b/src/Entity/UpdatableInterface.php new file mode 100644 index 0000000..c43f8e4 --- /dev/null +++ b/src/Entity/UpdatableInterface.php @@ -0,0 +1,18 @@ +updatedAt; + } + + public function setUpdatedAt(\DateTimeInterface $updatedAt, bool $initIntegerFields = true): void + { + $this->updatedAt = $updatedAt; + if ($initIntegerFields) { + $this->updatedAtMonth = (int) $updatedAt->format('m'); + $this->updatedAtYear = (int) $updatedAt->format('Y'); + } + } + + public function getUpdatedAtMonth(): ?int + { + return $this->updatedAtMonth; + } + + public function setUpdatedAtMonth(?int $updatedAtMonth): void + { + $this->updatedAtMonth = $updatedAtMonth; + } + + public function getUpdatedAtYear(): ?int + { + return $this->updatedAtYear; + } + + public function setUpdatedAtYear(?int $updatedAtYear): void + { + $this->updatedAtYear = $updatedAtYear; + } +}