|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase |
|
2
|
|
|
/** |
|
3
|
|
|
* Class representing a changelog. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-changelogger |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
// phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
|
9
|
|
|
|
|
10
|
|
|
namespace Automattic\Jetpack\Changelog; |
|
11
|
|
|
|
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
use JsonSerializable; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class representing a changelog. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Changelog implements JsonSerializable { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Content before the changelog itself. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $prologue = ''; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Content after the changelog itself. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $epilogue = ''; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Changelog entries, one per version. |
|
36
|
|
|
* |
|
37
|
|
|
* @var ChangelogEntry[] |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $entries = array(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the prologue content. |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getPrologue() { |
|
47
|
|
|
return $this->prologue; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Set the prologue content. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $prologue Prologue content to set. |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setPrologue( $prologue ) { |
|
57
|
|
|
$this->prologue = (string) $prologue; |
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the epilogue content. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getEpilogue() { |
|
67
|
|
|
return $this->epilogue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set the epilogue content. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $epilogue Epilogue content to set. |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setEpilogue( $epilogue ) { |
|
77
|
|
|
$this->epilogue = (string) $epilogue; |
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the list of changelog entries. |
|
83
|
|
|
* |
|
84
|
|
|
* @return ChangelogEntry[] |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getEntries() { |
|
87
|
|
|
return $this->entries; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Set the list of changelog entries. |
|
92
|
|
|
* |
|
93
|
|
|
* This replaces all existing entries. |
|
94
|
|
|
* |
|
95
|
|
|
* @param ChangelogEntry[] $entries Changelog entries. |
|
96
|
|
|
* @return $this |
|
97
|
|
|
* @throws InvalidArgumentException If an argument is invalid. |
|
98
|
|
|
*/ |
|
99
|
|
View Code Duplication |
public function setEntries( array $entries ) { |
|
100
|
|
|
foreach ( $entries as $i => $entry ) { |
|
101
|
|
|
if ( ! $entry instanceof ChangelogEntry ) { |
|
102
|
|
|
$what = is_object( $entry ) ? get_class( $entry ) : gettype( $entry ); |
|
103
|
|
|
throw new InvalidArgumentException( __METHOD__ . ": Expected a ChangelogEntry, got $what at index $i" ); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
$this->entries = array_values( $entries ); |
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the latest changelog entry. |
|
112
|
|
|
* |
|
113
|
|
|
* @return ChangelogEntry|null |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getLatestEntry() { |
|
116
|
|
|
return isset( $this->entries[0] ) ? $this->entries[0] : null; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Add a new entry as the latest. |
|
121
|
|
|
* |
|
122
|
|
|
* @param ChangelogEntry $entry New entry. |
|
123
|
|
|
* @return $this |
|
124
|
|
|
*/ |
|
125
|
|
|
public function addEntry( ChangelogEntry $entry ) { |
|
126
|
|
|
array_unshift( $this->entries, $entry ); |
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get the list of versions in the changelog. |
|
132
|
|
|
* |
|
133
|
|
|
* @return string[] |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getVersions() { |
|
136
|
|
|
$ret = array(); |
|
137
|
|
|
foreach ( $this->entries as $entry ) { |
|
138
|
|
|
$ret[] = $entry->getVersion(); |
|
139
|
|
|
} |
|
140
|
|
|
return $ret; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Return data for serializing to JSON. |
|
145
|
|
|
* |
|
146
|
|
|
* @return array |
|
147
|
|
|
*/ |
|
148
|
|
|
public function jsonSerialize() { |
|
149
|
|
|
return array( |
|
150
|
|
|
'__class__' => static::class, |
|
151
|
|
|
'prologue' => $this->prologue, |
|
152
|
|
|
'epilogue' => $this->epilogue, |
|
153
|
|
|
'entries' => $this->entries, |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Unserialize from JSON. |
|
159
|
|
|
* |
|
160
|
|
|
* @param array $data JSON data as returned by self::jsonSerialize(). |
|
161
|
|
|
* @return static |
|
162
|
|
|
* @throws InvalidArgumentException If the data is invalid. |
|
163
|
|
|
*/ |
|
164
|
|
|
public static function jsonUnserialize( $data ) { |
|
165
|
|
|
$data = (array) $data; |
|
166
|
|
|
if ( ! isset( $data['__class__'] ) ) { |
|
167
|
|
|
throw new InvalidArgumentException( 'Invalid data' ); |
|
168
|
|
|
} |
|
169
|
|
|
$class = $data['__class__']; |
|
170
|
|
|
unset( $data['__class__'] ); |
|
171
|
|
View Code Duplication |
if ( ! class_exists( $class ) || ! is_a( $class, static::class, true ) ) { |
|
172
|
|
|
throw new InvalidArgumentException( "Cannot instantiate $class via " . static::class . '::' . __FUNCTION__ ); |
|
173
|
|
|
} |
|
174
|
|
|
$ret = new $class(); |
|
175
|
|
|
if ( isset( $data['prologue'] ) ) { |
|
176
|
|
|
$ret->setPrologue( $data['prologue'] ); |
|
177
|
|
|
} |
|
178
|
|
|
if ( isset( $data['epilogue'] ) ) { |
|
179
|
|
|
$ret->setEpilogue( $data['epilogue'] ); |
|
180
|
|
|
} |
|
181
|
|
|
if ( isset( $data['entries'] ) ) { |
|
182
|
|
|
$ret->setEntries( array_map( array( ChangelogEntry::class, 'jsonUnserialize' ), $data['entries'] ) ); |
|
183
|
|
|
} |
|
184
|
|
|
return $ret; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|