1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Queryr\EntityStore; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\DBALException; |
7
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @licence GNU GPL v2+ |
11
|
|
|
* @author Jeroen De Dauw < [email protected] > |
12
|
|
|
*/ |
13
|
|
|
class PropertyTypeLookup { |
14
|
|
|
|
15
|
|
|
private $connection; |
16
|
|
|
private $tableName; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This constructor is package private. Construction is done via EntityStoreFactory. |
20
|
|
|
* |
21
|
|
|
* @param Connection $connection |
22
|
|
|
* @param string $tableName |
23
|
|
|
*/ |
24
|
4 |
|
public function __construct( Connection $connection, $tableName ) { |
25
|
4 |
|
$this->connection = $connection; |
26
|
4 |
|
$this->tableName = $tableName; |
27
|
4 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param PropertyId $id |
31
|
|
|
* |
32
|
|
|
* @return string|null |
33
|
|
|
* @throws PropertyTypeLookupException |
34
|
|
|
*/ |
35
|
4 |
|
public function getTypeOfProperty( PropertyId $id ) { |
36
|
4 |
|
$query = $this->buildQuery( $id ); |
37
|
|
|
|
38
|
|
|
try { |
39
|
4 |
|
$queryResult = $query->execute(); |
40
|
|
|
} |
41
|
1 |
|
catch ( DBALException $ex ) { |
42
|
1 |
|
throw new PropertyTypeLookupException( $ex->getMessage(), $ex ); |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
return $this->getTypeFromResult( $queryResult ); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
private function buildQuery( PropertyId $id ) { |
49
|
4 |
|
return $this->connection->createQueryBuilder() |
50
|
4 |
|
->select( 'property_type' ) |
51
|
4 |
|
->from( $this->tableName ) |
52
|
4 |
|
->where( 'property_id = ?' ) |
53
|
4 |
|
->setParameter( 0, (int)$id->getNumericId() ); |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
private function getTypeFromResult( \Traversable $rows ) { |
57
|
3 |
|
$rows = iterator_to_array( $rows ); |
58
|
|
|
|
59
|
3 |
|
if ( count( $rows ) < 1 ) { |
60
|
1 |
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
return reset( $rows )['property_type']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.