Skip to content

Commit

Permalink
CU-8695wmnkj Add more generic entity traits & interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieu-ducrot committed Sep 23, 2024
1 parent 46c79bb commit 56698f1
Show file tree
Hide file tree
Showing 15 changed files with 375 additions and 3 deletions.
16 changes: 13 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/Entity/ArchivableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Smart\CoreBundle\Entity;

/**
* The archive entity must have the __toString method because his string representation is shown on flash message
*
* @author Mathieu Ducrot <[email protected]>
*/
interface ArchivableInterface extends \Stringable
{
public function getArchivedAt(): ?\DateTimeInterface;

public function setArchivedAt(?\DateTimeInterface $archivedAt): self;

public function isArchived(): bool;
}
32 changes: 32 additions & 0 deletions src/Entity/ArchivableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Smart\CoreBundle\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

trait ArchivableTrait
{
/**
* @ORM\Column(name="archived_at", type="datetime", nullable=true)
*/
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
protected \DateTime|\DateTimeInterface|null $archivedAt = null;

public function getArchivedAt(): ?\DateTimeInterface
{
return $this->archivedAt;
}

public function setArchivedAt(?\DateTimeInterface $archivedAt): self
{
$this->archivedAt = $archivedAt;

return $this;
}

public function isArchived(): bool
{
return $this->getArchivedAt() != null;
}
}
33 changes: 33 additions & 0 deletions src/Entity/CanonicalInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Smart\CoreBundle\Entity;

/**
* Interface to facilitate the unique representation of an entity via its canonical.
*
* Technical documentation:
* - Once implemented on the entity, use its trait together.
* - Add the UniqueEntity attribute to display the error in the form:
* #[UniqueEntity(
* fields: 'canonical',
* errorPath: '%name of the target field where the error will be displayed%',
* message: '%Explicit message that explains on which field the canonical detection applies.%'
* )]
* or if you use annotation
* @UniqueEntity(fields={"canonical"}, errorPath="targetField", message="...")
* - Define the generateCanonical method. Recommendation: use an AsciiSlugger if it must deal with cases and without accents.
*
* @author Mathieu Ducrot <[email protected]>
*/
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;
}
32 changes: 32 additions & 0 deletions src/Entity/CanonicalTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Smart\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @author Mathieu Ducrot <[email protected]>
*/
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;
}
}
12 changes: 12 additions & 0 deletions src/Entity/CodableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Smart\CoreBundle\Entity;

interface CodableInterface
{
public function getCode(): ?string;

public function setCode(?string $code): self;

public function hasCode(): bool;
}
32 changes: 32 additions & 0 deletions src/Entity/CodableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Smart\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

trait CodableTrait
{
/**
* If the code needs to be mandatory then specify it via an #[Assert\Callback] in the entity
* @ORM\Column(type="string", unique=true, nullable=true)
*/
#[ORM\Column(unique: true, nullable: true)]
protected ?string $code = null;

public function getCode(): ?string
{
return $this->code;
}

public function setCode(?string $code): self
{
$this->code = $code;

return $this;
}

public function hasCode(): bool
{
return $this->getCode() != null;
}
}
10 changes: 10 additions & 0 deletions src/Entity/EnableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Smart\CoreBundle\Entity;

interface EnableInterface
{
public function isEnabled(): ?bool;

public function setEnabled(bool $enabled): self;
}
26 changes: 26 additions & 0 deletions src/Entity/EnableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Smart\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

trait EnableTrait
{
/**
* @ORM\Column(type="boolean", options={"default"=1})
*/
#[ORM\Column(options: ["default" => 1])]
protected ?bool $enabled = true;

public function isEnabled(): ?bool
{
return $this->enabled;
}

public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;

return $this;
}
}
13 changes: 13 additions & 0 deletions src/Entity/PositionableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Smart\CoreBundle\Entity;

/**
* @author Mathieu Ducrot <[email protected]>
*/
interface PositionableInterface
{
public function getPosition(): ?int;

public function setPosition(?int $position): self;
}
29 changes: 29 additions & 0 deletions src/Entity/PositionableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Smart\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @author Mathieu Ducrot <[email protected]>
*/
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;
}
}
21 changes: 21 additions & 0 deletions src/Entity/SearchableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Smart\CoreBundle\Entity;

/**
* Allows to automatically add search update treatments in managers
* @author Mathieu Ducrot <[email protected]>
*/
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;
}
26 changes: 26 additions & 0 deletions src/Entity/SearchableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Smart\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

trait SearchableTrait
{
/**
* @ORM\Column(type="string", length=500, nullable=true)
*/
#[ORM\Column(length: 500, nullable: true)]
protected ?string $search = null;

public function getSearch(): ?string
{
return $this->search;
}

public function setSearch(?string $search): self
{
$this->search = substr(strtolower($search), 0, 500);

return $this;
}
}
18 changes: 18 additions & 0 deletions src/Entity/UpdatableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Smart\CoreBundle\Entity;

interface UpdatableInterface
{
public function getUpdatedAt(): \DateTimeInterface;

public function setUpdatedAt(\DateTimeInterface $updatedAt, bool $initIntegerFields = true): void;

public function getUpdatedAtMonth(): ?int;

public function setUpdatedAtMonth(?int $updatedAtMonth): void;

public function getUpdatedAtYear(): ?int;

public function setUpdatedAtYear(?int $updatedAtYear): void;
}
Loading

0 comments on commit 56698f1

Please sign in to comment.