1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\Lib\Units; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Basic unit storage functionality. |
7
|
|
|
* Concrete classes need to fill in data loading. |
8
|
|
|
* |
9
|
|
|
* @license GPL-2.0-or-later |
10
|
|
|
* @author Stas Malyshev |
11
|
|
|
*/ |
12
|
|
|
abstract class BaseUnitStorage implements UnitStorage { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array[] |
16
|
|
|
*/ |
17
|
|
|
private $storageData; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Load data from concrete storage. |
21
|
|
|
* The method should return array indexed by source unit. |
22
|
|
|
* Each row should be either [<factor>, <unit>] or |
23
|
|
|
* ['factor' => <factor>, 'unit' => <unit>] |
24
|
|
|
* @return array[]|null null when loading failed. |
25
|
|
|
*/ |
26
|
|
|
abstract protected function loadStorageData(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Load data from storage. |
30
|
|
|
*/ |
31
|
|
|
private function loadData() { |
32
|
|
|
if ( $this->storageData === null ) { |
33
|
|
|
$this->storageData = $this->loadStorageData(); |
|
|
|
|
34
|
|
|
if ( $this->storageData === null ) { |
35
|
|
|
throw new \RuntimeException( "Failed to load unit storage" ); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Check if certain unit is primary. |
42
|
|
|
* @param string $unit |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function isPrimaryUnit( $unit ) { |
46
|
|
|
if ( $this->storageData === null ) { |
47
|
|
|
$this->loadData(); |
48
|
|
|
} |
49
|
|
|
if ( !isset( $this->storageData[$unit] ) ) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
return ( $this->storageData[$unit]['unit'] ?? $this->storageData[$unit][1] ) === $unit; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get conversion from this unit to primary unit |
57
|
|
|
* @param string $unit |
58
|
|
|
* @return array|null 'factor' => factor from this unit to primary unit |
59
|
|
|
* 'unit' => primary unit |
60
|
|
|
*/ |
61
|
|
|
public function getConversion( $unit ) { |
62
|
|
|
if ( $this->storageData === null ) { |
63
|
|
|
$this->loadData(); |
64
|
|
|
} |
65
|
|
|
if ( !isset( $this->storageData[$unit] ) ) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
if ( isset( $this->storageData[$unit]['factor'] ) ) { |
69
|
|
|
return $this->storageData[$unit]; |
70
|
|
|
} else { |
71
|
|
|
return [ |
72
|
|
|
'factor' => $this->storageData[$unit][0], |
73
|
|
|
'unit' => $this->storageData[$unit][1] |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.