Skip to content

Commit

Permalink
Add Entity::CreatableTrait and Entity::MonthelableTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Fortunier committed Sep 16, 2024
1 parent abb9925 commit 1d61f9f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG_add_trait_monthelable_and_created.md
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.
27 changes: 27 additions & 0 deletions src/Entity/CreatableTrait.php
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;
}
}
61 changes: 61 additions & 0 deletions src/Entity/MonthelableTrait.php
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);
}
}

0 comments on commit 1d61f9f

Please sign in to comment.