summaryrefslogtreecommitdiff
path: root/bin/reevotech/vendor/addwiki/mediawiki-datamodel/src/Revisions.php
blob: c6d2f4367e806ed777b37664dc92d62ae40df8ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

namespace Mediawiki\DataModel;

use InvalidArgumentException;
use RuntimeException;

/**
 * Represents a collection or revisions
 * @author Addshore
 */
class Revisions {

	/**
	 * @var Revision[]
	 */
	private $revisions;

	/**
	 * @param Revisions[] $revisions
	 */
	public function __construct( $revisions = array() ) {
		$this->revisions = array();
		$this->addRevisions( $revisions );
	}

	/**
	 * @param Revision[]|Revisions $revisions
	 *
	 * @throws InvalidArgumentException
	 */
	public function addRevisions( $revisions ) {
		if( !is_array( $revisions ) && !$revisions instanceof Revisions ) {
			throw new InvalidArgumentException( '$revisions needs to either be an array or a Revisions object' );
		}
		if( $revisions instanceof Revisions ) {
			$revisions = $revisions->toArray();
		}
		foreach( $revisions as $revision ) {
			$this->addRevision( $revision );
		}
	}

	/**
	 * @param Revision $revision
	 */
	public function addRevision( Revision $revision ) {
		$this->revisions[$revision->getId()] = $revision;
	}

	/**
	 * @param int $id
	 *
	 * @return bool
	 */
	public function hasRevisionWithId( $id ){
		return array_key_exists( $id, $this->revisions );
	}

	/**
	 * @param Revision $revision
	 *
	 * @return bool
	 */
	public function hasRevision( Revision $revision ){
		return array_key_exists( $revision->getId(), $this->revisions );
	}

	/**
	 * @return Revision|null Revision or null if there is no revision
	 */
	public function getLatest() {
		if( empty( $this->revisions ) ) {
			return null;
		}
		return $this->revisions[ max( array_keys( $this->revisions ) ) ];
	}

	/**
	 * @param int $revid
	 *
	 * @throws RuntimeException
	 * @throws InvalidArgumentException
	 * @return Revision
	 */
	public function get( $revid ){
		if( !is_int( $revid ) ) {
			throw new InvalidArgumentException( '$revid needs to be an int' );
		}
		if( $this->hasRevisionWithId( $revid ) ){
			return $this->revisions[$revid];
		}
		throw new RuntimeException( 'No such revision loaded in Revisions object' );
	}

	/**
	 * @return Revision[]
	 */
	public function toArray() {
		return $this->revisions;
	}
}