Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
DependencyMetadata | |
100.00% |
23 / 23 |
|
100.00% |
7 / 7 |
11 | |
100.00% |
1 / 1 |
blank | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
ofEntityId | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
ofFutureTime | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
merge | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
minTimeValue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getEntityIds | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFutureTime | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Cache; |
4 | |
5 | use DataValues\TimeValue; |
6 | use Wikibase\DataModel\Entity\EntityId; |
7 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\TimeValueComparer; |
8 | use Wikimedia\Assert\Assert; |
9 | |
10 | /** |
11 | * Information about what other things a value depends on. |
12 | * |
13 | * @author Lucas Werkmeister |
14 | * @license GPL-2.0-or-later |
15 | */ |
16 | class DependencyMetadata { |
17 | |
18 | /** |
19 | * @var EntityId[] |
20 | */ |
21 | private $entityIds = []; |
22 | |
23 | /** |
24 | * @var TimeValue|null |
25 | */ |
26 | private $timeValue = null; |
27 | |
28 | /** |
29 | * @return self Indication that a value does not depend on anything else. |
30 | */ |
31 | public static function blank() { |
32 | return new self; |
33 | } |
34 | |
35 | /** |
36 | * Track that a value depends on a certain entity ID. |
37 | * @param EntityId $entityId An entity ID from which the value was derived. |
38 | * @return self Indication that a value was derived from the entity with the given ID. |
39 | */ |
40 | public static function ofEntityId( EntityId $entityId ) { |
41 | $ret = new self; |
42 | $ret->entityIds[] = $entityId; |
43 | return $ret; |
44 | } |
45 | |
46 | /** |
47 | * Track that a value depends on a certain time value being a future date, not a past one. |
48 | * @param TimeValue $timeValue A point in time on which the value might start to become invalid. |
49 | * @return self Indication that a value will only remain valid |
50 | * as long as the given time value is in the future, not in the past. |
51 | */ |
52 | public static function ofFutureTime( TimeValue $timeValue ) { |
53 | $ret = new self; |
54 | $ret->timeValue = $timeValue; |
55 | return $ret; |
56 | } |
57 | |
58 | /** |
59 | * @param self[] $metadatas |
60 | * @return self |
61 | */ |
62 | public static function merge( array $metadatas ) { |
63 | Assert::parameterElementType( self::class, $metadatas, '$metadatas' ); |
64 | $ret = new self; |
65 | $entityIds = []; |
66 | foreach ( $metadatas as $metadata ) { |
67 | foreach ( $metadata->entityIds as $entityId ) { |
68 | $entityIds[$entityId->getSerialization()] = $entityId; |
69 | } |
70 | $ret->timeValue = self::minTimeValue( $ret->timeValue, $metadata->timeValue ); |
71 | } |
72 | $ret->entityIds = array_values( $entityIds ); |
73 | return $ret; |
74 | } |
75 | |
76 | /** |
77 | * @param TimeValue|null $t1 |
78 | * @param TimeValue|null $t2 |
79 | * @return TimeValue|null |
80 | */ |
81 | private static function minTimeValue( ?TimeValue $t1, ?TimeValue $t2 ) { |
82 | if ( $t1 === null ) { |
83 | return $t2; |
84 | } |
85 | if ( $t2 === null ) { |
86 | return $t1; |
87 | } |
88 | return ( new TimeValueComparer() )->getMinimum( $t1, $t2 ); |
89 | } |
90 | |
91 | /** |
92 | * @return EntityId[] Entity IDs from whose entities the value was derived. |
93 | * Changes to those entity IDs should invalidate the value. |
94 | */ |
95 | public function getEntityIds() { |
96 | return $this->entityIds; |
97 | } |
98 | |
99 | /** |
100 | * @return TimeValue|null A point in time which was in the future when the value was first determined. |
101 | * The value should be invalidated once this point in time is no longer in the future. |
102 | */ |
103 | public function getFutureTime() { |
104 | return $this->timeValue; |
105 | } |
106 | |
107 | } |