1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediawiki\DataModel; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use JsonSerializable; |
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Represents a collection of Log classes |
11
|
|
|
* @author Addshore |
12
|
|
|
*/ |
13
|
|
|
class LogList implements JsonSerializable { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Log[] |
17
|
|
|
*/ |
18
|
|
|
private $logs; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Log[] $logs |
22
|
|
|
*/ |
23
|
1 |
|
public function __construct( $logs = array() ) { |
24
|
1 |
|
$this->logs = array(); |
25
|
1 |
|
$this->addLogs( $logs ); |
26
|
1 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Log[]|LogList $logs |
30
|
|
|
* |
31
|
|
|
* @throws InvalidArgumentException |
32
|
|
|
*/ |
33
|
1 |
|
public function addLogs( $logs ) { |
34
|
1 |
|
if( !is_array( $logs ) && !$logs instanceof LogList ) { |
35
|
|
|
throw new InvalidArgumentException( '$logs needs to either be an array or a LogList object' ); |
36
|
|
|
} |
37
|
1 |
|
if( $logs instanceof LogList ) { |
38
|
|
|
$logs = $logs->toArray(); |
39
|
|
|
} |
40
|
1 |
|
foreach( $logs as $log ) { |
41
|
1 |
|
$this->addLog( $log ); |
42
|
1 |
|
} |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Log $log |
47
|
|
|
*/ |
48
|
1 |
|
public function addLog( Log $log ) { |
49
|
1 |
|
$this->logs[$log->getId()] = $log; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int $id |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function hasLogWithId( $id ){ |
58
|
|
|
return array_key_exists( $id, $this->logs ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Log $log |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function hasLog( Log $log ){ |
67
|
|
|
return array_key_exists( $log->getId(), $this->logs ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return Log|null Log or null if there is no log |
72
|
|
|
*/ |
73
|
|
|
public function getLatest() { |
74
|
|
|
if( empty( $this->logs ) ) { |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
return $this->logs[ max( array_keys( $this->logs ) ) ]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @since 0.6 |
82
|
|
|
* @return Log|null Log or null if there is no log |
83
|
|
|
*/ |
84
|
|
|
public function getOldest() { |
85
|
|
|
if( empty( $this->logs ) ) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
return $this->logs[ min( array_keys( $this->logs ) ) ]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @since 0.6 |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function isEmpty() { |
96
|
|
|
return empty( $this->logs ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param int $id |
101
|
|
|
* |
102
|
|
|
* @throws RuntimeException |
103
|
|
|
* @return Log |
104
|
|
|
*/ |
105
|
|
|
public function get( $id ){ |
106
|
|
|
if( $this->hasLogWithId( $id ) ){ |
107
|
|
|
return $this->logs[$id]; |
108
|
|
|
} |
109
|
|
|
throw new RuntimeException( 'No such Log loaded in LogList object' ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return Log[] |
114
|
|
|
*/ |
115
|
1 |
|
public function toArray() { |
116
|
1 |
|
return $this->logs; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
121
|
|
|
*/ |
122
|
1 |
|
public function jsonSerialize() { |
123
|
1 |
|
return $this->toArray(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array $json |
128
|
|
|
* |
129
|
|
|
* @return self |
130
|
|
|
*/ |
131
|
1 |
|
public static function jsonDeserialize( $json ) { |
132
|
1 |
|
$self = new LogList(); |
133
|
1 |
|
foreach ( $json as $logJson ) { |
134
|
1 |
|
$self->addLog( Log::jsonDeserialize( $logJson ) ); |
135
|
1 |
|
} |
136
|
1 |
|
return $self; |
137
|
|
|
} |
138
|
|
|
} |