TermFallback::__construct()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.1111
c 0
b 0
f 0
cc 6
nc 3
nop 4
1
<?php
2
3
namespace Wikibase\DataModel\Term;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Immutable value object.
9
 *
10
 * @since 2.4.0
11
 *
12
 * @license GPL-2.0-or-later
13
 * @author Jan Zerebecki < [email protected] >
14
 */
15
class TermFallback extends Term {
16
17
	/**
18
	 * @var string Actual language of the text.
19
	 */
20
	private $actualLanguageCode;
21
22
	/**
23
	 * @var string|null Source language if the text is a transliteration.
24
	 */
25
	private $sourceLanguageCode;
26
27
	/**
28
	 * @param string $requestedLanguageCode Requested language, not necessarily the language of the
29
	 * text.
30
	 * @param string $text
31
	 * @param string $actualLanguageCode Actual language of the text.
32
	 * @param string|null $sourceLanguageCode Source language if the text is a transliteration.
33
	 *
34
	 * @throws InvalidArgumentException
35
	 */
36
	public function __construct( $requestedLanguageCode, $text, $actualLanguageCode, $sourceLanguageCode ) {
37
		parent::__construct( $requestedLanguageCode, $text );
38
39
		if ( !is_string( $actualLanguageCode ) || $actualLanguageCode === '' ) {
40
			throw new InvalidArgumentException( '$actualLanguageCode must be a non-empty string' );
41
		}
42
43
		if ( !( $sourceLanguageCode === null
44
			|| ( is_string( $sourceLanguageCode ) && $sourceLanguageCode !== '' )
45
		) ) {
46
			throw new InvalidArgumentException( '$sourceLanguageCode must be a non-empty string or null' );
47
		}
48
49
		$this->actualLanguageCode = $actualLanguageCode;
50
		$this->sourceLanguageCode = $sourceLanguageCode;
51
	}
52
53
	/**
54
	 * @return string
55
	 */
56
	public function getActualLanguageCode() {
57
		return $this->actualLanguageCode;
58
	}
59
60
	/**
61
	 * @return string|null
62
	 */
63
	public function getSourceLanguageCode() {
64
		return $this->sourceLanguageCode;
65
	}
66
67
	/**
68
	 *
69
	 * @param mixed $target
70
	 *
71
	 * @return bool
72
	 */
73
	public function equals( $target ) {
74
		if ( $this === $target ) {
75
			return true;
76
		}
77
78
		return $target instanceof self
79
			&& parent::equals( $target )
80
			&& $this->actualLanguageCode === $target->actualLanguageCode
81
			&& $this->sourceLanguageCode === $target->sourceLanguageCode;
82
	}
83
84
}
85