|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\EntityStore\Tests\Fixtures; |
|
4
|
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\Entity; |
|
6
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
|
7
|
|
|
use Wikibase\Lib\Store\EntityLookup; |
|
8
|
|
|
use Wikibase\Lib\Store\StorageException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @licence GNU GPL v2+ |
|
12
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
13
|
|
|
*/ |
|
14
|
|
|
class InMemoryEntityLookup implements EntityLookup { |
|
15
|
|
|
|
|
16
|
|
|
private $entities; |
|
17
|
|
|
private $idsForWhichToThrowException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param Entity[] $entities |
|
21
|
|
|
* @param EntityId[] $idsForWhichToThrowException |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct( array $entities, array $idsForWhichToThrowException ) { |
|
24
|
|
|
foreach ( $entities as $entity ) { |
|
25
|
|
|
$this->entities[$entity->getId()->getSerialization()] = $entity; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$this->idsForWhichToThrowException = $idsForWhichToThrowException; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @see EntityLookup::getEntity |
|
33
|
|
|
* |
|
34
|
|
|
* @param EntityId $entityId |
|
35
|
|
|
* |
|
36
|
|
|
* @throws StorageException |
|
37
|
|
|
* @return Entity|null |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getEntity( EntityId $entityId ) { |
|
40
|
|
|
if ( in_array( $entityId, $this->idsForWhichToThrowException ) ) { |
|
41
|
|
|
throw new StorageException( 'The id is in idsForWhichToThrowException' ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ( array_key_exists( $entityId->getSerialization(), $this->entities ) ) { |
|
45
|
|
|
return $this->entities[$entityId->getSerialization()]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @see EntityLookup::getEntity |
|
53
|
|
|
* |
|
54
|
|
|
* @param EntityId $entityId |
|
55
|
|
|
* |
|
56
|
|
|
* @throws StorageException |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public function hasEntity( EntityId $entityId ) { |
|
60
|
|
|
throw new StorageException( 'No implemented' ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
} |