Skip to content

Commit 5c99a97

Browse files
committed
Create ByPropertyIdMap
1 parent 1e085d2 commit 5c99a97

2 files changed

Lines changed: 451 additions & 0 deletions

File tree

src/ByPropertyIdMap.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace Wikibase\DataModel;
4+
5+
use InvalidArgumentException;
6+
use OutOfBoundsException;
7+
use Traversable;
8+
use Wikibase\DataModel\Entity\PropertyId;
9+
10+
/**
11+
* Maps property id providers by their property id.
12+
*
13+
* @since 3.0
14+
*
15+
* @license GNU GPL v2+
16+
* @author Bene* < benestar.wikimedia@gmail.com >
17+
*/
18+
class ByPropertyIdMap {
19+
20+
/**
21+
* @var array[]
22+
*/
23+
private $byPropertyId = array();
24+
25+
/**
26+
* @var PropertyId[]
27+
*/
28+
private $propertyIds = array();
29+
30+
/**
31+
* @param PropertyIdProvider[]|Traversable $propertyIdProviders
32+
*
33+
* @throws InvalidArgumentException
34+
*/
35+
public function __construct( $propertyIdProviders ) {
36+
$byPropertyIdGrouper = new ByPropertyIdGrouper( $propertyIdProviders );
37+
38+
$this->propertyIds = $byPropertyIdGrouper->getPropertyIds();
39+
foreach ( $this->propertyIds as $propertyId ) {
40+
$this->byPropertyId[$propertyId->getSerialization()] = $byPropertyIdGrouper->getByPropertyId( $propertyId );
41+
}
42+
}
43+
44+
/**
45+
* @param PropertyId $propertyId
46+
* @param int|null $index
47+
*
48+
* @throws InvalidArgumentException
49+
* @throws OutOfBoundsException
50+
*/
51+
public function moveGroupToIndex( PropertyId $propertyId, $index ) {
52+
$this->assertIsIndex( $index );
53+
54+
$oldIndex = array_search( $propertyId, $this->propertyIds );
55+
56+
if ( $oldIndex === false ) {
57+
throw new OutOfBoundsException( 'There is no group for property id ' . $propertyId->getSerialization() );
58+
}
59+
60+
if ( $index === null ) {
61+
$index = count( $this->propertyIds );
62+
}
63+
64+
array_splice( $this->propertyIds, $oldIndex, 1 );
65+
array_splice( $this->propertyIds, $index, 0, array( $propertyId ) );
66+
}
67+
68+
/**
69+
* @param PropertyIdProvider $propertyIdProvider
70+
* @param int|null $index
71+
*
72+
* @throws InvalidArgumentException
73+
* @throws OutOfBoundsException
74+
*/
75+
public function moveElementToIndex( PropertyIdProvider $propertyIdProvider, $index ) {
76+
$this->assertIsIndex( $index );
77+
78+
$idSerialization = $propertyIdProvider->getPropertyId()->getSerialization();
79+
80+
if ( !isset( $this->byPropertyId[$idSerialization] ) ) {
81+
throw new OutOfBoundsException( 'There is no group for property id ' . $idSerialization );
82+
}
83+
84+
$oldIndex = array_search( $propertyIdProvider, $this->byPropertyId[$idSerialization] );
85+
86+
if ( $oldIndex === false ) {
87+
throw new OutOfBoundsException( 'The property id provider does not exist in this map' );
88+
}
89+
90+
if ( $index === null ) {
91+
$index = count( $this->byPropertyId[$idSerialization] );
92+
}
93+
94+
array_splice( $this->byPropertyId[$idSerialization], $oldIndex, 1 );
95+
array_splice( $this->byPropertyId[$idSerialization], $index, 0, array( $propertyIdProvider ) );
96+
}
97+
98+
/**
99+
* @param PropertyIdProvider $propertyIdProvider
100+
* @param int|null $index
101+
*
102+
* @throws InvalidArgumentException
103+
*/
104+
public function addElementAtIndex( PropertyIdProvider $propertyIdProvider, $index ) {
105+
$this->assertIsIndex( $index );
106+
107+
$idSerialization = $propertyIdProvider->getPropertyId()->getSerialization();
108+
109+
if ( !isset( $this->byPropertyId[$idSerialization] ) ) {
110+
$this->byPropertyId[$idSerialization] = array();
111+
$this->propertyIds[] = $propertyIdProvider->getPropertyId();
112+
}
113+
114+
if ( $index === null ) {
115+
$index = count( $this->byPropertyId[$idSerialization] );
116+
}
117+
118+
array_splice( $this->byPropertyId[$idSerialization], $index, 0, array( $propertyIdProvider ) );
119+
}
120+
121+
private function assertIsIndex( $index ) {
122+
if ( ( !is_int( $index ) || $index < 0 ) && $index !== null ) {
123+
throw new InvalidArgumentException( '$index must be a non-negative integer or null' );
124+
}
125+
}
126+
127+
/**
128+
* @return PropertyIdProvider[]
129+
*/
130+
public function getFlatArray() {
131+
$propertyIdProviders = array();
132+
133+
foreach ( $this->propertyIds as $propertyId ) {
134+
foreach ( $this->byPropertyId[$propertyId->getSerialization()] as $propertyIdProvider ) {
135+
$propertyIdProviders[] = $propertyIdProvider;
136+
}
137+
}
138+
139+
return $propertyIdProviders;
140+
}
141+
142+
}

0 commit comments

Comments
 (0)