Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
50 / 50 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
CheckResultSerializer | |
100.00% |
50 / 50 |
|
100.00% |
4 / 4 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
serialize | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
5 | |||
serializeCachingMetadata | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
serializeDependencyMetadata | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Result; |
6 | |
7 | use Wikibase\DataModel\Entity\EntityId; |
8 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata; |
9 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\ContextCursorSerializer; |
10 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessageSerializer; |
11 | use WikibaseQuality\ConstraintReport\ConstraintSerializer; |
12 | |
13 | /** |
14 | * A serializer for {@link CheckResult}s. |
15 | * Caching metadata for {@link NullResult}s is never serialized, |
16 | * since it doesn’t make sense for those results to be cached |
17 | * (though they can carry dependency metadata, which is serialized correctly). |
18 | * |
19 | * @author Lucas Werkmeister |
20 | * @license GPL-2.0-or-later |
21 | */ |
22 | class CheckResultSerializer { |
23 | |
24 | public const KEY_CONTEXT_CURSOR = '|'; |
25 | public const KEY_CONSTRAINT = 'c'; |
26 | public const KEY_CHECK_RESULT_STATUS = 's'; |
27 | public const KEY_VIOLATION_MESSAGE = 'm'; |
28 | public const KEY_CONSTRAINT_CLARIFICATION = 'l'; |
29 | public const KEY_CACHING_METADATA = 'CM'; |
30 | public const KEY_DEPENDENCY_METADATA = 'DM'; |
31 | public const KEY_NULL_RESULT = '0'; |
32 | |
33 | public const KEY_CACHING_METADATA_MAX_AGE = 'a'; |
34 | |
35 | public const KEY_DEPENDENCY_METADATA_ENTITY_IDS = 'e'; |
36 | public const KEY_DEPENDENCY_METADATA_FUTURE_TIME = 'f'; |
37 | |
38 | private ConstraintSerializer $constraintSerializer; |
39 | private ContextCursorSerializer $contextCursorSerializer; |
40 | private ViolationMessageSerializer $violationMessageSerializer; |
41 | private bool $serializeDependencyMetadata; |
42 | |
43 | /** |
44 | * @param ConstraintSerializer $constraintSerializer |
45 | * @param ContextCursorSerializer $contextCursorSerializer |
46 | * @param ViolationMessageSerializer $violationMessageSerializer |
47 | * @param bool $serializeDependencyMetadata Whether to serialize the DependencyMetadata component |
48 | * of a result’s {@link CheckResult::getMetadata metadata} or not. |
49 | */ |
50 | public function __construct( |
51 | ConstraintSerializer $constraintSerializer, |
52 | ContextCursorSerializer $contextCursorSerializer, |
53 | ViolationMessageSerializer $violationMessageSerializer, |
54 | bool $serializeDependencyMetadata = true |
55 | ) { |
56 | $this->constraintSerializer = $constraintSerializer; |
57 | $this->contextCursorSerializer = $contextCursorSerializer; |
58 | $this->violationMessageSerializer = $violationMessageSerializer; |
59 | $this->serializeDependencyMetadata = $serializeDependencyMetadata; |
60 | } |
61 | |
62 | public function serialize( CheckResult $checkResult ): array { |
63 | $contextCursor = $checkResult->getContextCursor(); |
64 | |
65 | $serialization = [ |
66 | self::KEY_CONTEXT_CURSOR => $this->contextCursorSerializer->serialize( $contextCursor ), |
67 | ]; |
68 | |
69 | if ( $checkResult instanceof NullResult ) { |
70 | $serialization[self::KEY_NULL_RESULT] = 1; |
71 | } else { |
72 | $constraint = $checkResult->getConstraint(); |
73 | $cachingMetadata = $checkResult->getMetadata()->getCachingMetadata(); |
74 | $violationMessage = $checkResult->getMessage(); |
75 | $clarification = $checkResult->getConstraintClarification(); |
76 | |
77 | $serialization[self::KEY_CONSTRAINT] = |
78 | $this->constraintSerializer->serialize( $constraint ); |
79 | $serialization[self::KEY_CHECK_RESULT_STATUS] = |
80 | $checkResult->getStatus(); |
81 | $serialization[self::KEY_CACHING_METADATA] = |
82 | $this->serializeCachingMetadata( $cachingMetadata ); |
83 | |
84 | if ( $violationMessage !== null ) { |
85 | $serialization[self::KEY_VIOLATION_MESSAGE] = |
86 | $this->violationMessageSerializer->serialize( $violationMessage ); |
87 | } |
88 | if ( $clarification->getTexts() !== [] ) { |
89 | $serialization[self::KEY_CONSTRAINT_CLARIFICATION] = |
90 | $clarification->getArrayValue(); |
91 | } |
92 | } |
93 | |
94 | if ( $this->serializeDependencyMetadata ) { |
95 | $serialization[self::KEY_DEPENDENCY_METADATA] = |
96 | $this->serializeDependencyMetadata( $checkResult ); |
97 | } |
98 | |
99 | return $serialization; |
100 | } |
101 | |
102 | private function serializeCachingMetadata( CachingMetadata $cachingMetadata ): array { |
103 | $maximumAge = $cachingMetadata->getMaximumAgeInSeconds(); |
104 | |
105 | $serialization = []; |
106 | |
107 | if ( $maximumAge > 0 ) { |
108 | $serialization[self::KEY_CACHING_METADATA_MAX_AGE] = $maximumAge; |
109 | } |
110 | |
111 | return $serialization; |
112 | } |
113 | |
114 | private function serializeDependencyMetadata( CheckResult $checkResult ): array { |
115 | $dependencyMetadata = $checkResult->getMetadata()->getDependencyMetadata(); |
116 | $entityIds = $dependencyMetadata->getEntityIds(); |
117 | $futureTime = $dependencyMetadata->getFutureTime(); |
118 | |
119 | $serialization = [ |
120 | self::KEY_DEPENDENCY_METADATA_ENTITY_IDS => array_map( |
121 | static function ( EntityId $entityId ) { |
122 | return $entityId->getSerialization(); |
123 | }, |
124 | $entityIds |
125 | ), |
126 | ]; |
127 | |
128 | if ( $futureTime !== null ) { |
129 | $serialization[self::KEY_DEPENDENCY_METADATA_FUTURE_TIME] = |
130 | $futureTime->getArrayValue(); |
131 | } |
132 | |
133 | return $serialization; |
134 | } |
135 | |
136 | } |