AliasGroupFallback::equals()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
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-or-later
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 15
	public function __construct(
40 15
		$requestedLanguageCode,
41
		array $aliases,
42 15
		$actualLanguageCode,
43 3
		$sourceLanguageCode
44
	) {
45
		parent::__construct( $requestedLanguageCode, $aliases );
46
47 12
		if ( !is_string( $actualLanguageCode ) || $actualLanguageCode === '' ) {
48 12
			throw new InvalidArgumentException( '$actualLanguageCode must be a non-empty string' );
49 2
		}
50
51
		if ( !( $sourceLanguageCode === null
52 10
			|| ( is_string( $sourceLanguageCode ) && $sourceLanguageCode !== '' )
53 10
		) ) {
54 10
			throw new InvalidArgumentException( '$sourceLanguageCode must be a non-empty string or null' );
55
		}
56
57
		$this->actualLanguageCode = $actualLanguageCode;
58
		$this->sourceLanguageCode = $sourceLanguageCode;
59 1
	}
60 1
61
	/**
62
	 * @return string
63
	 */
64
	public function getActualLanguageCode() {
65
		return $this->actualLanguageCode;
66 2
	}
67 2
68
	/**
69
	 * @return string|null
70
	 */
71
	public function getSourceLanguageCode() {
72
		return $this->sourceLanguageCode;
73
	}
74
75
	/**
76
	 *
77 8
	 * @param mixed $target
78 8
	 *
79 1
	 * @return bool
80
	 */
81
	public function equals( $target ) {
82
		if ( $this === $target ) {
83 8
			return true;
84 8
		}
85 8
86
		return $target instanceof self
87
			&& parent::equals( $target )
88
			&& $this->actualLanguageCode === $target->actualLanguageCode
89
			&& $this->sourceLanguageCode === $target->sourceLanguageCode;
90
	}
91
92
}
93