Skip to content

Commit 1652156

Browse files
committed
Add a Rank class
1 parent d8dbe92 commit 1652156

2 files changed

Lines changed: 96 additions & 3 deletions

File tree

src/Statement/Rank.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Wikibase\DataModel\Statement;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* @see https://www.mediawiki.org/wiki/Wikibase/DataModel#Ranks_of_Statements
9+
* @see https://meta.wikimedia.org/wiki/Wikidata/Data_model_update#Ranks_and_order
10+
*
11+
* @since 4.4
12+
*
13+
* @licence GNU GPL v2+
14+
* @author Thiemo Mättig
15+
*/
16+
class Rank {
17+
18+
/**
19+
* Higher values are more preferred.
20+
* TODO: Link to discussion/documentation that guarantees increasing order.
21+
*/
22+
const DEPRECATED = 0;
23+
const NORMAL = 1;
24+
const PREFERRED = 2;
25+
26+
/**
27+
* TODO: Add doc.
28+
*/
29+
const RANK_0 = self::DEPRECATED;
30+
const RANK_1 = self::NORMAL;
31+
const RANK_2 = self::PREFERRED;
32+
33+
/**
34+
* @param int $rank
35+
*
36+
* @throws InvalidArgumentException
37+
* @return bool
38+
*/
39+
public static function isValid( $rank ) {
40+
if ( $rank === self::DEPRECATED
41+
|| $rank === self::NORMAL
42+
|| $rank === self::PREFERRED
43+
) {
44+
return true;
45+
}
46+
47+
throw new InvalidArgumentException( 'Unknown rank' );
48+
}
49+
50+
/**
51+
* @param int $rank1
52+
* @param int $rank2
53+
*
54+
* @return bool True if the given ranks are equal.
55+
*/
56+
public static function equals( $rank1, $rank2 ) {
57+
return self::compare( $rank1, $rank2 ) === 0;
58+
}
59+
60+
/**
61+
* @param int $rank1
62+
* @param int $rank2
63+
*
64+
* @return bool True if the first rank is less preferred than the second.
65+
*/
66+
public static function isLower( $rank1, $rank2 ) {
67+
return self::compare( $rank1, $rank2 ) === -1;
68+
}
69+
70+
/**
71+
* @param int $rank1
72+
* @param int $rank2
73+
*
74+
* @return bool True if the first rank is more preferred than the second.
75+
*/
76+
public static function isHigher( $rank1, $rank2 ) {
77+
return self::compare( $rank1, $rank2 ) === 1;
78+
}
79+
80+
/**
81+
* @param int $rank1
82+
* @param int $rank2
83+
*
84+
* @return int 0 if the ranks are equal, -1 if the first or +1 if the second is more preferred.
85+
*/
86+
public static function compare( $rank1, $rank2 ) {
87+
self::isValid( $rank1 );
88+
self::isValid( $rank2 );
89+
90+
return $rank1 - $rank2;
91+
}
92+
93+
}

src/Statement/Statement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class Statement implements Hashable, Comparable, PropertyIdProvider {
2929
*
3030
* @since 2.0
3131
*/
32-
const RANK_PREFERRED = 2;
33-
const RANK_NORMAL = 1;
34-
const RANK_DEPRECATED = 0;
32+
const RANK_PREFERRED = Rank::PREFERRED;
33+
const RANK_NORMAL = Rank::NORMAL;
34+
const RANK_DEPRECATED = Rank::DEPRECATED;
3535

3636
/**
3737
* @var string|null

0 commit comments

Comments
 (0)