FilteringStatementGrouper::getKey()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Wikibase\DataModel\Services\Statement\Grouper;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Statement\Statement;
7
use Wikibase\DataModel\Statement\StatementFilter;
8
use Wikibase\DataModel\Statement\StatementList;
9
10
/**
11
 * @since 3.2
12
 *
13
 * @license GPL-2.0-or-later
14
 * @author Thiemo Kreuz
15
 */
16
class FilteringStatementGrouper implements StatementGrouper {
17
18
	/**
19
	 * @var StatementFilter[] An associative array, mapping statement group identifiers to filters.
20
	 */
21
	private $filters = [];
22
23
	/**
24
	 * @var string[]
25
	 */
26
	private $groupIdentifiers = [];
27
28
	/**
29
	 * @var string
30
	 */
31
	private $defaultGroupIdentifier = null;
32
33
	/**
34
	 * @see StatementFilter
35
	 *
36
	 * @param array $filters An associative array, mapping statement group identifiers to either
37
	 *  StatementFilter objects, or to null for the default group.
38
	 *
39
	 * @throws InvalidArgumentException
40
	 */
41
	public function __construct( array $filters ) {
42
		foreach ( $filters as $key => $filter ) {
43 7
			if ( $filter instanceof StatementFilter ) {
44
				$this->filters[$key] = $filter;
45 7
			} elseif ( $filter === null ) {
46 6
				$this->setDefaultGroupIdentifier( $key );
47 6
			} else {
48 7
				throw new InvalidArgumentException( '$filter must be a StatementFilter or null' );
49
			}
50 6
51 4
			$this->setGroupIdentifier( $key );
52 4
		}
53 4
54 6
		$this->initializeDefaultGroup();
55
	}
56
57
	/**
58
	 * @param string $key
59
	 */
60 6
	private function setGroupIdentifier( $key ) {
61 6
		$this->groupIdentifiers[$key] = $key;
62 3
	}
63 1
64
	/**
65 3
	 * @param string $key
66 6
	 *
67 4
	 * @throws InvalidArgumentException
68 4
	 */
69 6
	private function setDefaultGroupIdentifier( $key ) {
70
		if ( $this->defaultGroupIdentifier !== null ) {
71
			throw new InvalidArgumentException( 'You must only define one default group' );
72
		}
73
74
		$this->defaultGroupIdentifier = $key;
75
	}
76
77
	private function initializeDefaultGroup() {
78 6
		if ( $this->defaultGroupIdentifier === null ) {
79 6
			$this->defaultGroupIdentifier = 'statements';
80
			$this->setGroupIdentifier( $this->defaultGroupIdentifier );
81 6
		}
82 3
	}
83 3
84 6
	/**
85
	 * @param StatementList $statements
86 6
	 *
87
	 * @return StatementList[] An associative array, mapping statement group identifiers to
88
	 *  StatementList objects. All identifiers given in the constructor are guaranteed to be in the
89 6
	 *  result.
90 6
	 */
91
	public function groupStatements( StatementList $statements ) {
92 6
		$groups = $this->getEmptyGroups();
93 6
94 6
		foreach ( $statements->toArray() as $statement ) {
95 6
			$key = $this->getKey( $statement );
96
			$groups[$key]->addStatement( $statement );
97
		}
98
99
		return $groups;
100
	}
101
102
	/**
103 3
	 * @return StatementList[]
104 3
	 */
105 3
	private function getEmptyGroups() {
106 3
		$groups = [];
107
108 1
		foreach ( $this->groupIdentifiers as $key ) {
109
			$groups[$key] = new StatementList();
110
		}
111
112
		return $groups;
113
	}
114
115
	/**
116
	 * @param Statement $statement
117
	 *
118
	 * @return string Statement group identifier
119
	 */
120
	private function getKey( Statement $statement ) {
121
		foreach ( $this->filters as $key => $filter ) {
122
			if ( $filter->statementMatches( $statement ) ) {
123
				return $key;
124
			}
125
		}
126
127
		return $this->defaultGroupIdentifier;
128
	}
129
130
}
131