blob: 7e2c06219a4bda9994580da323855dc588c22fa3 (
plain)
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
|
<?php
namespace Digitigrade\Model;
use Digitigrade\Model;
use Digitigrade\Model\UserAccount;
use Digitigrade\Timeline\TimelineItem;
class HomeTimelineItem extends Model {
public ?int $id;
public UserAccount $userAccount;
public ?string $reasonKind;
public ?string $reasonData;
public string $itemKind;
public string $itemData;
public \DateTimeImmutable $modified;
/**
* Creates a new home timeline entry for a particular user, but does not save it.
*/
public static function fromTimelineItem(TimelineItem $item, UserAccount $user): self {
$hti = new self();
$hti->userAccount = $user;
$reason = $item->getReason();
if ($reason != null) {
$hti->reasonKind = $reason::class;
$hti->reasonData = json_encode($reason->toJsonReference());
}
$object = $item->getObject();
$hti->itemKind = $object::class;
$hti->itemData = json_encode($object->toJsonReference());
$hti->modified = new \DateTimeImmutable();
return $hti;
}
public function toTimelineItem(): TimelineItem {
$reason = null;
if (isset($this->reasonKind)) {
$reason = $this->reasonKind::fromJsonReference(json_decode($this->reasonData));
}
$object = $this->itemKind::fromJsonReference(json_decode($this->itemData));
return new TimelineItem($object, $reason);
}
public static function findAllForUser(UserAccount $user, ?int $limit = null, int $offset = 0, ?\DateTimeInterface $since = null) {
$where = 'user_account = ?';
$params = [$user->id];
if (isset($since)) {
$where .= ' AND modified > ?';
$params[] = $since->format('c');
}
return self::findAllWhere($where, $params, $limit, $offset, 'id DESC');
}
}
|