1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace CloudPlayDev\ConfluenceClient\Entity; |
5
|
|
|
|
6
|
|
|
use CloudPlayDev\ConfluenceClient\Exception\HydrationException; |
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use DateTimeInterface; |
9
|
|
|
use Webmozart\Assert\Assert; |
10
|
|
|
|
11
|
|
|
class ContentHistory implements Hydratable |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
private DateTimeInterface $createdDate; |
16
|
|
|
private DateTimeInterface $updatedDate; |
17
|
|
|
private bool $isLatest = false; |
18
|
|
|
private User $createdBy; |
19
|
|
|
private User $updatedBy; |
20
|
|
|
|
21
|
|
|
private int $lastVersionNumber; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @throws HydrationException |
25
|
|
|
*/ |
26
|
|
|
public static function load(array $data): ContentHistory |
27
|
|
|
{ |
28
|
|
|
$contentHistory = new self; |
29
|
|
|
Assert::keyExists($data, 'createdDate'); |
30
|
|
|
Assert::keyExists($data, 'createdBy'); |
31
|
|
|
Assert::keyExists($data, 'lastUpdated'); |
32
|
|
|
Assert::isArray($data['createdBy']); |
33
|
|
|
Assert::isArray($data['lastUpdated']); |
34
|
|
|
|
35
|
|
|
Assert::keyExists($data['lastUpdated'], 'by'); |
36
|
|
|
Assert::isArray($data['lastUpdated']['by']); |
37
|
|
|
|
38
|
|
|
if(isset($data['latest'])) { |
39
|
|
|
Assert::boolean($data['latest']); |
40
|
|
|
$contentHistory->setLatest($data['latest']); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$contentHistory->setCreatedDate(self::getDateTimeFromString($data['createdDate'])); |
44
|
|
|
$contentHistory->setCreatedBy(User::load($data['createdBy'])); |
45
|
|
|
$contentHistory->setUpdatedBy(User::load($data['lastUpdated']['by'])); |
46
|
|
|
|
47
|
|
|
$contentHistory->setUpdatedDate(self::getDateTimeFromString($data['lastUpdated']['when'])); |
48
|
|
|
|
49
|
|
|
$contentHistory->setLastVersionNumber($data['lastUpdated']['number']); |
50
|
|
|
|
51
|
|
|
return $contentHistory; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws HydrationException |
56
|
|
|
*/ |
57
|
|
|
private static function getDateTimeFromString(string $dateString): DateTimeInterface |
58
|
|
|
{ |
59
|
|
|
$dateTimeImmutable = DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.vZ', $dateString); |
60
|
|
|
if($dateTimeImmutable === false) { |
61
|
|
|
throw new HydrationException('Invalid date string: ' . $dateString); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $dateTimeImmutable; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function setLatest(bool $latest): ContentHistory |
68
|
|
|
{ |
69
|
|
|
$this->isLatest = $latest; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function setCreatedDate(DateTimeInterface $createFromFormat): ContentHistory |
74
|
|
|
{ |
75
|
|
|
$this->createdDate = $createFromFormat; |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function setCreatedBy(User $user): ContentHistory |
80
|
|
|
{ |
81
|
|
|
$this->createdBy = $user; |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function setUpdatedBy(User $user): ContentHistory |
86
|
|
|
{ |
87
|
|
|
$this->updatedBy = $user; |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setUpdatedDate(DateTimeInterface $updatedDate): ContentHistory |
92
|
|
|
{ |
93
|
|
|
$this->updatedDate = $updatedDate; |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getUpdatedDate(): DateTimeInterface |
98
|
|
|
{ |
99
|
|
|
return $this->updatedDate; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getCreatedDate(): DateTimeInterface |
103
|
|
|
{ |
104
|
|
|
return $this->createdDate; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function isLatest(): bool |
108
|
|
|
{ |
109
|
|
|
return $this->isLatest; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getCreatedBy(): User |
113
|
|
|
{ |
114
|
|
|
return $this->createdBy; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getUpdatedBy(): User |
118
|
|
|
{ |
119
|
|
|
return $this->updatedBy; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getLastVersionNumber(): int |
123
|
|
|
{ |
124
|
|
|
return $this->lastVersionNumber; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function setLastVersionNumber(int $lastVersionNumber): void |
128
|
|
|
{ |
129
|
|
|
$this->lastVersionNumber = $lastVersionNumber; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|