diff --git a/src/DataFixtures/AppFixtures.php b/src/DataFixtures/AppFixtures.php index f5bd9297b..49235a7e0 100644 --- a/src/DataFixtures/AppFixtures.php +++ b/src/DataFixtures/AppFixtures.php @@ -17,6 +17,7 @@ use App\Entity\User; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Persistence\ObjectManager; +use Symfony\Component\Clock\DatePoint; use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\String\AbstractUnicodeString; use Symfony\Component\String\Slugger\SluggerInterface; @@ -85,7 +86,7 @@ private function loadPosts(ObjectManager $manager): void $comment = new Comment(); $comment->setAuthor($this->getReference('john_user', User::class)); $comment->setContent($this->getRandomText(random_int(255, 512))); - $comment->setPublishedAt(new \DateTimeImmutable('now + '.$i.'seconds')); + $comment->setPublishedAt(new DatePoint('now + '.$i.'seconds')); $post->addComment($comment); } @@ -128,7 +129,7 @@ private function getTagData(): array } /** - * @return array}> + * @return array}> * * @throws \Exception */ @@ -143,7 +144,7 @@ private function getPostData(): array $this->slugger->slug($title)->lower(), $this->getRandomText(), $this->getPostContent(), - new \DateTimeImmutable('now - '.$i.'days')->setTime(random_int(8, 17), random_int(7, 49), random_int(0, 59)), + new DatePoint('now - '.$i.'days')->setTime(random_int(8, 17), random_int(7, 49), random_int(0, 59)), // Ensure that the first post is written by Jane Doe to simplify tests $this->getReference(['jane_admin', 'tom_admin'][0 === $i ? 0 : random_int(0, 1)], User::class), $this->getRandomTags(), diff --git a/src/Entity/Comment.php b/src/Entity/Comment.php index 29335751b..34312161c 100644 --- a/src/Entity/Comment.php +++ b/src/Entity/Comment.php @@ -13,6 +13,7 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Clock\DatePoint; use Symfony\Component\Validator\Constraints as Assert; use function Symfony\Component\String\u; @@ -43,8 +44,8 @@ class Comment #[Assert\Length(min: 5, max: 10000, minMessage: 'comment.too_short', maxMessage: 'comment.too_long')] private ?string $content = null; - #[ORM\Column] - private \DateTimeImmutable $publishedAt; + #[ORM\Column(type: 'date_point')] + private DatePoint $publishedAt; #[ORM\ManyToOne(targetEntity: User::class)] #[ORM\JoinColumn(nullable: false)] @@ -52,7 +53,7 @@ class Comment public function __construct() { - $this->publishedAt = new \DateTimeImmutable(); + $this->publishedAt = new DatePoint(); } #[Assert\IsTrue(message: 'comment.is_spam')] @@ -78,12 +79,12 @@ public function setContent(string $content): void $this->content = $content; } - public function getPublishedAt(): \DateTimeImmutable + public function getPublishedAt(): DatePoint { return $this->publishedAt; } - public function setPublishedAt(\DateTimeImmutable $publishedAt): void + public function setPublishedAt(DatePoint $publishedAt): void { $this->publishedAt = $publishedAt; } diff --git a/src/Entity/Post.php b/src/Entity/Post.php index dbb43127d..07587aa8b 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -17,6 +17,7 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; +use Symfony\Component\Clock\DatePoint; use Symfony\Component\Validator\Constraints as Assert; /** @@ -55,8 +56,8 @@ class Post #[Assert\Length(min: 10, minMessage: 'post.too_short_content')] private ?string $content = null; - #[ORM\Column] - private \DateTimeImmutable $publishedAt; + #[ORM\Column(type: 'date_point')] + private DatePoint $publishedAt; #[ORM\ManyToOne(targetEntity: User::class)] #[ORM\JoinColumn(nullable: false)] @@ -80,7 +81,7 @@ class Post public function __construct() { - $this->publishedAt = new \DateTimeImmutable(); + $this->publishedAt = new DatePoint(); $this->comments = new ArrayCollection(); $this->tags = new ArrayCollection(); } @@ -120,12 +121,12 @@ public function setContent(?string $content): void $this->content = $content; } - public function getPublishedAt(): \DateTimeImmutable + public function getPublishedAt(): DatePoint { return $this->publishedAt; } - public function setPublishedAt(\DateTimeImmutable $publishedAt): void + public function setPublishedAt(DatePoint $publishedAt): void { $this->publishedAt = $publishedAt; } diff --git a/src/Form/Type/DateTimePickerType.php b/src/Form/Type/DateTimePickerType.php index 8c6f6040f..f6355df88 100644 --- a/src/Form/Type/DateTimePickerType.php +++ b/src/Form/Type/DateTimePickerType.php @@ -30,7 +30,7 @@ public function configureOptions(OptionsResolver $resolver): void // @see https://symfony.com/doc/current/reference/forms/types/date.html#rendering-a-single-html5-text-box $resolver->setDefaults([ 'widget' => 'single_text', - 'input' => 'datetime_immutable', + 'input' => 'date_point', // If true, the browser will display the native date picker widget // however, this app uses a custom JavaScript widget, so it must be set to false 'html5' => false, diff --git a/src/Repository/PostRepository.php b/src/Repository/PostRepository.php index 7f6de8d7f..1a1de55cf 100644 --- a/src/Repository/PostRepository.php +++ b/src/Repository/PostRepository.php @@ -16,6 +16,7 @@ use App\Pagination\Paginator; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; +use Symfony\Component\Clock\DatePoint; use function Symfony\Component\String\u; @@ -48,7 +49,7 @@ public function findLatest(int $page = 1, ?Tag $tag = null): Paginator ->leftJoin('p.tags', 't') ->where('p.publishedAt <= :now') ->orderBy('p.publishedAt', 'DESC') - ->setParameter('now', new \DateTimeImmutable()) + ->setParameter('now', new DatePoint()) ; if (null !== $tag) {