|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataAccess\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Wikibase\DataAccess\PrefetchingTermLookup; |
|
6
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
|
7
|
|
|
use Wikibase\DataModel\Term\TermTypes; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A PrefetchingTermLookup providing dummy TermLookup functionality, i.e. always returning a fake label/description, |
|
11
|
|
|
* and optional aliases, and also a Spy on TermBuffer |
|
12
|
|
|
* i.e. provides access to "prefetched" terms stored in the buffer after prefetchTerms method is called. |
|
13
|
|
|
* |
|
14
|
|
|
* @license GPL-2.0-or-later |
|
15
|
|
|
*/ |
|
16
|
|
|
class FakePrefetchingTermLookup implements PrefetchingTermLookup { |
|
17
|
|
|
|
|
18
|
|
|
private $buffer; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param array $entityIds |
|
22
|
|
|
* @param array|null $termTypes if null, defaults to labels and descriptions only |
|
23
|
|
|
* @param array|null $languageCodes if null, defaults to de and en |
|
24
|
|
|
*/ |
|
25
|
|
|
public function prefetchTerms( array $entityIds, array $termTypes, array $languageCodes ) { |
|
26
|
|
|
if ( $termTypes === null ) { |
|
27
|
|
|
$termTypes = [ TermTypes::TYPE_LABEL, TermTypes::TYPE_DESCRIPTION ]; |
|
28
|
|
|
} |
|
29
|
|
|
if ( $languageCodes === null ) { |
|
30
|
|
|
$languageCodes = [ 'de', 'en' ]; |
|
31
|
|
|
} |
|
32
|
|
|
$this->bufferFakeTermsForEntities( $entityIds, $termTypes, $languageCodes ); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function bufferFakeTermsForEntities( array $entityIds, array $termTypes, array $languageCodes ) { |
|
36
|
|
|
foreach ( $entityIds as $id ) { |
|
37
|
|
|
foreach ( $termTypes as $type ) { |
|
38
|
|
|
foreach ( $languageCodes as $lang ) { |
|
39
|
|
|
if ( $type !== TermTypes::TYPE_ALIAS ) { |
|
40
|
|
|
$this->bufferNonAliasTerm( $id, $type, $lang ); |
|
41
|
|
|
} else { |
|
42
|
|
|
$this->bufferAliasTerms( $id, $type, $lang ); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function bufferNonAliasTerm( EntityId $id, $type, $lang ) { |
|
50
|
|
|
$this->buffer[$id->getSerialization()][$type][$lang] = $this->generateFakeTerm( $id, $type, $lang ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function bufferAliasTerms( EntityId $id, $type, $lang ) { |
|
54
|
|
|
$this->buffer[$id->getSerialization()][$type][$lang] = []; |
|
55
|
|
|
$this->buffer[$id->getSerialization()][$type][$lang][] = $this->generateFakeTerm( $id, $type, $lang, 1 ); |
|
56
|
|
|
$this->buffer[$id->getSerialization()][$type][$lang][] = $this->generateFakeTerm( $id, $type, $lang, 2 ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param EntityId $id |
|
61
|
|
|
* @param string $type |
|
62
|
|
|
* @param string $lang |
|
63
|
|
|
* @param int $count Used for aliases |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
private function generateFakeTerm( EntityId $id, $type, $lang, $count = 0 ) { |
|
67
|
|
|
$suffix = $count ? ' ' . $count : ''; |
|
68
|
|
|
|
|
69
|
|
|
return $id->getSerialization() . ' ' . $lang . ' ' . $type . $suffix; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getPrefetchedTerms() { |
|
73
|
|
|
$terms = []; |
|
74
|
|
|
|
|
75
|
|
|
foreach ( $this->buffer as $entityTerms ) { |
|
76
|
|
|
foreach ( $entityTerms as $termsByLang ) { |
|
77
|
|
|
foreach ( $termsByLang as $term ) { |
|
78
|
|
|
$terms[] = $term; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $terms; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getPrefetchedTerm( EntityId $entityId, $termType, $languageCode ) { |
|
87
|
|
|
$id = $entityId->getSerialization(); |
|
88
|
|
|
return $this->buffer[$id][$termType][$languageCode] ?? null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getLabel( EntityId $entityId, $languageCode ) { |
|
92
|
|
|
return $this->generateFakeTerm( $entityId, TermTypes::TYPE_LABEL, $languageCode ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getLabels( EntityId $entityId, array $languageCodes ) { |
|
96
|
|
|
$labels = []; |
|
97
|
|
|
|
|
98
|
|
|
foreach ( $languageCodes as $lang ) { |
|
99
|
|
|
$labels[$lang] = $this->generateFakeTerm( $entityId, TermTypes::TYPE_LABEL, $lang ); |
|
100
|
|
|
} |
|
101
|
|
|
return $labels; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getDescription( EntityId $entityId, $languageCode ) { |
|
105
|
|
|
return $this->generateFakeTerm( $entityId, TermTypes::TYPE_DESCRIPTION, $languageCode ); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getDescriptions( EntityId $entityId, array $languageCodes ) { |
|
109
|
|
|
$descriptions = []; |
|
110
|
|
|
|
|
111
|
|
|
foreach ( $languageCodes as $lang ) { |
|
112
|
|
|
$descriptions[$lang] = $this->generateFakeTerm( $entityId, TermTypes::TYPE_DESCRIPTION, $lang ); |
|
113
|
|
|
} |
|
114
|
|
|
return $descriptions; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function getPrefetchedAliases( EntityId $entityId, $languageCode ) { |
|
118
|
|
|
$id = $entityId->getSerialization(); |
|
119
|
|
|
if ( array_key_exists( $id, $this->buffer ) ) { |
|
120
|
|
|
if ( array_key_exists( TermTypes::TYPE_ALIAS, $this->buffer[$id] ) ) { |
|
121
|
|
|
if ( array_key_exists( $languageCode, $this->buffer[$id][TermTypes::TYPE_ALIAS] ) ) { |
|
122
|
|
|
return $this->buffer[$id][TermTypes::TYPE_ALIAS][$languageCode]; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return []; |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.