Passed
Push — Facets ( b72148...68bbc0 )
by Daniel
07:49 queued 03:58
created

Term::listFacets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Wikibase\DataModel\Term;
4
5
use Comparable;
6
use InvalidArgumentException;
7
use Wikibase\DataModel\Facet\FacetContainer;
8
use Wikibase\DataModel\Facet\NoSuchFacetException;
9
use Wikibase\DataModel\Internal\FacetManager;
10
11
/**
12
 * Immutable value object.
13
 *
14
 * @since 0.7.3
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 * @author Daniel Kinzler
19
 */
20
class Term implements Comparable, FacetContainer {
21
22
	/**
23
	 * @var string Language code identifying the language of the text, but note that there is
24
	 * nothing this class can do to enforce this convention.
25
	 */
26
	private $languageCode;
27
28
	/**
29
	 * @var string
30
	 */
31
	private $text;
32
33
	/**
34
	 * @var FacetManager
35
	 */
36
	private $facetManager;
37
38
	/**
39
	 * @param string $languageCode Language of the text.
40
	 * @param string $text
41
	 *
42
	 * @throws InvalidArgumentException
43
	 */
44 13
	public function __construct( $languageCode, $text ) {
45 13
		if ( !is_string( $languageCode ) || $languageCode === '' ) {
46 3
			throw new InvalidArgumentException( '$languageCode must be a non-empty string' );
47
		}
48
49 10
		if ( !is_string( $text ) ) {
50 4
			throw new InvalidArgumentException( '$text must be a string' );
51
		}
52
53 6
		$this->languageCode = $languageCode;
54 6
		$this->text = $text;
55 6
	}
56
57
	/**
58
	 * @return string
59
	 */
60 1
	public function getLanguageCode() {
61 1
		return $this->languageCode;
62
	}
63
64
	/**
65
	 * @return string
66
	 */
67 1
	public function getText() {
68 1
		return $this->text;
69
	}
70
71
	/**
72
	 * @see Comparable::equals
73
	 *
74
	 * @param mixed $target
75
	 *
76
	 * @return bool
77
	 */
78 1
	public function equals( $target ) {
79 1
		if ( $this === $target ) {
80 1
			return true;
81
		}
82
83 1
		return is_object( $target )
84 1
			&& get_called_class() === get_class( $target )
85 1
			&& $this->languageCode === $target->languageCode
86 1
			&& $this->text === $target->text;
87
	}
88
89
	/**
90
	 * @param string $name
91
	 *
92
	 * @return boolean
93
	 */
94 1
	public function hasFacet( $name ) {
95 1
		return $this->facetManager && $this->facetManager->hasFacet( $name );
96
	}
97
98
	/**
99
	 * @return string[]
100
	 */
101 1
	public function listFacets() {
102 1
		return $this->facetManager ? $this->facetManager->listFacets() : array();
103
	}
104
105
	/**
106
	 * @param string $name
107
	 * @param string|null $type The desired type
108
	 *
109
	 * @return object
110
	 */
111 2
	public function getFacet( $name, $type = null ) {
112 2
		if ( !$this->facetManager ) {
113
			throw new NoSuchFacetException( $name );
114
		}
115
116 2
		return $this->facetManager->getFacet( $name, $type );
117
	}
118
119
	/**
120
	 * @param string $name
121
	 * @param object $facetObject
122
	 */
123 4
	public function addFacet( $name, $facetObject ) {
124 4
		if ( !$this->facetManager ) {
125 4
			$this->facetManager = new FacetManager();
126 4
		}
127
128 4
		$this->facetManager->addFacet( $name, $facetObject );
129 4
	}
130
131
}
132