1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Fixtures\Document; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @MongoDB\Document(collection="post") |
11
|
|
|
*/ |
12
|
|
|
class Post |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @MongoDB\EmbedMany( |
16
|
|
|
* targetDocument=Comment::class |
17
|
|
|
* ) |
18
|
|
|
*/ |
19
|
|
|
protected $comments; |
20
|
|
|
/** |
21
|
|
|
* @MongoDB\Id |
22
|
|
|
*/ |
23
|
|
|
private $id; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @MongoDB\Field(type="string") |
27
|
|
|
*/ |
28
|
|
|
private $title; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @MongoDB\Field(type="string") |
32
|
|
|
*/ |
33
|
|
|
private $body; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @MongoDB\Field(type="string", nullable=true) |
37
|
|
|
*/ |
38
|
|
|
private $shortDescription; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @MongoDB\Field(type="int") |
42
|
|
|
*/ |
43
|
|
|
private $viewCount = 0; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @MongoDB\Field(type="date") |
47
|
|
|
*/ |
48
|
|
|
private $createdAt; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @MongoDB\Field(type="date", nullable=true) |
52
|
|
|
*/ |
53
|
|
|
private $publishedAt; |
54
|
|
|
|
55
|
|
|
public function __construct(string $title, string $body, ?string $shortDescription = null) |
56
|
|
|
{ |
57
|
|
|
$this->title = $title; |
58
|
|
|
$this->body = $body; |
59
|
|
|
$this->shortDescription = $shortDescription; |
60
|
|
|
$this->createdAt = new \DateTime('now'); |
61
|
|
|
$this->comments = new ArrayCollection(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function __toString(): string |
65
|
|
|
{ |
66
|
|
|
return $this->title; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getTitle(): ?string |
70
|
|
|
{ |
71
|
|
|
return $this->title; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getBody(): ?string |
75
|
|
|
{ |
76
|
|
|
return $this->body; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getShortDescription(): ?string |
80
|
|
|
{ |
81
|
|
|
return $this->shortDescription; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getViewCount(): int |
85
|
|
|
{ |
86
|
|
|
return $this->viewCount; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function increaseViewCount(int $amount = 1): void |
90
|
|
|
{ |
91
|
|
|
$this->viewCount += $amount; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getCreatedAt(): ?\DateTime |
95
|
|
|
{ |
96
|
|
|
return $this->createdAt; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function isPublished(): bool |
100
|
|
|
{ |
101
|
|
|
return null !== $this->publishedAt; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setPublishedAt(\DateTime $timestamp) |
105
|
|
|
{ |
106
|
|
|
$this->publishedAt = $timestamp; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return Collection<Comment> |
111
|
|
|
*/ |
112
|
|
|
public function getComments(): Collection |
113
|
|
|
{ |
114
|
|
|
return $this->comments; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function addComment(Comment $comment): self |
118
|
|
|
{ |
119
|
|
|
if (!$this->comments->contains($comment)) { |
120
|
|
|
$this->comments->add($comment); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function removeComment(Comment $comment): self |
127
|
|
|
{ |
128
|
|
|
if ($this->comments->contains($comment)) { |
129
|
|
|
$this->comments->removeElement($comment); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|