-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathStatementRank.php
More file actions
165 lines (143 loc) · 3.66 KB
/
StatementRank.php
File metadata and controls
165 lines (143 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace Wikibase\DataModel\Statement;
use InvalidArgumentException;
/**
* Everything we know about statement's ranks and what they are supposed to mean.
*
* @see https://www.mediawiki.org/wiki/Wikibase/DataModel#Ranks_of_Statements
* @see https://meta.wikimedia.org/wiki/Wikidata/Data_model_update#Ranks_and_order
*
* @since 4.4
*
* @license GNU GPL v2+
* @author Thiemo Kreuz
*/
class StatementRank {
/**
* Higher values are more preferred.
* TODO: Link to discussion/documentation that guarantees increasing order.
*/
const DEPRECATED = 0;
const NORMAL = 1;
const PREFERRED = 2;
private static $names = [
self::DEPRECATED => 'deprecated',
self::NORMAL => 'normal',
self::PREFERRED => 'preferred',
];
/**
* @return string[] Array mapping all known self::... constants (integers) to string names.
*/
public static function getNames() {
return self::$names;
}
/**
* @return int[] Array mapping string names to all known self::... constants (integers).
*/
public static function getAllRanks() {
return array_flip( self::$names );
}
/**
* @param int $rank
*
* @throws InvalidArgumentException
*/
public static function assertIsValid( $rank ) {
if ( !self::isValid( $rank ) ) {
throw new InvalidArgumentException( 'Invalid rank' );
}
}
/**
* @param int $rank
*
* @return bool
*/
public static function isValid( $rank ) {
return is_int( $rank ) && array_key_exists( $rank, self::$names );
}
/**
* @param int $rank
*
* @throws InvalidArgumentException
* @return bool Statements with a deprecated (or lower) rank are known to be false. But don't be
* fooled, this does not mean higher ranks are known to be true!
*/
public static function isFalse( $rank ) {
self::assertIsValid( $rank );
return $rank === self::DEPRECATED;
}
/**
* @param int|null $rank1
* @param int|null $rank2
*
* @throws InvalidArgumentException
* @return bool True if the given ranks are equal.
*/
public static function isEqual( $rank1, $rank2 ) {
return self::compare( $rank1, $rank2 ) === 0;
}
/**
* @param int|null $rank1
* @param int|null $rank2
*
* @throws InvalidArgumentException
* @return bool True if the first rank is less preferred than the second.
*/
public static function isLower( $rank1, $rank2 ) {
return self::compare( $rank1, $rank2 ) === -1;
}
/**
* @param int|null $rank1
* @param int|null $rank2
*
* @throws InvalidArgumentException
* @return bool True if the first rank is more preferred than the second.
*/
public static function isHigher( $rank1, $rank2 ) {
return self::compare( $rank1, $rank2 ) === 1;
}
/**
* @param int|null $rank1
* @param int|null $rank2
*
* @throws InvalidArgumentException
* @return int 0 if the ranks are equal, -1 if the first rank is less preferred than the second,
* or +1 if the first rank is more preferred than the second.
*/
public static function compare( $rank1, $rank2 ) {
if ( $rank1 !== null ) {
self::assertIsValid( $rank1 );
}
if ( $rank2 !== null ) {
self::assertIsValid( $rank2 );
}
if ( $rank1 === $rank2 ) {
return 0;
} elseif ( $rank1 === null || $rank1 < $rank2 ) {
return -1;
} else {
return 1;
}
}
/**
* @param int[]|int $ranks
* @param int [$rank2,...]
*
* @return int|null Best rank in the array or list of arguments, or null if none given.
*/
public static function findBestRank( $ranks = [] /*...*/ ) {
if ( !is_array( $ranks ) ) {
$ranks = func_get_args();
}
$best = null;
foreach ( $ranks as $rank ) {
if ( self::isHigher( $rank, $best ) ) {
$best = $rank;
if ( $best === self::PREFERRED ) {
break;
}
}
}
return $best;
}
}