Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
/**
* @ORM\Entity()
* @ORM\Table(name="categories", indexes={
* @ORM\Index(columns={"name"}),
* @ORM\Index(columns={"date"})
* })
*/
class Category
{
/**
* @var Uuid
* @ORM\Id()
* @ORM\Column(type="uuid")
*/
private $id;
/**
* @var string
* @ORM\Column(type="string", nullable=false)
*/
private $name;
/**
* @var \DateTimeImmutable
* @ORM\Column(type="datetime_immutable")
*/
private $date;
public function __construct(string $name)
{
$this->id = Uuid::uuid4();
$this->name = $name;
$this->date = new \DateTimeImmutable('now', new \DateTimeZone('Europe/Moscow'));
}
/**
* @return Uuid
*/
public function getId(): Uuid
{
return $this->id;
}
/**
* @return \DateTimeImmutable
*/
public function getDate(): \DateTimeImmutable
{
return $this->date;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
}
<?php
declare(strict_types=1);
namespace App\Repository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use App\Entity\Category;
class CategoryRepository
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var EntityRepository
*/
private $repository;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->repository = $em->getRepository(Category::class);
}
public function add(Category $category)
{
$this->em->persist($category);
}
public function findOneByName(string $name)
{
return $this->repository->findOneBy(['name' => $name]);
}
}
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
/**
* @ORM\Entity()
* @ORM\Table(name="posts", indexes={
* @ORM\Index(columns={"user_id"}),
* @ORM\Index(columns={"status"}),
* @ORM\Index(columns={"created_at"})
* })
*/
class Post
{
public const DRAFT = 'draft';
public const PUBLISHED = 'published';
/**
* @var Uuid
* @ORM\Id()
* @ORM\Column(type="uuid")
*/
private $id;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $title;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $body;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $slug;
/**
* @var string
* @ORM\Column(type="string", nullable=false)
*/
private $status;
/**
* @ORM\Column(type="datetime_immutable", nullable=false)
*/
private $createdAt;
/**
* @var \DateTimeImmutable
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $publishedAt;
/**
* @var \DateTimeImmutable
* @ORM\Column(type="datetime_immutable", nullable=false)
*/
private $updatedAt;
/**
* @var User
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(referencedColumnName="id", onDelete="SET NULL", nullable=true)
*/
private $user;
/**
* @var ArrayCollection
* @ORM\ManyToMany(targetEntity="App\Entity\Category", cascade={"persist"})
* @ORM\JoinTable(name="post_categories")
* @ORM\JoinColumn(referencedColumnName="id", nullable=false)
*/
private $categories;
private function __construct()
{
$this->id = Uuid::uuid4();
$this->categories = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable('now', new \DateTimeZone('Europe/Moscow'));
}
public static function fromDraft(
User $user,
?string $title,
?string $body,
?string $slug
): Post {
$post = new self();
$post->title = $title;
$post->body = $body;
$post->slug = $slug;
$post->user = $user;
$post->updatedAt = new \DateTimeImmutable('now', new \DateTimeZone('Europe/Moscow'));
$post->status = self::DRAFT;
return $post;
}
public static function fromPublished(string $title, string $body, string $slug): Post
{
$post = new self();
$post->title = $title;
$post->body = $body;
$post->slug = $slug;
$post->updatedAt = new \DateTimeImmutable('now', new \DateTimeZone('Europe/Moscow'));
$post->publishedAt = new \DateTimeImmutable('now', new \DateTimeZone('Europe/Moscow'));
$post->status = self::PUBLISHED;
return $post;
}
/**
* @return Uuid
*/
public function getId(): Uuid
{
return $this->id;
}
/**
* @return string|null
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* @return string|null
*/
public function getBody(): ?string
{
return $this->body;
}
/**
* @return string|null
*/
public function getSlug(): ?string
{
return $this->slug;
}
/**
* @return \DateTimeImmutable
*/
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
/**
* @return string
*/
public function getStatus(): string
{
return $this->status;
}
/**
* @return \DateTimeImmutable
*/
public function getPublishedAt(): \DateTimeImmutable
{
return $this->publishedAt;
}
/**
* @return \DateTimeImmutable
*/
public function getUpdatedAt(): \DateTimeImmutable
{
return $this->updatedAt;
}
/**
* @return User
*/
public function getUser(): User
{
return $this->user;
}
}
<?php
declare(strict_types=1);
namespace App\Repository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use App\Entity\Post;
class PostRepository
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var EntityRepository
*/
private $repository;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->repository = $em->getRepository(Post::class);
}
public function add(Post $post)
{
$this->em->persist($post);
}
public function findOneBySlug(string $slug)
{
return $this->repository->findOneBy(['slug' => $slug]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.