zend-blog-3-backend
547 строк · 8.9 Кб
1<?php
2
3namespace App\Entity;
4
5use App\Entity\Traits\ModifyEntityTrait;
6use DateTime;
7use Doctrine\Common\Collections\ArrayCollection;
8use Doctrine\Common\Collections\Collection;
9use Doctrine\Common\Collections\Criteria;
10use Doctrine\ORM\Mapping as ORM;
11
12/**
13* @ORM\Table(name="posts")
14* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
15* @ORM\HasLifecycleCallbacks()
16*/
17class Post
18{
19use ModifyEntityTrait;
20
21/**
22* @var int
23*
24* @ORM\Id
25* @ORM\Column(type="integer")
26* @ORM\GeneratedValue(strategy="AUTO")
27*/
28protected $id;
29
30/**
31* @var string
32*
33* @ORM\Column(type="string", length=128)
34*/
35protected $title;
36
37/**
38* @var string
39*
40* @ORM\Column(type="string", length=255, unique=true)
41*/
42protected $url;
43
44/**
45* @var bool
46*
47* @ORM\Column(type="boolean")
48*/
49protected $hide = false;
50
51/**
52* @var string
53*
54* @ORM\Column(type="text", name="text_post")
55*/
56protected $text;
57
58/**
59* @var string
60*
61* @ORM\Column(type="text")
62*/
63protected $preview;
64
65/**
66* @var string
67*
68* @ORM\Column(type="text")
69*/
70protected $rawText;
71
72/**
73* @var string
74*
75* @ORM\Column(type="string", length=255, nullable=true)
76*/
77protected $description;
78
79/**
80* @var Category
81*
82* @ORM\ManyToOne(targetEntity="Category", inversedBy="posts")
83* @ORM\JoinColumn(nullable=false)
84*/
85protected $category;
86
87/**
88* @var Collection
89*
90* @ORM\ManyToMany(targetEntity="Tag", inversedBy="posts")
91* @ORM\JoinTable(name="relation_topictag")
92*/
93protected $tags;
94
95/**
96* @var int
97*
98* @ORM\Column(type="integer")
99*/
100protected $commentsCount = 0;
101
102/**
103* @var int
104*
105* @ORM\Column(type="integer")
106*/
107protected $viewsCount = 0;
108
109/**
110* @var Collection
111*
112* @ORM\OneToMany(targetEntity="Comment", mappedBy="post")
113*/
114protected $comments;
115
116/**
117* @var Collection
118*
119* @ORM\OneToMany(targetEntity="MediaFile", mappedBy="post")
120*/
121protected $mediaFiles;
122
123/**
124* @var DateTime
125*
126* @ORM\Column(type="datetime", options={
127* "default": "CURRENT_TIMESTAMP",
128* "comment": "updated if article content changes"})
129*/
130private $updatedAt;
131
132/**
133* @var bool
134*
135* @ORM\Column(type="boolean", options={"default": false})
136*/
137private $disableComments = false;
138
139public function __construct()
140{
141$this->tags = new ArrayCollection();
142$this->comments = new ArrayCollection();
143$this->mediaFiles = new ArrayCollection();
144
145$this->timeCreated = new DateTime();
146$this->updatedAt = new DateTime();
147}
148
149/**
150* Get id
151*
152* @return int
153*/
154public function getId()
155{
156return $this->id;
157}
158
159/**
160* Set title
161*
162* @param string $title
163*
164* @return Post
165*/
166public function setTitle($title)
167{
168$this->title = $title;
169
170return $this;
171}
172
173/**
174* Get title
175*
176* @return string
177*/
178public function getTitle()
179{
180return $this->title;
181}
182
183/**
184* Set url
185*
186* @param string $url
187*
188* @return Post
189*/
190public function setUrl($url)
191{
192$this->url = $url;
193
194return $this;
195}
196
197/**
198* Get url
199*
200* @return string
201*/
202public function getUrl()
203{
204return $this->url;
205}
206
207/**
208* Set hide
209*
210* @param bool $hide
211*
212* @return Post
213*/
214public function setHide($hide)
215{
216$this->hide = $hide;
217
218return $this;
219}
220
221/**
222* Get hide
223*
224* @return bool
225*/
226public function isHide()
227{
228return $this->hide;
229}
230
231/**
232* Set text
233*
234* @param string $text
235*
236* @return Post
237*/
238public function setText($text)
239{
240$this->text = $text;
241
242return $this;
243}
244
245/**
246* Get text
247*
248* @return string
249*/
250public function getText()
251{
252return $this->text;
253}
254
255/**
256* Set description
257*
258* @param string $description
259*
260* @return Post
261*/
262public function setDescription($description)
263{
264$this->description = $description;
265
266return $this;
267}
268
269/**
270* Get description
271*
272* @return string
273*/
274public function getDescription()
275{
276return $this->description;
277}
278
279/**
280* Set category
281*
282* @param Category $category
283*
284* @return Post
285*/
286public function setCategory(Category $category = null)
287{
288$this->category = $category;
289
290return $this;
291}
292
293/**
294* Get category
295*
296* @return Category
297*/
298public function getCategory()
299{
300return $this->category;
301}
302
303/**
304* Add tag
305*
306* @param Tag $tag
307*
308* @return Post
309*/
310public function addTag(Tag $tag)
311{
312if (!$this->tags->contains($tag)) {
313$this->tags->add($tag);
314}
315
316return $this;
317}
318
319/**
320* Remove tag
321*
322* @param Tag $tag
323*/
324public function removeTag(Tag $tag)
325{
326$this->tags->removeElement($tag);
327}
328
329/**
330* Get tags
331*
332* @return Collection
333*/
334public function getTags()
335{
336return $this->tags;
337}
338
339/**
340* Add comments
341*
342* @param Comment $comments
343*
344* @return Post
345*/
346public function addComment(Comment $comments)
347{
348$this->comments[] = $comments;
349
350return $this;
351}
352
353/**
354* Remove comments
355*
356* @param Comment $comments
357*/
358public function removeComment(Comment $comments)
359{
360$this->comments->removeElement($comments);
361}
362
363/**
364* Get comments
365*
366* @return Collection
367*/
368public function getComments()
369{
370return $this->comments;
371}
372
373/**
374* Add mediaFile
375*
376* @param MediaFile $mediaFile
377*
378* @return Post
379*/
380public function addMediaFile(MediaFile $mediaFile)
381{
382$this->mediaFiles[] = $mediaFile;
383
384return $this;
385}
386
387/**
388* Remove mediaFile
389*
390* @param MediaFile $mediaFile
391*/
392public function removeMediaFile(MediaFile $mediaFile)
393{
394$this->mediaFiles->removeElement($mediaFile);
395}
396
397/**
398* Get mediaFiles
399*
400* @return Collection
401*/
402public function getMediaFiles()
403{
404return $this->mediaFiles;
405}
406
407public function getDefaultImage(): ?MediaFile
408{
409$criteria = Criteria::create()
410->andWhere(Criteria::expr()->eq('defaultImage', true))
411;
412
413return $this->mediaFiles->matching($criteria)->first() ?: null;
414}
415
416/**
417* Set rawText
418*
419* @param string $rawText
420*
421* @return Post
422*/
423public function setRawText($rawText)
424{
425$this->rawText = $rawText;
426
427return $this;
428}
429
430/**
431* Get rawText
432*
433* @return string
434*/
435public function getRawText()
436{
437return $this->rawText;
438}
439
440/**
441* Set commentsCount
442*
443* @param int $commentsCount
444*
445* @return Post
446*/
447public function setCommentsCount($commentsCount)
448{
449$this->commentsCount = $commentsCount;
450
451return $this;
452}
453
454/**
455* Get commentsCount
456*
457* @return int
458*/
459public function getCommentsCount()
460{
461return $this->commentsCount;
462}
463
464/**
465* Set viewsCount
466*
467* @param int $viewsCount
468*
469* @return Post
470*/
471public function setViewsCount($viewsCount)
472{
473$this->viewsCount = $viewsCount;
474
475return $this;
476}
477
478/**
479* Get viewsCount
480*
481* @return int
482*/
483public function getViewsCount()
484{
485return $this->viewsCount;
486}
487
488/**
489* @return string
490*/
491public function getPreview()
492{
493return $this->preview;
494}
495
496/**
497* @param string $preview
498*
499* @return Post
500*/
501public function setPreview($preview): Post
502{
503$this->preview = $preview;
504
505return $this;
506}
507
508/**
509* @return DateTime
510*/
511public function getUpdatedAt(): DateTime
512{
513return $this->updatedAt;
514}
515
516/**
517* @param DateTime $updatedAt
518*
519* @return Post
520*/
521public function setUpdatedAt(DateTime $updatedAt): self
522{
523$this->updatedAt = $updatedAt;
524
525return $this;
526}
527
528/**
529* @return bool
530*/
531public function isDisableComments(): bool
532{
533return $this->disableComments;
534}
535
536/**
537* @param bool $disableComments
538*
539* @return Post
540*/
541public function setDisableComments(bool $disableComments): self
542{
543$this->disableComments = $disableComments;
544
545return $this;
546}
547}
548