1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Queryr\TermStore; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\DBALException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Package private. |
9
|
|
|
* |
10
|
|
|
* @licence GNU GPL v2+ |
11
|
|
|
* @author Jeroen De Dauw < [email protected] > |
12
|
|
|
*/ |
13
|
|
|
class IdLookup implements EntityIdLookup { |
14
|
|
|
|
15
|
|
|
private $labelTable; |
16
|
|
|
private $aliasesTable; |
17
|
|
|
|
18
|
16 |
|
public function __construct( TableQueryExecutor $labelTable, TableQueryExecutor $aliasesTable ) { |
19
|
16 |
|
$this->labelTable = $labelTable; |
20
|
16 |
|
$this->aliasesTable = $aliasesTable; |
21
|
16 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Returns the first matching entity id. Case insensitive. |
25
|
|
|
* |
26
|
|
|
* @throws TermStoreException |
27
|
|
|
*/ |
28
|
2 |
|
public function getIdByLabel( string $labelLanguageCode, string $labelText ): ?string { |
29
|
2 |
|
return $this->getEntityIdByLabel( $labelLanguageCode, $labelText ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns the first matching item id. Case insensitive. |
34
|
|
|
* |
35
|
|
|
* @throws TermStoreException |
36
|
|
|
*/ |
37
|
2 |
|
public function getItemIdByLabel( string $labelLanguageCode, string $labelText ): ?string { |
38
|
2 |
|
return $this->getEntityIdByLabel( $labelLanguageCode, $labelText, 'item' ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns the first matching property id. Case insensitive. |
43
|
|
|
* |
44
|
|
|
* @throws TermStoreException |
45
|
|
|
*/ |
46
|
2 |
|
public function getPropertyIdByLabel( string $labelLanguageCode, string $labelText ): ?string { |
47
|
2 |
|
return $this->getEntityIdByLabel( $labelLanguageCode, $labelText, 'property' ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $labelLanguageCode |
52
|
|
|
* @param string $labelText |
53
|
|
|
* @param string|null $entityTypeFilter |
54
|
|
|
* |
55
|
|
|
* @return string|null |
56
|
|
|
* @throws TermStoreException |
57
|
|
|
*/ |
58
|
12 |
|
private function getEntityIdByLabel( $labelLanguageCode, $labelText, $entityTypeFilter = null ) { |
59
|
|
|
$conditions = [ |
60
|
12 |
|
'text_lowercase' => strtolower( $labelText ), |
61
|
12 |
|
'language' => $labelLanguageCode, |
62
|
|
|
]; |
63
|
|
|
|
64
|
12 |
|
if ( $entityTypeFilter !== null ) { |
65
|
8 |
|
$conditions['entity_type'] = $entityTypeFilter; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
try { |
69
|
12 |
|
return $this->labelTable->selectOneField( |
70
|
12 |
|
'entity_id', |
71
|
12 |
|
$conditions |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
catch ( DBALException $ex ) { |
75
|
|
|
throw new TermStoreException( $ex->getMessage(), $ex ); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the first matching entity id. Case insensitive. |
81
|
|
|
* |
82
|
|
|
* @throws TermStoreException |
83
|
|
|
*/ |
84
|
2 |
|
public function getIdByText( string $languageCode, string $termText ): ?string { |
85
|
2 |
|
return $this->getEntityIdByText( $languageCode, $termText ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the first matching item id. Case insensitive. |
90
|
|
|
* |
91
|
|
|
* @throws TermStoreException |
92
|
|
|
*/ |
93
|
2 |
|
public function getItemIdByText( string $languageCode, string $termText ): ?string { |
94
|
2 |
|
return $this->getEntityIdByText( $languageCode, $termText, 'item' ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the first matching property id. Case insensitive. |
99
|
|
|
* |
100
|
|
|
* @throws TermStoreException |
101
|
|
|
*/ |
102
|
2 |
|
public function getPropertyIdByText( string $languageCode, string $termText ): ?string { |
103
|
2 |
|
return $this->getEntityIdByText( $languageCode, $termText, 'property' ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $languageCode |
108
|
|
|
* @param string $termText |
109
|
|
|
* @param string|null $entityTypeFilter |
110
|
|
|
* |
111
|
|
|
* @return string|null |
112
|
|
|
* @throws TermStoreException |
113
|
|
|
*/ |
114
|
6 |
|
private function getEntityIdByText( string $languageCode, string $termText, $entityTypeFilter = null ) { |
115
|
6 |
|
$labelMatch = $this->getEntityIdByLabel( $languageCode, $termText, $entityTypeFilter ); |
116
|
|
|
|
117
|
6 |
|
if ( $labelMatch !== null ) { |
118
|
3 |
|
return $labelMatch; |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
return $this->getIdByAlias( $languageCode, $termText, $entityTypeFilter ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $aliasLanguageCode |
126
|
|
|
* @param string $aliasText |
127
|
|
|
* @param string|null $entityTypeFilter |
128
|
|
|
* |
129
|
|
|
* @return string|null |
130
|
|
|
* @throws TermStoreException |
131
|
|
|
*/ |
132
|
3 |
|
private function getIdByAlias( string $aliasLanguageCode, string $aliasText, $entityTypeFilter = null ) { |
133
|
|
|
$conditions = [ |
134
|
3 |
|
'text_lowercase' => strtolower( $aliasText ), |
135
|
3 |
|
'language' => $aliasLanguageCode |
136
|
|
|
]; |
137
|
|
|
|
138
|
3 |
|
if ( $entityTypeFilter !== null ) { |
139
|
2 |
|
$conditions['entity_type'] = $entityTypeFilter; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
try { |
143
|
3 |
|
return $this->aliasesTable->selectOneField( |
144
|
3 |
|
'entity_id', |
145
|
3 |
|
$conditions |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
catch ( DBALException $ex ) { |
149
|
|
|
throw new TermStoreException( $ex->getMessage(), $ex ); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |