Skip to content

Commit 135247c

Browse files
committed
feat: use DatePoint class
1 parent 02ad0e9 commit 135247c

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

src/DataFixtures/AppFixtures.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use App\Entity\User;
1818
use Doctrine\Bundle\FixturesBundle\Fixture;
1919
use Doctrine\Persistence\ObjectManager;
20+
use Symfony\Component\Clock\DatePoint;
2021
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
2122
use Symfony\Component\String\AbstractUnicodeString;
2223
use Symfony\Component\String\Slugger\SluggerInterface;
@@ -85,7 +86,7 @@ private function loadPosts(ObjectManager $manager): void
8586
$comment = new Comment();
8687
$comment->setAuthor($this->getReference('john_user', User::class));
8788
$comment->setContent($this->getRandomText(random_int(255, 512)));
88-
$comment->setPublishedAt(new \DateTimeImmutable('now + '.$i.'seconds'));
89+
$comment->setPublishedAt(new DatePoint('now + '.$i.'seconds'));
8990

9091
$post->addComment($comment);
9192
}
@@ -128,7 +129,7 @@ private function getTagData(): array
128129
}
129130

130131
/**
131-
* @return array<int, array{0: string, 1: AbstractUnicodeString, 2: string, 3: string, 4: \DateTimeImmutable, 5: User, 6: array<Tag>}>
132+
* @return array<int, array{0: string, 1: AbstractUnicodeString, 2: string, 3: string, 4: DatePoint, 5: User, 6: array<Tag>}>
132133
*
133134
* @throws \Exception
134135
*/
@@ -143,7 +144,7 @@ private function getPostData(): array
143144
$this->slugger->slug($title)->lower(),
144145
$this->getRandomText(),
145146
$this->getPostContent(),
146-
(new \DateTimeImmutable('now - '.$i.'days'))->setTime(random_int(8, 17), random_int(7, 49), random_int(0, 59)),
147+
(new DatePoint('now - '.$i.'days'))->setTime(random_int(8, 17), random_int(7, 49), random_int(0, 59)),
147148
// Ensure that the first post is written by Jane Doe to simplify tests
148149
$this->getReference(['jane_admin', 'tom_admin'][0 === $i ? 0 : random_int(0, 1)], User::class),
149150
$this->getRandomTags(),

src/Entity/Comment.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\DBAL\Types\Types;
1515
use Doctrine\ORM\Mapping as ORM;
16+
use Symfony\Component\Clock\DatePoint;
1617
use Symfony\Component\Validator\Constraints as Assert;
1718

1819
use function Symfony\Component\String\u;
@@ -43,16 +44,16 @@ class Comment
4344
#[Assert\Length(min: 5, max: 10000, minMessage: 'comment.too_short', maxMessage: 'comment.too_long')]
4445
private ?string $content = null;
4546

46-
#[ORM\Column]
47-
private \DateTimeImmutable $publishedAt;
47+
#[ORM\Column(type: 'date_point')]
48+
private DatePoint $publishedAt;
4849

4950
#[ORM\ManyToOne(targetEntity: User::class)]
5051
#[ORM\JoinColumn(nullable: false)]
5152
private ?User $author = null;
5253

5354
public function __construct()
5455
{
55-
$this->publishedAt = new \DateTimeImmutable();
56+
$this->publishedAt = new DatePoint();
5657
}
5758

5859
#[Assert\IsTrue(message: 'comment.is_spam')]
@@ -78,12 +79,12 @@ public function setContent(string $content): void
7879
$this->content = $content;
7980
}
8081

81-
public function getPublishedAt(): \DateTimeImmutable
82+
public function getPublishedAt(): DatePoint
8283
{
8384
return $this->publishedAt;
8485
}
8586

86-
public function setPublishedAt(\DateTimeImmutable $publishedAt): void
87+
public function setPublishedAt(DatePoint $publishedAt): void
8788
{
8889
$this->publishedAt = $publishedAt;
8990
}

src/Entity/Post.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Doctrine\DBAL\Types\Types;
1818
use Doctrine\ORM\Mapping as ORM;
1919
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
20+
use Symfony\Component\Clock\DatePoint;
2021
use Symfony\Component\Validator\Constraints as Assert;
2122

2223
/**
@@ -55,8 +56,8 @@ class Post
5556
#[Assert\Length(min: 10, minMessage: 'post.too_short_content')]
5657
private ?string $content = null;
5758

58-
#[ORM\Column]
59-
private \DateTimeImmutable $publishedAt;
59+
#[ORM\Column(type: 'date_point')]
60+
private DatePoint $publishedAt;
6061

6162
#[ORM\ManyToOne(targetEntity: User::class)]
6263
#[ORM\JoinColumn(nullable: false)]
@@ -80,7 +81,7 @@ class Post
8081

8182
public function __construct()
8283
{
83-
$this->publishedAt = new \DateTimeImmutable();
84+
$this->publishedAt = new DatePoint();
8485
$this->comments = new ArrayCollection();
8586
$this->tags = new ArrayCollection();
8687
}
@@ -120,12 +121,12 @@ public function setContent(?string $content): void
120121
$this->content = $content;
121122
}
122123

123-
public function getPublishedAt(): \DateTimeImmutable
124+
public function getPublishedAt(): DatePoint
124125
{
125126
return $this->publishedAt;
126127
}
127128

128-
public function setPublishedAt(\DateTimeImmutable $publishedAt): void
129+
public function setPublishedAt(DatePoint $publishedAt): void
129130
{
130131
$this->publishedAt = $publishedAt;
131132
}

src/Form/Type/DateTimePickerType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function configureOptions(OptionsResolver $resolver): void
3030
// @see https://symfony.com/doc/current/reference/forms/types/date.html#rendering-a-single-html5-text-box
3131
$resolver->setDefaults([
3232
'widget' => 'single_text',
33-
'input' => 'datetime_immutable',
33+
'input' => 'date_point',
3434
// If true, the browser will display the native date picker widget
3535
// however, this app uses a custom JavaScript widget, so it must be set to false
3636
'html5' => false,

src/Repository/PostRepository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
1818
use Doctrine\Persistence\ManagerRegistry;
1919

20+
use Symfony\Component\Clock\DatePoint;
2021
use function Symfony\Component\String\u;
2122

2223
/**
@@ -48,7 +49,7 @@ public function findLatest(int $page = 1, ?Tag $tag = null): Paginator
4849
->leftJoin('p.tags', 't')
4950
->where('p.publishedAt <= :now')
5051
->orderBy('p.publishedAt', 'DESC')
51-
->setParameter('now', new \DateTimeImmutable())
52+
->setParameter('now', new DatePoint())
5253
;
5354

5455
if (null !== $tag) {

0 commit comments

Comments
 (0)