Completed
Pull Request — master (#5)
by Sam
01:46
created

Pages::addPages()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.0488

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 8
cp 0.875
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 5
nop 1
crap 5.0488
1
<?php
2
3
namespace Mediawiki\DataModel;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
8
/**
9
 * Represents a collection or Page classes
10
 * @author Addshore
11
 */
12
class Pages {
13
14
	/**
15
	 * @var Page[]
16
	 */
17
	private $pages;
18
19
	/**
20
	 * @param Page[] $pages
21
	 */
22 4
	public function __construct( $pages = array() ) {
23 4
		$this->pages = array();
24 4
		$this->addPages( $pages );
25 4
	}
26
27
	/**
28
	 * @param Page[]|Pages $pages
29
	 *
30
	 * @throws InvalidArgumentException
31
	 */
32 4
	public function addPages( $pages ) {
33 4
		if( !is_array( $pages ) && !$pages instanceof Pages ) {
34
			throw new InvalidArgumentException( '$pages needs to either be an array or a Pages object' );
35
		}
36 4
		if( $pages instanceof Pages ) {
37 1
			$pages = $pages->toArray();
38
		}
39 4
		foreach( $pages as $page ) {
40 4
			$this->addPage( $page );
41
		}
42 4
	}
43
44
	/**
45
	 * @param Page $page
46
	 */
47 4
	public function addPage( Page $page ) {
48 4
		$this->pages[$page->getId()] = $page;
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Page::getId() has been deprecated with message: since 0.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
49 4
	}
50
51
	/**
52
	 * @param int $id
53
	 *
54
	 * @return bool
55
	 */
56
	public function hasPageWithId( $id ){
57
		return array_key_exists( $id, $this->pages );
58
	}
59
60
	/**
61
	 * @param Page $page
62
	 *
63
	 * @return bool
64
	 */
65
	public function hasPage( Page $page ){
66
		return array_key_exists( $page->getId(), $this->pages );
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Page::getId() has been deprecated with message: since 0.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
67
	}
68
69
	/**
70
	 * @return Page|null Page or null if there is no page
71
	 */
72
	public function getLatest() {
73
		if( empty( $this->pages ) ) {
74
			return null;
75
		}
76
		return $this->pages[ max( array_keys( $this->pages ) ) ];
77
	}
78
79
80
	/**
81
	 * @param int $pageid
82
	 *
83
	 * @throws RuntimeException
84
	 * @return Page
85
	 */
86
	public function get( $pageid ){
87
		if( $this->hasPageWithId( $pageid ) ){
88
			return $this->pages[$pageid];
89
		}
90
		throw new RuntimeException( 'No such page loaded in Pages object' );
91
	}
92
93
	/**
94
	 * @return Page[]
95
	 */
96 4
	public function toArray() {
97 4
		return $this->pages;
98
	}
99
}