-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathComment.php
More file actions
132 lines (121 loc) · 4.43 KB
/
Comment.php
File metadata and controls
132 lines (121 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace App\Entity;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Post;
use App\InputFilter;
use App\Repository\CommentRepository;
use App\State\CommentCreateProcessor;
use App\Validator\AssertBelongsToSameCamp;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* A Comment someone left on an activity, to give feedback on the planned programme,
* for notes which are only relevant during camp planning, or for other communication.
*/
#[ApiResource(
operations: [
new Get(
security: 'is_granted("CAMP_COLLABORATOR", object) or
object.author === user',
),
new Delete(
security: 'object.author === user',
),
new GetCollection(
security: 'is_authenticated()'
),
new Post(
processor: CommentCreateProcessor::class,
denormalizationContext: ['groups' => ['create', 'write']],
securityPostDenormalize: 'is_granted("CAMP_COLLABORATOR", object)',
),
new GetCollection(
uriTemplate: self::ACTIVITY_SUBRESOURCE_URI_TEMPLATE,
uriVariables: [
'activityId' => new Link(
toProperty: 'activity',
fromClass: Activity::class,
security: 'is_granted("CAMP_COLLABORATOR", activity) or
is_granted("CAMP_IS_PUBLIC", activity)',
),
],
security: 'is_fully_authenticated()',
),
],
denormalizationContext: ['groups' => ['write']],
normalizationContext: ['groups' => ['read']],
order: ['createTime' => 'ASC'],
)]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['camp', 'activity'])]
#[ORM\Entity(repositoryClass: CommentRepository::class)]
class Comment extends BaseEntity implements BelongsToCampInterface {
public const ACTIVITY_SUBRESOURCE_URI_TEMPLATE = '/activities/{activityId}/comments{._format}';
/**
* The camp this comment belongs to.
*/
#[ApiProperty(example: '/camps/1a2b3c4d')]
#[Groups(['read', 'create'])]
#[ORM\ManyToOne(targetEntity: Camp::class, inversedBy: 'comments')]
#[ORM\JoinColumn(nullable: false, onDelete: 'cascade')]
public ?Camp $camp = null;
/**
* The activity this comment belongs to.
*/
#[AssertBelongsToSameCamp]
#[ApiProperty(example: '/activities/1a2b3c4d')]
#[Groups(['read', 'create'])]
#[ORM\ManyToOne(targetEntity: Activity::class, inversedBy: 'comments')]
#[ORM\JoinColumn(nullable: true)]
public ?Activity $activity = null;
/**
* The author of the comment.
*/
#[Assert\DisableAutoMapping] // avoids validation error when author is null in payload
#[ApiProperty(example: '/users/1a2b3c4d', writable: false)]
#[Groups(['read', 'create'])]
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'comments')]
#[ORM\JoinColumn(nullable: false, onDelete: 'cascade')]
public ?User $author = null;
/**
* The actual comment.
*/
#[InputFilter\Trim]
#[InputFilter\CleanHTML]
#[Assert\NotBlank]
#[Assert\Length(max: 1024)]
#[ApiProperty(example: 'This activity is great!')]
#[Groups(['read', 'create'])]
#[ORM\Column(type: 'text', nullable: false)]
public ?string $textHtml = null;
/**
* Persisted description of the context where the comment was originally writen.
* Only non-null when activity pointer is null, i.e. activity was deleted.
* Currently defined as the title of the activity when it was deleted.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 32)]
#[ApiProperty(example: 'Sportolympiade', writable: false)]
#[Groups(['read', 'create'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $orphanDescription = null;
public function __construct() {
parent::__construct();
}
public function getCamp(): ?Camp {
return $this->camp;
}
#[ApiProperty(writable: false)]
#[Groups(['read'])]
public function getCreateTime(): \DateTime {
return $this->createTime;
}
}