|
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
|
|
|
private ?int $previousVersionNumber = null; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @throws HydrationException |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function load(array $data): ContentHistory |
|
29
|
|
|
{ |
|
30
|
|
|
$contentHistory = new self; |
|
31
|
|
|
Assert::keyExists($data, 'createdDate'); |
|
32
|
|
|
Assert::keyExists($data, 'createdBy'); |
|
33
|
|
|
Assert::keyExists($data, 'lastUpdated'); |
|
34
|
|
|
Assert::isArray($data['createdBy']); |
|
35
|
|
|
Assert::isArray($data['lastUpdated']); |
|
36
|
|
|
|
|
37
|
|
|
Assert::keyExists($data['lastUpdated'], 'by'); |
|
38
|
|
|
Assert::isArray($data['lastUpdated']['by']); |
|
39
|
|
|
|
|
40
|
|
|
if(isset($data['latest'])) { |
|
41
|
|
|
Assert::boolean($data['latest']); |
|
42
|
|
|
$contentHistory->setLatest($data['latest']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$contentHistory->setCreatedDate(self::getDateTimeFromString($data['createdDate'])); |
|
46
|
|
|
$contentHistory->setCreatedBy(User::load($data['createdBy'])); |
|
47
|
|
|
$contentHistory->setUpdatedBy(User::load($data['lastUpdated']['by'])); |
|
48
|
|
|
|
|
49
|
|
|
$contentHistory->setUpdatedDate(self::getDateTimeFromString($data['lastUpdated']['when'])); |
|
50
|
|
|
|
|
51
|
|
|
$contentHistory->setLastVersionNumber($data['lastUpdated']['number']); |
|
52
|
|
|
|
|
53
|
|
|
if(isset($data['previousVersion'])) { |
|
54
|
|
|
Assert::isArray($data['previousVersion']); |
|
55
|
|
|
Assert::keyExists($data['previousVersion'], 'number'); |
|
56
|
|
|
$contentHistory->setPreviousVersionNumber($data['previousVersion']['number']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $contentHistory; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @throws HydrationException |
|
64
|
|
|
*/ |
|
65
|
|
|
private static function getDateTimeFromString(string $dateString): DateTimeInterface |
|
66
|
|
|
{ |
|
67
|
|
|
$dateTimeImmutable = DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.vZ', $dateString); |
|
68
|
|
|
if($dateTimeImmutable === false) { |
|
69
|
|
|
throw new HydrationException('Invalid date string: ' . $dateString); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $dateTimeImmutable; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function setLatest(bool $latest): ContentHistory |
|
76
|
|
|
{ |
|
77
|
|
|
$this->isLatest = $latest; |
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function setCreatedDate(DateTimeInterface $createFromFormat): ContentHistory |
|
82
|
|
|
{ |
|
83
|
|
|
$this->createdDate = $createFromFormat; |
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function setCreatedBy(User $user): ContentHistory |
|
88
|
|
|
{ |
|
89
|
|
|
$this->createdBy = $user; |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function setUpdatedBy(User $user): ContentHistory |
|
94
|
|
|
{ |
|
95
|
|
|
$this->updatedBy = $user; |
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function setUpdatedDate(DateTimeInterface $updatedDate): ContentHistory |
|
100
|
|
|
{ |
|
101
|
|
|
$this->updatedDate = $updatedDate; |
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getUpdatedDate(): DateTimeInterface |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->updatedDate; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getCreatedDate(): DateTimeInterface |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->createdDate; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function isLatest(): bool |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->isLatest; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function getCreatedBy(): User |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->createdBy; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getUpdatedBy(): User |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->updatedBy; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getLastVersionNumber(): int |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->lastVersionNumber; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function setLastVersionNumber(int $lastVersionNumber): void |
|
136
|
|
|
{ |
|
137
|
|
|
$this->lastVersionNumber = $lastVersionNumber; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function getPreviousVersionNumber(): ?int |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->previousVersionNumber; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function setPreviousVersionNumber(?int $previousVersionNumber): void |
|
146
|
|
|
{ |
|
147
|
|
|
$this->previousVersionNumber = $previousVersionNumber; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|