-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathComment.php
More file actions
469 lines (417 loc) · 13.5 KB
/
Copy pathComment.php
File metadata and controls
469 lines (417 loc) · 13.5 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php
namespace BNETDocs\Libraries;
use \BNETDocs\Libraries\Core\DateTimeImmutable;
use \BNETDocs\Libraries\Db\MariaDb;
use \BNETDocs\Libraries\EventLog\EventTypes;
use \BNETDocs\Libraries\Tag\Tag;
use \BNETDocs\Libraries\Tag\Types as TagTypes;
use \BNETDocs\Libraries\User\User;
use \DateTimeInterface;
use \DateTimeZone;
use \StdClass;
use \UnexpectedValueException;
class Comment implements \BNETDocs\Interfaces\DatabaseObject, \JsonSerializable
{
public const MAX_CONTENT = 0xFFFF;
public const MAX_EDITED_COUNT = 0xFFFFFFFFFFFFFFFF;
public const MAX_ID = 0xFFFFFFFFFFFFFFFF;
public const MAX_PARENT_ID = 0xFFFFFFFFFFFFFFFF;
public const MAX_PARENT_TYPE = 0xFFFFFFFFFFFFFFFF;
public const MAX_USER_ID = 0xFFFFFFFFFFFFFFFF;
public const PARENT_TYPE_COMMENT = 0;
public const PARENT_TYPE_DOCUMENT = 1;
public const PARENT_TYPE_NEWS_POST = 2;
public const PARENT_TYPE_PACKET = 3;
public const PARENT_TYPE_SERVER = 4;
public const PARENT_TYPE_USER = 5;
protected string $content;
protected DateTimeInterface $created_datetime;
protected int $edited_count;
protected ?DateTimeInterface $edited_datetime;
protected ?int $id;
protected int $parent_id;
protected int $parent_type;
protected ?int $user_id;
public function __construct(StdClass|int|null $value)
{
if ($value instanceof StdClass)
{
$this->allocateObject($value);
}
else
{
$this->setId($value);
if (!$this->allocate()) throw new \BNETDocs\Exceptions\CommentNotFoundException($this);
}
}
/**
* Allocates the properties of this object from the database.
*
* @return boolean Whether the operation was successful.
*/
public function allocate(): bool
{
$this->setContent('');
$this->setCreatedDateTime('now');
$this->setEditedCount(0);
$this->setEditedDateTime(null);
$this->setParentId(0);
$this->setParentType(self::PARENT_TYPE_COMMENT);
$this->setUserId(null);
$id = $this->getId();
if (is_null($id)) return true;
$q = MariaDb::instance()->prepare('
SELECT
`content`,
`created_datetime`,
`edited_count`,
`edited_datetime`,
`id`,
`parent_id`,
`parent_type`,
`user_id`
FROM `comments` WHERE `id` = ? LIMIT 1;
');
if (!$q || !$q->execute([$id]) || $q->rowCount() != 1) return false;
$this->allocateObject($q->fetchObject());
$q->closeCursor();
return true;
}
protected function allocateObject(StdClass $value): void
{
$this->setContent($value->content);
$this->setCreatedDateTime($value->created_datetime);
$this->setEditedCount($value->edited_count);
$this->setEditedDateTime($value->edited_datetime);
$this->setId($value->id);
$this->setParentId($value->parent_id);
$this->setParentType($value->parent_type);
$this->setUserId($value->user_id);
}
/**
* Commits the properties of this object to the database.
*
* @return boolean Whether the operation was successful.
*/
public function commit(): bool
{
$q = MariaDb::instance()->prepare('
INSERT INTO `comments` (
`content`,
`created_datetime`,
`edited_count`,
`edited_datetime`,
`id`,
`parent_id`,
`parent_type`,
`user_id`
) VALUES (
:content,
:created_dt,
:edited_count,
:edited_dt,
:id,
:parent_id,
:parent_type,
:user_id
) ON DUPLICATE KEY UPDATE
`content` = :content,
`created_datetime` = :created_dt,
`edited_count` = :edited_count,
`edited_datetime` = :edited_dt,
`parent_id` = :parent_id,
`parent_type` = :parent_type,
`user_id` = :user_id;
');
$p = [
':content' => $this->getContent(false),
':created_dt' => $this->getCreatedDateTime(),
':edited_count' => $this->getEditedCount(),
':edited_dt' => $this->getEditedDateTime(),
':id' => $this->getId(),
':parent_id' => $this->getParentId(),
':parent_type' => $this->getParentType(),
':user_id' => $this->getUserId(),
];
foreach ($p as $k => $v)
{
if ($v instanceof DateTimeInterface)
{
$p[$k] = $v->format(self::DATE_SQL);
}
}
if (!$q || !$q->execute($p)) return false;
if (is_null($p[':id'])) $this->setId(MariaDb::instance()->lastInsertId());
$q->closeCursor();
return true;
}
/**
* Deallocates the properties of this object from the database.
*
* @return boolean Whether the operation was successful.
*/
public function deallocate(): bool
{
$id = $this->getId();
if (is_null($id)) return false;
$q = MariaDb::instance()->prepare('DELETE FROM `comments` WHERE `id` = ? LIMIT 1;');
try { return $q && $q->execute([$id]); }
finally { $q->closeCursor(); }
}
public static function getAll(int $parent_type, int $parent_id): ?array
{
$q = MariaDb::instance()->prepare('
SELECT
`content`,
`created_datetime`,
`edited_count`,
`edited_datetime`,
`id`,
`parent_id`,
`parent_type`,
`user_id`
FROM `comments` WHERE
`parent_type` = :pt AND
`parent_id` = :pid
ORDER BY
`created_datetime` ASC,
`id` ASC;
');
if (!$q || !$q->execute([':pid' => $parent_id, ':pt' => $parent_type])) return null;
$r = [];
while ($row = $q->fetchObject()) $r[] = new self($row);
$q->closeCursor();
return $r;
}
public static function getCommentsByUserId(int $user_id, bool $descending): ?array
{
$o = $descending ? 'DESC' : 'ASC';
$q = MariaDb::instance()->prepare(sprintf('
SELECT
`content`,
`created_datetime`,
`edited_count`,
`edited_datetime`,
`id`,
`parent_id`,
`parent_type`,
`user_id`
FROM `comments`
WHERE `user_id` = ? ORDER BY `created_datetime` %s, `id` %s;
', $o, $o));
if (!$q || !$q->execute([$user_id])) return null;
$r = [];
while ($row = $q->fetchObject()) $r[] = new self($row);
$q->closeCursor();
return $r;
}
public function getContent(bool $format, bool $autobreak = true): string
{
if (!$format) return $this->content;
$md = new \Parsedown();
$md->setBreaksEnabled($autobreak);
$md->setSafeMode(true); // unsafe user-input
return $md->text($this->content);
}
public function getCreatedDateTime(): DateTimeInterface
{
return $this->created_datetime;
}
public function getEditedCount(): int
{
return $this->edited_count;
}
public function getEditedDateTime(): ?DateTimeInterface
{
return $this->edited_datetime;
}
public function getId(): ?int
{
return $this->id;
}
public function getParentId(): int
{
return $this->parent_id;
}
public function getParentType(): int
{
return $this->parent_type;
}
public function getParentTypeCreatedEventId(): int
{
$pt = $this->getParentType();
switch ($pt)
{
case Comment::PARENT_TYPE_DOCUMENT: $r = EventTypes::COMMENT_CREATED_DOCUMENT; break;
case Comment::PARENT_TYPE_COMMENT: $r = EventTypes::COMMENT_CREATED_COMMENT; break;
case Comment::PARENT_TYPE_NEWS_POST: $r = EventTypes::COMMENT_CREATED_NEWS; break;
case Comment::PARENT_TYPE_PACKET: $r = EventTypes::COMMENT_CREATED_PACKET; break;
case Comment::PARENT_TYPE_SERVER: $r = EventTypes::COMMENT_CREATED_SERVER; break;
case Comment::PARENT_TYPE_USER: $r = EventTypes::COMMENT_CREATED_USER; break;
default: throw new UnexpectedValueException(sprintf('Parent type (%d) unknown', $pt));
}
return $r;
}
public function getParentTypeEditedEventId(): int
{
$pt = $this->getParentType();
switch ($pt)
{
case Comment::PARENT_TYPE_DOCUMENT: $r = EventTypes::COMMENT_EDITED_DOCUMENT; break;
case Comment::PARENT_TYPE_COMMENT: $r = EventTypes::COMMENT_EDITED_COMMENT; break;
case Comment::PARENT_TYPE_NEWS_POST: $r = EventTypes::COMMENT_EDITED_NEWS; break;
case Comment::PARENT_TYPE_PACKET: $r = EventTypes::COMMENT_EDITED_PACKET; break;
case Comment::PARENT_TYPE_SERVER: $r = EventTypes::COMMENT_EDITED_SERVER; break;
case Comment::PARENT_TYPE_USER: $r = EventTypes::COMMENT_EDITED_USER; break;
default: throw new UnexpectedValueException(sprintf('Parent type (%d) unknown', $pt));
}
return $r;
}
public function getParentTypeDeletedEventId(): int
{
$pt = $this->getParentType();
switch ($pt)
{
case Comment::PARENT_TYPE_DOCUMENT: $r = EventTypes::COMMENT_DELETED_DOCUMENT; break;
case Comment::PARENT_TYPE_COMMENT: $r = EventTypes::COMMENT_DELETED_COMMENT; break;
case Comment::PARENT_TYPE_NEWS_POST: $r = EventTypes::COMMENT_DELETED_NEWS; break;
case Comment::PARENT_TYPE_PACKET: $r = EventTypes::COMMENT_DELETED_PACKET; break;
case Comment::PARENT_TYPE_SERVER: $r = EventTypes::COMMENT_DELETED_SERVER; break;
case Comment::PARENT_TYPE_USER: $r = EventTypes::COMMENT_DELETED_USER; break;
default: throw new UnexpectedValueException(sprintf('Parent type (%d) unknown', $pt));
}
return $r;
}
public function getParentUrl(): string|false
{
$pt = $this->getParentType();
switch ($pt)
{
case self::PARENT_TYPE_DOCUMENT: $pts = 'document'; break;
case self::PARENT_TYPE_COMMENT: $pts = 'comment'; break;
case self::PARENT_TYPE_NEWS_POST: $pts = 'news'; break;
case self::PARENT_TYPE_PACKET: $pts = 'packet'; break;
case self::PARENT_TYPE_SERVER: $pts = 'server'; break;
case self::PARENT_TYPE_USER: $pts = 'user'; break;
default: throw new UnexpectedValueException(sprintf('Parent type (%d) unknown', $pt));
}
return \BNETDocs\Libraries\Core\UrlFormatter::format(sprintf('/%s/%d', $pts, $this->getParentId()));
}
public function getTags(): array
{
$id = $this->getId();
return is_null($id) ? [] : (Tag::allocateAll(TagTypes::Comment, $this->getId()) ?? []);
}
public function getUser(): ?User
{
return is_null($this->user_id) ? null : new User($this->user_id);
}
public function getUserId(): ?int
{
return $this->user_id;
}
public function jsonSerialize(): mixed
{
return [
'content' => $this->getContent(false),
'created_datetime' => $this->getCreatedDateTime(),
'edited_count' => $this->getEditedCount(),
'edited_datetime' => $this->getEditedDateTime(),
'id' => $this->getId(),
'parent_id' => $this->getParentId(),
'parent_type' => $this->getParentType(),
'user_id' => $this->getUserId(),
];
}
public function incrementEdited(): void
{
$this->setEditedCount($this->getEditedCount() + 1);
$this->setEditedDateTime(new DateTimeImmutable('now'));
}
public function setContent(string $value): void
{
if (strlen($value) > self::MAX_CONTENT)
{
throw new UnexpectedValueException(sprintf('value must be between 0-%d characters', self::MAX_CONTENT));
}
$this->content = $value;
}
public function setCreatedDateTime(DateTimeInterface|string $value)
{
$this->created_datetime = (is_string($value) ?
new DateTimeImmutable($value, new DateTimeZone(self::DATE_TZ)) : $value
);
}
public function setEditedCount(int $value): void
{
if ($value < 0 || $value > self::MAX_EDITED_COUNT)
{
throw new UnexpectedValueException(sprintf('value must be an integer between 0-%d', self::MAX_EDITED_COUNT));
}
$this->edited_count = $value;
}
public function setEditedDateTime(DateTimeInterface|string|null $value): void
{
$this->edited_datetime = (is_string($value) ?
new DateTimeImmutable($value, new DateTimeZone(self::DATE_TZ)) : $value
);
}
public function setId(?int $value): void
{
if ($value < 0 || $value > self::MAX_ID)
{
throw new UnexpectedValueException(sprintf('value must be null or an integer between 0-%d', self::MAX_ID));
}
$this->id = $value;
}
public function setParentId(int $value): void
{
if ($value < 0 || $value > self::MAX_PARENT_ID)
{
throw new UnexpectedValueException(sprintf('value must be an integer between 0-%d', self::MAX_PARENT_ID));
}
$this->parent_id = $value;
}
public function setParentType(int $value): void
{
if ($value < 0 || $value > self::MAX_PARENT_TYPE)
{
throw new UnexpectedValueException(sprintf('value must be an integer between 0-%d', self::MAX_PARENT_TYPE));
}
if (!self::validateParentType($value))
{
throw new UnexpectedValueException('value must be a valid Comment::PARENT_TYPE_ constant');
}
$this->parent_type = $value;
}
public function setUser(?User $value): void
{
$this->user_id = $value instanceof User ? $value->getId() : null;
}
public function setUserId(?int $value): void
{
if (!is_null($value) && ($value < 0 || $value > self::MAX_USER_ID))
{
throw new UnexpectedValueException(sprintf('value must be null or an integer between 0-%d', self::MAX_USER_ID));
}
$this->user_id = $value;
}
public static function validateParentType(int $value): bool
{
if ($value < 0 || $value > self::MAX_PARENT_TYPE)
{
throw new UnexpectedValueException(sprintf('value must be an integer between 0-%d', self::MAX_PARENT_TYPE));
}
switch ($value)
{
case self::PARENT_TYPE_COMMENT:
case self::PARENT_TYPE_DOCUMENT:
case self::PARENT_TYPE_NEWS_POST:
case self::PARENT_TYPE_PACKET:
case self::PARENT_TYPE_SERVER:
case self::PARENT_TYPE_USER:
return true;
default: return false;
}
}
}