DataTypeStatementFilter::statementMatches()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 1
cts 1
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Wikibase\DataModel\Services\Statement\Filter;
4
5
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
6
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookupException;
7
use Wikibase\DataModel\Statement\Statement;
8
use Wikibase\DataModel\Statement\StatementFilter;
9
10
/**
11
 * A filter that only accepts statements with specific property data types, and rejects all other
12
 * property data types.
13
 *
14
 * @since 3.2
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Thiemo Kreuz
18
 */
19
class DataTypeStatementFilter implements StatementFilter {
20
21
	/**
22
	 * @since 3.3
23
	 */
24
	public const FILTER_TYPE = 'dataType';
25
26
	/**
27
	 * @var PropertyDataTypeLookup
28
	 */
29
	private $dataTypeLookup;
30
31
	/**
32 10
	 * @var string[]
33 10
	 */
34 10
	private $dataTypes;
35 10
36
	/**
37
	 * @param PropertyDataTypeLookup $dataTypeLookup
38
	 * @param string[]|string $dataTypes One or more property data type identifiers.
39
	 */
40
	public function __construct( PropertyDataTypeLookup $dataTypeLookup, $dataTypes ) {
41
		$this->dataTypeLookup = $dataTypeLookup;
42
		$this->dataTypes = (array)$dataTypes;
43
	}
44 10
45 10
	/**
46
	 * @see StatementFilter::statementMatches
47
	 *
48 10
	 * @param Statement $statement
49 10
	 *
50 1
	 * @return bool
51
	 */
52
	public function statementMatches( Statement $statement ) {
53 9
		$id = $statement->getPropertyId();
54
55
		try {
56
			$dataType = $this->dataTypeLookup->getDataTypeIdForProperty( $id );
57
		} catch ( PropertyDataTypeLookupException $ex ) {
58
			return false;
59
		}
60
61
		return in_array( $dataType, $this->dataTypes );
62
	}
63
64
}
65