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

Title::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Addwiki\Mediawiki\DataModel;
4
5
use InvalidArgumentException;
6
use JsonSerializable;
7
8
/**
9
 * @author Addshore
10
 */
11
class Title implements JsonSerializable {
12
13
	/**
14
	 * @var string
15
	 */
16
	private $title;
17
18
	/**
19
	 * @var int
20
	 */
21
	private $ns;
22
23
	/**
24
	 * @param string $title
25
	 * @param int $ns
26
	 *
27
	 * @throws InvalidArgumentException
28
	 */
29
	public function __construct( $title, $ns = 0 ) {
30
		if ( !is_string( $title ) ) {
31
			throw new InvalidArgumentException( '$title must be a string' );
32
		}
33
		if ( !is_int( $ns ) ) {
34
			throw new InvalidArgumentException( '$ns must be an int' );
35
		}
36
		$this->title = $title;
37
		$this->ns = $ns;
38
	}
39
40
	/**
41
	 * @return int
42
	 * @since 0.1
43
	 */
44
	public function getNs() {
45
		return $this->ns;
46
	}
47
48
	/**
49
	 * @return string
50
	 * @since 0.6
51
	 */
52
	public function getText() {
53
		return $this->title;
54
	}
55
56
	/**
57
	 * @return string
58
	 * @deprecated in 0.6 use getText (makes things look cleaner)
59
	 */
60
	public function getTitle() {
61
		return $this->getText();
62
	}
63
64
	/**
65
	 * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
66
	 */
67
	public function jsonSerialize() {
68
		return [
69
		'title' => $this->title,
70
		'ns' => $this->ns,
71
		];
72
	}
73
74
	/**
75
	 * @param array $json
76
	 *
77
	 * @return self
78
	 */
79
	public static function jsonDeserialize( $json ) {
80
		return new self( $json['title'], $json['ns'] );
81
	}
82
83
}
84