|
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
|
|
|
use Sulu\Component\Security\Authentication\UserInterface; |
|
19
|
|
|
|
|
20
|
|
|
class Thread implements ThreadInterface, AuditableInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use AuditableTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $id; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $type; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $entityId; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $title; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var int |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $commentCount = 0; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var Collection |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $comments; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var \DateTime |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $created; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var \DateTime |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $changed; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var UserInterface |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $changer; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var UserInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $creator; |
|
73
|
|
|
|
|
74
|
|
|
public function __construct(string $type, string $entityId, Collection $comments = null, int $commentCount = 0) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->type = $type; |
|
77
|
|
|
$this->entityId = $entityId; |
|
78
|
|
|
$this->comments = $comments ?: new ArrayCollection(); |
|
79
|
|
|
$this->commentCount = $commentCount; |
|
80
|
27 |
|
} |
|
81
|
|
|
|
|
82
|
27 |
|
public function getId(): int |
|
83
|
27 |
|
{ |
|
84
|
27 |
|
return $this->id; |
|
85
|
27 |
|
} |
|
86
|
27 |
|
|
|
87
|
|
|
public function getType(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->type; |
|
90
|
|
|
} |
|
91
|
8 |
|
|
|
92
|
|
|
public function getEntityId(): string |
|
93
|
8 |
|
{ |
|
94
|
|
|
return $this->entityId; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getTitle(): string |
|
98
|
|
|
{ |
|
99
|
10 |
|
return $this->title; |
|
100
|
|
|
} |
|
101
|
10 |
|
|
|
102
|
|
|
public function setTitle(string $title): ThreadInterface |
|
103
|
|
|
{ |
|
104
|
|
|
$this->title = $title; |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
|
|
|
|
|
107
|
10 |
|
} |
|
108
|
|
|
|
|
109
|
10 |
|
public function getCommentCount(): int |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->commentCount; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function increaseCommentCount(): ThreadInterface |
|
115
|
3 |
|
{ |
|
116
|
|
|
++$this->commentCount; |
|
117
|
3 |
|
|
|
118
|
|
|
return $this; |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function decreaseCommentCount(): ThreadInterface |
|
122
|
|
|
{ |
|
123
|
21 |
|
--$this->commentCount; |
|
124
|
|
|
|
|
125
|
21 |
|
return $this; |
|
|
|
|
|
|
126
|
21 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function setCommentCount(int $commentCount): ThreadInterface |
|
129
|
|
|
{ |
|
130
|
|
|
$this->commentCount = $commentCount; |
|
131
|
11 |
|
|
|
132
|
|
|
return $this; |
|
|
|
|
|
|
133
|
11 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function getComments(): Collection |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->comments; |
|
138
|
|
|
} |
|
139
|
17 |
|
|
|
140
|
|
|
public function addComment(CommentInterface $comment): ThreadInterface |
|
141
|
17 |
|
{ |
|
142
|
|
|
$this->comments->add($comment); |
|
143
|
17 |
|
$comment->setThread($this); |
|
144
|
|
|
|
|
145
|
|
|
if ($comment->isPublished()) { |
|
146
|
|
|
$this->increaseCommentCount(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
6 |
|
return $this; |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
6 |
|
|
|
152
|
|
|
public function removeComment(CommentInterface $comment): ThreadInterface |
|
153
|
6 |
|
{ |
|
154
|
|
|
$this->comments->removeElement($comment); |
|
155
|
|
|
|
|
156
|
|
|
if ($comment->isPublished()) { |
|
157
|
|
|
$this->decreaseCommentCount(); |
|
158
|
|
|
} |
|
159
|
1 |
|
|
|
160
|
|
|
return $this; |
|
|
|
|
|
|
161
|
1 |
|
} |
|
162
|
|
|
|
|
163
|
1 |
|
public function getCreatorFullName(): string |
|
164
|
|
|
{ |
|
165
|
|
|
$creator = $this->getCreator(); |
|
166
|
|
|
if (!$creator) { |
|
167
|
|
|
return ''; |
|
168
|
|
|
} |
|
169
|
9 |
|
|
|
170
|
|
|
return $creator->getFullName(); |
|
171
|
9 |
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function getChangerFullName(): string |
|
174
|
|
|
{ |
|
175
|
|
|
$changer = $this->getChanger(); |
|
176
|
|
|
if (!$changer) { |
|
177
|
16 |
|
return ''; |
|
178
|
|
|
} |
|
179
|
16 |
|
|
|
180
|
16 |
|
return $changer->getFullName(); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
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.