Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
TextRevisions | |
100.00% |
14 / 14 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
toArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLatest | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
calculateLatestKey | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace FileImporter\Data; |
4 | |
5 | use Wikimedia\Assert\Assert; |
6 | |
7 | /** |
8 | * @license GPL-2.0-or-later |
9 | * @author Addshore |
10 | */ |
11 | class TextRevisions { |
12 | |
13 | /** @var TextRevision[] */ |
14 | private array $textRevisions; |
15 | /** @var int|null */ |
16 | private $latestKey = null; |
17 | |
18 | /** |
19 | * @param TextRevision[] $textRevisions |
20 | */ |
21 | public function __construct( array $textRevisions ) { |
22 | Assert::parameter( $textRevisions !== [], '$textRevisions', 'cannot be empty' ); |
23 | Assert::parameterElementType( TextRevision::class, $textRevisions, '$textRevisions' ); |
24 | $this->textRevisions = $textRevisions; |
25 | } |
26 | |
27 | /** |
28 | * @return TextRevision[] |
29 | */ |
30 | public function toArray(): array { |
31 | return $this->textRevisions; |
32 | } |
33 | |
34 | /** |
35 | * @return TextRevision|null |
36 | */ |
37 | public function getLatest() { |
38 | $this->latestKey ??= $this->calculateLatestKey(); |
39 | return $this->latestKey !== null ? $this->textRevisions[$this->latestKey] : null; |
40 | } |
41 | |
42 | private function calculateLatestKey(): ?int { |
43 | $latestTimestamp = 0; |
44 | $latestKey = null; |
45 | foreach ( $this->textRevisions as $key => $revision ) { |
46 | $timestamp = strtotime( $revision->getField( 'timestamp' ) ); |
47 | if ( $latestTimestamp < $timestamp ) { |
48 | $latestTimestamp = $timestamp; |
49 | $latestKey = $key; |
50 | } |
51 | } |
52 | return $latestKey; |
53 | } |
54 | |
55 | } |