|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProjetNormandie\ArticleBundle\Entity; |
|
6
|
|
|
|
|
7
|
|
|
use ApiPlatform\Metadata\ApiResource; |
|
8
|
|
|
use ApiPlatform\Metadata\GetCollection; |
|
9
|
|
|
use ApiPlatform\Metadata\Get; |
|
10
|
|
|
use ApiPlatform\Metadata\Link; |
|
11
|
|
|
use ApiPlatform\Metadata\Post; |
|
12
|
|
|
use ApiPlatform\Metadata\Put; |
|
13
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
14
|
|
|
use Gedmo\Timestampable\Traits\TimestampableEntity; |
|
15
|
|
|
use ProjetNormandie\ArticleBundle\Repository\CommentRepository; |
|
16
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
|
17
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
18
|
|
|
|
|
19
|
|
|
#[ORM\Table(name:'pna_comment')] |
|
20
|
|
|
#[ORM\Entity(repositoryClass: CommentRepository::class)] |
|
21
|
|
|
#[ORM\EntityListeners(["ProjetNormandie\ArticleBundle\EventListener\Entity\CommentListener"])] |
|
22
|
|
|
#[ApiResource( |
|
23
|
|
|
shortName: 'ArticleComment', |
|
24
|
|
|
operations: [ |
|
25
|
|
|
new GetCollection(), |
|
26
|
|
|
new Get(), |
|
27
|
|
|
new Post( |
|
28
|
|
|
denormalizationContext: ['groups' => ['comment:insert']], |
|
29
|
|
|
security: 'is_granted("ROLE_USER")' |
|
30
|
|
|
), |
|
31
|
|
|
new Put( |
|
32
|
|
|
denormalizationContext: ['groups' => ['comment:update']], |
|
33
|
|
|
security: 'is_granted("ROLE_USER") and (object.getUser() == user)' |
|
34
|
|
|
) |
|
35
|
|
|
], |
|
36
|
|
|
normalizationContext: ['groups' => ['comment:read', 'user:read']], |
|
37
|
|
|
order: ['id' => 'ASC'] |
|
38
|
|
|
)] |
|
39
|
|
|
#[ApiResource( |
|
40
|
|
|
uriTemplate: '/articles/{id}/comments', |
|
41
|
|
|
shortName: 'ArticleComment', |
|
42
|
|
|
operations: [ new GetCollection() ], |
|
43
|
|
|
uriVariables: [ |
|
44
|
|
|
'id' => new Link(toProperty: 'article', fromClass: Article::class), |
|
45
|
|
|
], |
|
46
|
|
|
normalizationContext: ['groups' => ['comment:read', 'user:read']], |
|
47
|
|
|
)] |
|
48
|
|
|
|
|
49
|
|
|
class Comment |
|
50
|
|
|
{ |
|
51
|
|
|
use TimestampableEntity; |
|
52
|
|
|
|
|
53
|
|
|
#[Groups(['comment:read'])] |
|
54
|
|
|
#[ORM\Id, ORM\Column, ORM\GeneratedValue] |
|
55
|
|
|
private ?int $id = null; |
|
56
|
|
|
|
|
57
|
|
|
#[Groups(['comment:insert'])] |
|
58
|
|
|
#[ORM\ManyToOne(targetEntity: Article::class, inversedBy: 'comments')] |
|
59
|
|
|
#[ORM\JoinColumn(name:'article_id', referencedColumnName:'id', nullable:false)] |
|
60
|
|
|
private Article $article; |
|
61
|
|
|
|
|
62
|
|
|
#[Groups(['comment:read', 'comment:user'])] |
|
63
|
|
|
#[ORM\ManyToOne(targetEntity: UserInterface::class, fetch: 'EAGER')] |
|
64
|
|
|
#[ORM\JoinColumn(name:'user_id', referencedColumnName:'id', nullable:false)] |
|
65
|
|
|
private $user; |
|
66
|
|
|
|
|
67
|
|
|
#[Groups(['comment:read', 'comment:insert', 'comment:update'])] |
|
68
|
|
|
#[ORM\Column(type: 'text', nullable: false)] |
|
69
|
|
|
private string $content; |
|
70
|
|
|
|
|
71
|
|
|
public function __toString() |
|
72
|
|
|
{ |
|
73
|
|
|
return sprintf('comment [%s]', $this->id); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function setId(int $id): void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->id = $id; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getId(): ?int |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->id; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getArticle(): Article |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->article; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function setArticle(Article $article): void |
|
92
|
|
|
{ |
|
93
|
|
|
$this->article = $article; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getUser() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->user; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function setUser($user): void |
|
102
|
|
|
{ |
|
103
|
|
|
$this->user = $user; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function getContent(): string |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->content; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function setContent(string $content): void |
|
112
|
|
|
{ |
|
113
|
|
|
$this->content = $content; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|