1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediawiki\DataModel; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Represents a collection or revisions |
10
|
|
|
* |
11
|
|
|
* @author Addshore |
12
|
|
|
*/ |
13
|
|
|
class Revisions { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Revision[] |
17
|
|
|
*/ |
18
|
|
|
private $revisions; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Revisions[] $revisions |
22
|
4 |
|
*/ |
23
|
4 |
|
public function __construct( $revisions = [] ) { |
24
|
4 |
|
$this->revisions = []; |
25
|
4 |
|
$this->addRevisions( $revisions ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Revision[]|Revisions $revisions |
30
|
|
|
* |
31
|
|
|
* @throws InvalidArgumentException |
32
|
4 |
|
*/ |
33
|
4 |
View Code Duplication |
public function addRevisions( $revisions ) { |
|
|
|
|
34
|
|
|
if ( !is_array( $revisions ) && !$revisions instanceof Revisions ) { |
35
|
|
|
throw new InvalidArgumentException( '$revisions needs to either be an array or a Revisions object' ); |
36
|
4 |
|
} |
37
|
1 |
|
if ( $revisions instanceof Revisions ) { |
38
|
1 |
|
$revisions = $revisions->toArray(); |
39
|
4 |
|
} |
40
|
4 |
|
foreach ( $revisions as $revision ) { |
41
|
4 |
|
$this->addRevision( $revision ); |
42
|
4 |
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Revision $revision |
47
|
4 |
|
*/ |
48
|
4 |
|
public function addRevision( Revision $revision ) { |
49
|
4 |
|
$this->revisions[$revision->getId()] = $revision; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int $id |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function hasRevisionWithId( $id ) { |
58
|
|
|
return array_key_exists( $id, $this->revisions ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Revision $revision |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function hasRevision( Revision $revision ) { |
67
|
|
|
return array_key_exists( $revision->getId(), $this->revisions ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return Revision|null Revision or null if there is no revision |
72
|
|
|
*/ |
73
|
|
|
public function getLatest() { |
74
|
|
|
if ( empty( $this->revisions ) ) { |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
return $this->revisions[ max( array_keys( $this->revisions ) ) ]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param int $revid |
82
|
|
|
* |
83
|
|
|
* @throws RuntimeException |
84
|
|
|
* @throws InvalidArgumentException |
85
|
|
|
* @return Revision |
86
|
|
|
*/ |
87
|
|
|
public function get( $revid ) { |
88
|
|
|
if ( !is_int( $revid ) ) { |
89
|
|
|
throw new InvalidArgumentException( '$revid needs to be an int' ); |
90
|
|
|
} |
91
|
|
|
if ( $this->hasRevisionWithId( $revid ) ) { |
92
|
|
|
return $this->revisions[$revid]; |
93
|
|
|
} |
94
|
|
|
throw new RuntimeException( 'No such revision loaded in Revisions object' ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return Revision[] |
99
|
4 |
|
*/ |
100
|
4 |
|
public function toArray() { |
101
|
|
|
return $this->revisions; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.