summaryrefslogtreecommitdiff
path: root/bin/reevotech/vendor/addwiki/mediawiki-datamodel/src/Pages.php
blob: b8c5614ce88de131690953e3444d93aae0fd3fdb (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
<?php

namespace Mediawiki\DataModel;

use InvalidArgumentException;
use RuntimeException;

/**
 * Represents a collection or Page classes
 * @author Addshore
 */
class Pages {

	/**
	 * @var Page[]
	 */
	private $pages;

	/**
	 * @param Page[] $pages
	 */
	public function __construct( $pages = array() ) {
		$this->pages = array();
		$this->addPages( $pages );
	}

	/**
	 * @param Page[]|Pages $pages
	 *
	 * @throws InvalidArgumentException
	 */
	public function addPages( $pages ) {
		if( !is_array( $pages ) && !$pages instanceof Pages ) {
			throw new InvalidArgumentException( '$pages needs to either be an array or a Pages object' );
		}
		if( $pages instanceof Pages ) {
			$pages = $pages->toArray();
		}
		foreach( $pages as $page ) {
			$this->addPage( $page );
		}
	}

	/**
	 * @param Page $page
	 */
	public function addPage( Page $page ) {
		$this->pages[$page->getId()] = $page;
	}

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

	/**
	 * @param Page $page
	 *
	 * @return bool
	 */
	public function hasPage( Page $page ){
		return array_key_exists( $page->getId(), $this->pages );
	}

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


	/**
	 * @param int $pageid
	 *
	 * @throws RuntimeException
	 * @return Page
	 */
	public function get( $pageid ){
		if( $this->hasPageWithId( $pageid ) ){
			return $this->pages[$pageid];
		}
		throw new RuntimeException( 'No such page loaded in Pages object' );
	}

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