Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -128,7 +129,7 @@ private function getTagData(): array
}

/**
* @return array<int, array{0: string, 1: AbstractUnicodeString, 2: string, 3: string, 4: \DateTimeImmutable, 5: User, 6: array<Tag>}>
* @return array<int, array{0: string, 1: AbstractUnicodeString, 2: string, 3: string, 4: DatePoint, 5: User, 6: array<Tag>}>
*
* @throws \Exception
*/
Expand All @@ -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(),
Expand Down
11 changes: 6 additions & 5 deletions src/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,16 +44,16 @@ 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)]
private ?User $author = null;

public function __construct()
{
$this->publishedAt = new \DateTimeImmutable();
$this->publishedAt = new DatePoint();
}

#[Assert\IsTrue(message: 'comment.is_spam')]
Expand All @@ -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;
}
Expand Down
11 changes: 6 additions & 5 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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)]
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Type/DateTimePickerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down