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

PageIdentifier::jsonSerialize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 0
crap 3
1
<?php
2
3
namespace Mediawiki\DataModel;
4
5
use InvalidArgumentException;
6
use JsonSerializable;
7
8
class PageIdentifier implements JsonSerializable {
9
10
	/**
11
	 * @var int|null
12
	 */
13
	private $id;
14
15
	/**
16
	 * @var Title|null
17
	 */
18
	private $title;
19
20
	/**
21
	 * @param Title|null $title
22
	 * @param int|null $id
23
	 * @throws InvalidArgumentException
24
	 */
25 8
	public function __construct( Title $title = null, $id = null ) {
26 8
		if( !is_int( $id ) && !is_null( $id ) ) {
27
			throw new InvalidArgumentException( '$id must be an int' );
28
		}
29 8
		$this->title = $title;
30 8
		$this->id = $id;
31 8
	}
32
33
	/**
34
	 * @return int|null
35
	 */
36 4
	public function getId() {
37 4
		return $this->id;
38
	}
39
40
	/**
41
	 * @return Title|null
42
	 */
43 4
	public function getTitle() {
44 4
		return $this->title;
45
	}
46
47
	/**
48
	 * Does this object identify a page
49
	 * @return bool
50
	 */
51 4
	public function identifiesPage() {
52 4
		if( is_null( $this->title ) && is_null( $this->id ) ) {
53 1
			return false;
54
		}
55 3
		return true;
56
	}
57
58
	/**
59
	 * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
60
	 */
61 4
	public function jsonSerialize() {
62 4
		$array = array();
63 4
		if ( $this->id !== null ) {
64 2
			$array['id'] = $this->id;
65
		}
66 4
		if ( $this->title !== null ) {
67 2
			$array['title'] = $this->title->jsonSerialize();
68
		}
69 4
		return $array;
70
	}
71
72
	/**
73
	 * @param array $array
74
	 *
75
	 * @returns self
76
	 */
77 4
	public static function jsonDeserialize( $array ) {
78 4
		return new self(
79 4
			isset( $array['title'] ) ? Title::jsonDeserialize( $array['title'] ) : null,
80 4
			isset( $array['id'] ) ? $array['id'] : null
81
82
		);
83
	}
84
}
85