yiisoft /
yii-demo
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace App\Blog\Entity; |
||
| 6 | |||
| 7 | use App\User\User; |
||
| 8 | use Cycle\Annotated\Annotation\Column; |
||
| 9 | use Cycle\Annotated\Annotation\Entity; |
||
| 10 | use Cycle\Annotated\Annotation\Relation\BelongsTo; |
||
| 11 | use Cycle\Annotated\Annotation\Relation\HasMany; |
||
| 12 | use Cycle\Annotated\Annotation\Relation\ManyToMany; |
||
| 13 | use Cycle\Annotated\Annotation\Table; |
||
| 14 | use Cycle\Annotated\Annotation\Table\Index; |
||
| 15 | use Cycle\ORM\Relation\Pivoted\PivotedCollection; |
||
| 16 | use DateTimeImmutable; |
||
| 17 | use Doctrine\Common\Collections\ArrayCollection; |
||
| 18 | use Yiisoft\Security\Random; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @Entity( |
||
| 22 | * repository="App\Blog\Post\PostRepository", |
||
| 23 | * mapper="App\Blog\Post\PostMapper", |
||
| 24 | * constrain="App\Blog\Post\Scope\PublicScope" |
||
| 25 | * ) |
||
| 26 | * @Table( |
||
| 27 | * indexes={ |
||
| 28 | * @Index(columns={"public","published_at"}), |
||
| 29 | * } |
||
| 30 | * ) |
||
| 31 | */ |
||
| 32 | class Post |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @Column(type="primary") |
||
| 36 | */ |
||
| 37 | private ?int $id = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @Column(type="string(128)") |
||
| 41 | */ |
||
| 42 | private string $slug; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @Column(type="string(255)", default="") |
||
| 46 | */ |
||
| 47 | private string $title = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @Column(type="bool", default="false") |
||
| 51 | */ |
||
| 52 | private bool $public = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @Column(type="text") |
||
| 56 | */ |
||
| 57 | private string $content = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @Column(type="datetime") |
||
| 61 | */ |
||
| 62 | private DateTimeImmutable $created_at; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @Column(type="datetime") |
||
| 66 | */ |
||
| 67 | private DateTimeImmutable $updated_at; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @Column(type="datetime", nullable=true) |
||
| 71 | */ |
||
| 72 | private ?DateTimeImmutable $published_at = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @Column(type="datetime", nullable=true) |
||
| 76 | */ |
||
| 77 | private ?DateTimeImmutable $deleted_at = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @BelongsTo(target="App\User\User", nullable=false) |
||
| 81 | * |
||
| 82 | * @var \Cycle\ORM\Promise\Reference|User |
||
| 83 | */ |
||
| 84 | private $user = null; |
||
| 85 | private ?int $user_id = null; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 86 | |||
| 87 | /** |
||
| 88 | * @ManyToMany(target="App\Blog\Entity\Tag", though="PostTag", fkAction="CASCADE") |
||
| 89 | * |
||
| 90 | * @var PivotedCollection|Tag[] |
||
| 91 | */ |
||
| 92 | private $tags; |
||
| 93 | private ?int $tag_id = null; |
||
|
0 ignored issues
–
show
|
|||
| 94 | |||
| 95 | /** |
||
| 96 | * @HasMany(target="App\Blog\Entity\Comment") |
||
| 97 | * |
||
| 98 | * @var ArrayCollection|Comment[] |
||
| 99 | */ |
||
| 100 | private $comments; |
||
| 101 | |||
| 102 | public function __construct(string $title = '', string $content = '') |
||
| 103 | { |
||
| 104 | $this->title = $title; |
||
| 105 | $this->content = $content; |
||
| 106 | $this->created_at = new DateTimeImmutable(); |
||
| 107 | $this->updated_at = new DateTimeImmutable(); |
||
| 108 | $this->tags = new PivotedCollection(); |
||
| 109 | $this->comments = new ArrayCollection(); |
||
| 110 | $this->resetSlug(); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function getId(): ?int |
||
| 114 | { |
||
| 115 | return $this->id; |
||
| 116 | } |
||
| 117 | |||
| 118 | public function getSlug(): ?string |
||
| 119 | { |
||
| 120 | return $this->slug; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function resetSlug(): void |
||
| 124 | { |
||
| 125 | $this->slug = Random::string(128); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function getTitle(): string |
||
| 129 | { |
||
| 130 | return $this->title; |
||
| 131 | } |
||
| 132 | |||
| 133 | public function setTitle(string $title): void |
||
| 134 | { |
||
| 135 | $this->title = $title; |
||
| 136 | } |
||
| 137 | |||
| 138 | public function getContent(): string |
||
| 139 | { |
||
| 140 | return $this->content; |
||
| 141 | } |
||
| 142 | |||
| 143 | public function setContent(string $content): void |
||
| 144 | { |
||
| 145 | $this->content = $content; |
||
| 146 | } |
||
| 147 | |||
| 148 | public function isPublic(): bool |
||
| 149 | { |
||
| 150 | return $this->public; |
||
| 151 | } |
||
| 152 | |||
| 153 | public function setPublic(bool $public): void |
||
| 154 | { |
||
| 155 | $this->public = $public; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function getCreatedAt(): DateTimeImmutable |
||
| 159 | { |
||
| 160 | return $this->created_at; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function getUpdatedAt(): DateTimeImmutable |
||
| 164 | { |
||
| 165 | return $this->updated_at; |
||
| 166 | } |
||
| 167 | |||
| 168 | public function getDeletedAt(): ?DateTimeImmutable |
||
| 169 | { |
||
| 170 | return $this->deleted_at; |
||
| 171 | } |
||
| 172 | |||
| 173 | public function setUser(User $user): void |
||
| 174 | { |
||
| 175 | $this->user = $user; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function getUser(): ?User |
||
| 179 | { |
||
| 180 | return $this->user; |
||
|
0 ignored issues
–
show
|
|||
| 181 | } |
||
| 182 | |||
| 183 | public function getPublishedAt(): ?DateTimeImmutable |
||
| 184 | { |
||
| 185 | return $this->published_at; |
||
| 186 | } |
||
| 187 | |||
| 188 | public function setPublishedAt(?DateTimeImmutable $published_at): void |
||
| 189 | { |
||
| 190 | $this->published_at = $published_at; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return Comment[] |
||
| 195 | */ |
||
| 196 | public function getComments(): array |
||
| 197 | { |
||
| 198 | return $this->comments->toArray(); |
||
| 199 | } |
||
| 200 | |||
| 201 | public function addComment(Comment $post): void |
||
| 202 | { |
||
| 203 | $this->comments->add($post); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return Tag[] |
||
| 208 | */ |
||
| 209 | public function getTags(): array |
||
| 210 | { |
||
| 211 | return $this->tags->toArray(); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function addTag(Tag $post): void |
||
| 215 | { |
||
| 216 | $this->tags->add($post); |
||
| 217 | } |
||
| 218 | |||
| 219 | public function resetTags(): void |
||
| 220 | { |
||
| 221 | $this->tags->clear(); |
||
| 222 | } |
||
| 223 | |||
| 224 | public function isNewRecord(): bool |
||
| 225 | { |
||
| 226 | return $this->getId() === null; |
||
| 227 | } |
||
| 228 | } |
||
| 229 |