ByPropertyIdGrouper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Wikibase\DataModel\Services;
4
5
use InvalidArgumentException;
6
use OutOfBoundsException;
7
use Traversable;
8
use Wikibase\DataModel\Entity\NumericPropertyId;
9
use Wikibase\DataModel\PropertyIdProvider;
10
11
/**
12
 * Groups property id providers by their property id.
13
 *
14
 * @since 1.0
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Bene* < [email protected] >
18
 */
19
class ByPropertyIdGrouper {
20
21
	/**
22
	 * @var array[]
23
	 */
24
	private $byPropertyId = [];
25
26
	/**
27
	 * @param PropertyIdProvider[]|Traversable $propertyIdProviders
28
	 *
29
	 * @throws InvalidArgumentException
30
	 */
31 21
	public function __construct( $propertyIdProviders ) {
32 21
		$this->assertArePropertyIdProviders( $propertyIdProviders );
33 15
		$this->indexPropertyIdProviders( $propertyIdProviders );
34 15
	}
35
36 21
	/**
37 21
	 * @param PropertyIdProvider[]|Traversable $propertyIdProviders
38 2
	 */
39
	private function assertArePropertyIdProviders( $propertyIdProviders ) {
40
		if ( !is_iterable( $propertyIdProviders ) ) {
41 19
			throw new InvalidArgumentException( '$propertyIdProviders must be an array or an instance of Traversable' );
42 16
		}
43 4
44
		foreach ( $propertyIdProviders as $propertyIdProvider ) {
45 4
			if ( !( $propertyIdProvider instanceof PropertyIdProvider ) ) {
46
				throw new InvalidArgumentException(
47 15
					'Every element in $propertyIdProviders must be an instance of PropertyIdProvider'
48 15
				);
49
			}
50
		}
51
	}
52
53 15
	/**
54 15
	 * @param PropertyIdProvider[]|Traversable $propertyIdProviders
55 12
	 */
56 15
	private function indexPropertyIdProviders( $propertyIdProviders ) {
57 15
		foreach ( $propertyIdProviders as $propertyIdProvider ) {
58
			$this->addPropertyIdProvider( $propertyIdProvider );
59 12
		}
60 12
	}
61
62 12
	private function addPropertyIdProvider( PropertyIdProvider $propertyIdProvider ) {
63 9
		$idSerialization = $propertyIdProvider->getPropertyId()->getSerialization();
64 9
		$this->byPropertyId[$idSerialization][] = $propertyIdProvider;
65 12
	}
66
67 12
	/**
68
	 * Returns all property ids which were found.
69
	 *
70
	 * @since 1.0
71
	 *
72
	 * @return NumericPropertyId[]
73
	 */
74
	public function getPropertyIds() {
75
		return array_map(
76 7
			static function( $propertyId ) {
77 7
				return new NumericPropertyId( $propertyId );
78 7
			},
79 4
			array_keys( $this->byPropertyId )
80 7
		);
81 7
	}
82 7
83
	/**
84
	 * Returns the PropertyIdProvider instances for the given NumericPropertyId.
85
	 *
86
	 * @since 1.0
87
	 *
88
	 * @param NumericPropertyId $propertyId
89
	 *
90
	 * @throws OutOfBoundsException
91
	 * @return PropertyIdProvider[]
92
	 */
93
	public function getByPropertyId( NumericPropertyId $propertyId ) {
94
		$idSerialization = $propertyId->getSerialization();
95 3
96 3
		if ( !isset( $this->byPropertyId[$idSerialization] ) ) {
97
			throw new OutOfBoundsException( 'PropertyIdProvider with propertyId "' . $idSerialization . '" not found' );
98 3
		}
99 1
100
		return $this->byPropertyId[$idSerialization];
101
	}
102 2
103
	/**
104
	 * Checks if there are PropertyIdProvider instances for the given NumericPropertyId.
105
	 *
106
	 * @since 1.0
107
	 *
108
	 * @param NumericPropertyId $propertyId
109
	 *
110
	 * @return bool
111
	 */
112
	public function hasPropertyId( NumericPropertyId $propertyId ) {
113
		return isset( $this->byPropertyId[$propertyId->getSerialization()] );
114 5
	}
115 5
116
}
117