|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sulu GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sulu\Bundle\CommentBundle\Entity; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
15
|
|
|
use Doctrine\Common\Collections\Collection; |
|
16
|
|
|
use Sulu\Component\Persistence\Model\AuditableInterface; |
|
17
|
|
|
use Sulu\Component\Persistence\Model\AuditableTrait; |
|
18
|
|
|
|
|
19
|
|
|
class Comment implements CommentInterface, AuditableInterface |
|
20
|
|
|
{ |
|
21
|
|
|
use AuditableTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var int |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $id; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $state = self::STATE_PUBLISHED; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $message; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var ThreadInterface|null |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $thread; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var int |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $lft; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var int |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $rgt; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var int |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $depth; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var CommentInterface|null |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $parent; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var Collection|CommentInterface[] |
|
65
|
|
|
*/ |
|
66
|
10 |
|
protected $children; |
|
67
|
|
|
|
|
68
|
10 |
|
public function __construct(int $state = self::STATE_PUBLISHED, ThreadInterface $thread = null) |
|
69
|
10 |
|
{ |
|
70
|
|
|
$this->state = $state; |
|
71
|
10 |
|
$this->thread = $thread; |
|
72
|
8 |
|
$this->children = new ArrayCollection(); |
|
73
|
|
|
|
|
74
|
10 |
|
if ($this->thread && $this->isPublished()) { |
|
75
|
|
|
$this->thread->increaseCommentCount(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getId(): int |
|
80
|
|
|
{ |
|
81
|
6 |
|
return $this->id; |
|
82
|
|
|
} |
|
83
|
6 |
|
|
|
84
|
|
|
public function getState(): int |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->state; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
public function publish(): CommentInterface |
|
90
|
|
|
{ |
|
91
|
1 |
|
if (null !== $this->thread && !$this->isPublished()) { |
|
92
|
|
|
$this->thread->increaseCommentCount(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->state = self::STATE_PUBLISHED; |
|
96
|
|
|
|
|
97
|
2 |
|
return $this; |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
2 |
|
|
|
100
|
2 |
|
public function unpublish(): CommentInterface |
|
101
|
|
|
{ |
|
102
|
|
|
if (null !== $this->thread && $this->isPublished()) { |
|
103
|
2 |
|
$this->thread->decreaseCommentCount(); |
|
104
|
|
|
} |
|
105
|
2 |
|
|
|
106
|
|
|
$this->state = self::STATE_UNPUBLISHED; |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
public function isPublished(): bool |
|
112
|
|
|
{ |
|
113
|
2 |
|
return self::STATE_PUBLISHED === $this->state; |
|
114
|
2 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getMessage(): string |
|
117
|
2 |
|
{ |
|
118
|
|
|
return $this->message ?? ''; |
|
119
|
2 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function setMessage(string $message): CommentInterface |
|
122
|
|
|
{ |
|
123
|
|
|
$this->message = $message; |
|
124
|
|
|
|
|
125
|
15 |
|
return $this; |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
15 |
|
|
|
128
|
|
|
public function getThread(): ThreadInterface |
|
129
|
|
|
{ |
|
130
|
|
|
if (!$this->thread) { |
|
131
|
|
|
throw new \RuntimeException('No thread assigned.'); |
|
132
|
|
|
} |
|
133
|
1 |
|
|
|
134
|
|
|
return $this->thread; |
|
135
|
1 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function setThread(ThreadInterface $thread): CommentInterface |
|
138
|
|
|
{ |
|
139
|
|
|
$this->thread = $thread; |
|
140
|
|
|
|
|
141
|
9 |
|
return $this; |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
9 |
|
|
|
144
|
|
|
public function getParent(): ?CommentInterface |
|
145
|
9 |
|
{ |
|
146
|
|
|
return $this->parent; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function setParent(?CommentInterface $parent = null): CommentInterface |
|
150
|
|
|
{ |
|
151
|
5 |
|
$this->parent = $parent; |
|
152
|
|
|
|
|
153
|
5 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function getDepth(): int |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->depth; |
|
159
|
14 |
|
} |
|
160
|
|
|
|
|
161
|
14 |
|
public function getChildren(): Collection |
|
162
|
|
|
{ |
|
163
|
14 |
|
return $this->children; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function getPublishedChildren(): Collection |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->children->filter( |
|
169
|
|
|
function(CommentInterface $comment) { |
|
170
|
|
|
return $comment->isPublished(); |
|
171
|
|
|
} |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function getCreatorFullName(): string |
|
176
|
|
|
{ |
|
177
|
9 |
|
$creator = $this->getCreator(); |
|
178
|
|
|
if (!$creator) { |
|
179
|
9 |
|
return ''; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $creator->getFullName(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
9 |
|
public function getChangerFullName(): string |
|
186
|
|
|
{ |
|
187
|
9 |
|
$changer = $this->getChanger(); |
|
188
|
|
|
if (!$changer) { |
|
189
|
|
|
return ''; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $changer->getFullName(); |
|
193
|
9 |
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.