Completed
Push — main ( 1f6f71...689fbc )
by
unknown
04:30
created

PageIdentifier   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 75
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A getId() 0 3 1
A getTitle() 0 3 1
A identifiesPage() 0 3 2
A jsonSerialize() 0 10 3
A jsonDeserialize() 0 7 2
1
<?php
2
3
namespace Addwiki\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
	public function __construct( Title $title = null, $id = null ) {
26
		if ( !is_int( $id ) && $id !== null ) {
27
			throw new InvalidArgumentException( '$id must be an int' );
28
		}
29
		$this->title = $title;
30
		$this->id = $id;
31
	}
32
33
	/**
34
	 * @return int|null
35
	 */
36
	public function getId() {
37
		return $this->id;
38
	}
39
40
	/**
41
	 * @return Title|null
42
	 */
43
	public function getTitle() {
44
		return $this->title;
45
	}
46
47
	/**
48
	 * Does this object identify a page
49
	 *
50
	 * @return bool
51
	 */
52
	public function identifiesPage() {
53
		return !( !$this->title instanceof Title && $this->id === null );
54
	}
55
56
	/**
57
	 * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
58
	 */
59
	public function jsonSerialize() {
60
		$array = [];
61
		if ( $this->id !== null ) {
62
			$array['id'] = $this->id;
63
		}
64
		if ( $this->title !== null ) {
65
			$array['title'] = $this->title->jsonSerialize();
66
		}
67
		return $array;
68
	}
69
70
	/**
71
	 * @param array $array
72
	 *
73
	 * @return self
74
	 */
75
	public static function jsonDeserialize( $array ) {
76
		return new self(
77
		isset( $array['title'] ) ? Title::jsonDeserialize( $array['title'] ) : null,
78
		$array['id'] ?? null
79
80
		);
81
	}
82
}
83