Passed
Push — pureRelease610 ( 3f6616...93fb4a )
by no
03:51
created

AliasGroupFallback::__construct()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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