1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Explicit Architecture POC, |
7
|
|
|
* which is created on top of the Symfony Demo application. |
8
|
|
|
* |
9
|
|
|
* (c) Herberto Graça <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Acme\App\Core\Component\Blog\Domain\Post\Comment; |
16
|
|
|
|
17
|
|
|
use Acme\App\Core\Component\Blog\Domain\Post\Post; |
18
|
|
|
use Acme\App\Core\SharedKernel\Component\Blog\Domain\Post\Comment\CommentId; |
19
|
|
|
use Acme\App\Core\SharedKernel\Component\User\Domain\User\UserId; |
20
|
|
|
use Acme\PhpExtension\DateTime\DateTimeGenerator; |
21
|
|
|
use DateTimeImmutable; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Defines the properties of the Comment entity to represent the blog comments. |
25
|
|
|
* See https://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class. |
26
|
|
|
* |
27
|
|
|
* Tip: if you have an existing database, you can generate these entity class automatically. |
28
|
|
|
* See https://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html |
29
|
|
|
* |
30
|
|
|
* @author Ryan Weaver <[email protected]> |
31
|
|
|
* @author Javier Eguiluz <[email protected]> |
32
|
|
|
* @author Herberto Graca <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class Comment |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var CommentId |
38
|
|
|
*/ |
39
|
|
|
private $id; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Post |
43
|
|
|
*/ |
44
|
|
|
private $post; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $content; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var DateTimeImmutable |
53
|
|
|
*/ |
54
|
|
|
private $publishedAt; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var UserId |
58
|
|
|
*/ |
59
|
|
|
private $authorId; |
60
|
|
|
|
61
|
|
|
public function __construct(string $content = '') |
62
|
|
|
{ |
63
|
|
|
$this->publishedAt = DateTimeGenerator::generate(); |
64
|
|
|
$this->id = new CommentId(); |
65
|
|
|
$this->content = $content; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Used for validation, in config/validator/validation.yaml:14 |
70
|
|
|
*/ |
71
|
|
|
public function isLegitComment(): bool |
72
|
|
|
{ |
73
|
|
|
$containsInvalidCharacters = mb_strpos($this->content, '@') !== false; |
74
|
|
|
|
75
|
|
|
return !$containsInvalidCharacters; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getId(): CommentId |
79
|
|
|
{ |
80
|
|
|
return $this->id; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getContent(): ?string |
84
|
|
|
{ |
85
|
|
|
return $this->content; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* This is needed for the $form->handleRequest() in the CommentController. |
90
|
|
|
* We shouldn't have this method here just because the framework needs it, but to remove it we need to change our |
91
|
|
|
* strategy for creating entities from forms. We will do that when we integrate a command bus. |
92
|
|
|
*/ |
93
|
|
|
public function setContent(string $content): void |
94
|
|
|
{ |
95
|
|
|
$this->content = $content; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getPublishedAt(): DateTimeImmutable |
99
|
|
|
{ |
100
|
|
|
return $this->publishedAt; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function setPublishedAt(DateTimeImmutable $publishedAt): void |
104
|
|
|
{ |
105
|
|
|
$this->publishedAt = $publishedAt; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getAuthorId(): UserId |
109
|
|
|
{ |
110
|
|
|
return $this->authorId; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function setAuthorId(UserId $authorId): void |
114
|
|
|
{ |
115
|
|
|
$this->authorId = $authorId; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setPost(Post $post): void |
119
|
|
|
{ |
120
|
|
|
$this->post = $post; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|