-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Entity::CreatableTrait
and Entity::MonthelableTrait
- Loading branch information
Louis Fortunier
committed
Sep 16, 2024
1 parent
abb9925
commit 1d61f9f
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### Added | ||
`Entity::CreatableTrait` Trait with creation date | ||
`Entity::MonthelableTrait` Trait with numeric representation of a month and his numeric year. Useful for statistics purposes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Smart\CoreBundle\Entity; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
trait CreatableTrait | ||
{ | ||
/** | ||
* @ORM\Column(name="created_at", type="datetime") | ||
*/ | ||
#[ORM\Column(type: Types::DATETIME_MUTABLE)] | ||
private ?\DateTime $createdAt = null; | ||
|
||
public function getCreatedAt(): ?\DateTime | ||
{ | ||
return $this->createdAt; | ||
} | ||
|
||
public function setCreatedAt(?\DateTime $createdAt): static | ||
{ | ||
$this->createdAt = $createdAt; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Smart\CoreBundle\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Smart\CoreBundle\Utils\DateUtils; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
trait MonthelableTrait | ||
{ | ||
/** | ||
* @ORM\Column(type="integer", nullable=true) | ||
* @Assert\Range( | ||
* min = 1, | ||
* max = 12, | ||
* ) | ||
*/ | ||
#[Assert\Range(min: 1, max: 12)] | ||
#[ORM\Column(nullable: true)] | ||
protected ?int $month = null; | ||
|
||
/** | ||
* @ORM\Column(type="integer", nullable=true) | ||
*/ | ||
#[ORM\Column(nullable: true)] | ||
protected ?int $year = null; | ||
|
||
public function getMonth(): int | ||
{ | ||
return $this->month; | ||
} | ||
|
||
public function setMonth(int $month): void | ||
{ | ||
$this->month = $month; | ||
} | ||
|
||
public function getYear(): int | ||
{ | ||
return $this->year; | ||
} | ||
|
||
public function setYear(int $year): void | ||
{ | ||
$this->year = $year; | ||
} | ||
|
||
public function setMonthelableDate(\DateTime $date): void | ||
{ | ||
$this->month = (int) $date->format('n'); | ||
$this->year = (int) $date->format('Y'); | ||
} | ||
|
||
/** | ||
* Return formatted month ex: 01 | ||
*/ | ||
public function getFormattedMonth(): string | ||
{ | ||
return DateUtils::getFormattedDayOrMonth($this->month); | ||
} | ||
} |