1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediawiki\DataModel; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Represents a collection or revisions |
10
|
|
|
* @author Addshore |
11
|
|
|
*/ |
12
|
|
|
class Revisions { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Revision[] |
16
|
|
|
*/ |
17
|
|
|
private $revisions; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Revisions[] $revisions |
21
|
|
|
*/ |
22
|
4 |
|
public function __construct( $revisions = array() ) { |
23
|
4 |
|
$this->revisions = array(); |
24
|
4 |
|
$this->addRevisions( $revisions ); |
|
|
|
|
25
|
4 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Revision[]|Revisions $revisions |
29
|
|
|
* |
30
|
|
|
* @throws InvalidArgumentException |
31
|
|
|
*/ |
32
|
4 |
|
public function addRevisions( $revisions ) { |
33
|
4 |
|
if( !is_array( $revisions ) && !$revisions instanceof Revisions ) { |
34
|
|
|
throw new InvalidArgumentException( '$revisions needs to either be an array or a Revisions object' ); |
35
|
|
|
} |
36
|
4 |
|
if( $revisions instanceof Revisions ) { |
37
|
1 |
|
$revisions = $revisions->toArray(); |
38
|
1 |
|
} |
39
|
4 |
|
foreach( $revisions as $revision ) { |
40
|
4 |
|
$this->addRevision( $revision ); |
41
|
4 |
|
} |
42
|
4 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Revision $revision |
46
|
|
|
*/ |
47
|
4 |
|
public function addRevision( Revision $revision ) { |
48
|
4 |
|
$this->revisions[$revision->getId()] = $revision; |
49
|
4 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param int $id |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function hasRevisionWithId( $id ){ |
57
|
|
|
return array_key_exists( $id, $this->revisions ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Revision $revision |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function hasRevision( Revision $revision ){ |
66
|
|
|
return array_key_exists( $revision->getId(), $this->revisions ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return Revision|null Revision or null if there is no revision |
71
|
|
|
*/ |
72
|
|
|
public function getLatest() { |
73
|
|
|
if( empty( $this->revisions ) ) { |
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
return $this->revisions[ max( array_keys( $this->revisions ) ) ]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $revid |
81
|
|
|
* |
82
|
|
|
* @throws RuntimeException |
83
|
|
|
* @throws InvalidArgumentException |
84
|
|
|
* @return Revision |
85
|
|
|
*/ |
86
|
|
|
public function get( $revid ){ |
87
|
|
|
if( !is_int( $revid ) ) { |
88
|
|
|
throw new InvalidArgumentException( '$revid needs to be an int' ); |
89
|
|
|
} |
90
|
|
|
if( $this->hasRevisionWithId( $revid ) ){ |
91
|
|
|
return $this->revisions[$revid]; |
92
|
|
|
} |
93
|
|
|
throw new RuntimeException( 'No such revision loaded in Revisions object' ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return Revision[] |
98
|
|
|
*/ |
99
|
4 |
|
public function toArray() { |
100
|
4 |
|
return $this->revisions; |
101
|
|
|
} |
102
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: