Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
ClientHintsBatchFormatterResults | |
100.00% |
14 / 14 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getStringForReferenceId | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | namespace MediaWiki\CheckUser\ClientHints; |
4 | |
5 | use InvalidArgumentException; |
6 | use MediaWiki\CheckUser\Services\UserAgentClientHintsManager; |
7 | |
8 | /** |
9 | * Value object for the result of UserAgentClientHintsFormatter::batchFormatClientHintsData |
10 | * which contains reference IDs to formatted strings of Client Hints data. |
11 | */ |
12 | class ClientHintsBatchFormatterResults { |
13 | /** @var int[][] */ |
14 | private array $referenceIdsToFormattedClientHintsIndex; |
15 | |
16 | /** @var string[] */ |
17 | private array $formattedClientHints; |
18 | |
19 | /** |
20 | * @param int[][] $referenceIdsToFormattedClientHintsIndex A map of reference type and reference ID values |
21 | * to integer keys in $formattedClientHints array. |
22 | * @param string[] $formattedClientHints An array of strings where the keys are integers that are the |
23 | * second-dimension value in the first parameter. |
24 | */ |
25 | public function __construct( array $referenceIdsToFormattedClientHintsIndex, array $formattedClientHints ) { |
26 | $this->referenceIdsToFormattedClientHintsIndex = $referenceIdsToFormattedClientHintsIndex; |
27 | $this->formattedClientHints = $formattedClientHints; |
28 | } |
29 | |
30 | /** |
31 | * Get the human-readable Client Hints data string for a given reference ID and reference type. |
32 | * |
33 | * @param int $referenceId The reference ID |
34 | * @param int $referenceType The reference type (one of the UserAgentClientHintsManager::IDENTIFIER_* integer |
35 | * constants). |
36 | * @return string|null |
37 | */ |
38 | public function getStringForReferenceId( int $referenceId, int $referenceType ): ?string { |
39 | // Validate that the $referenceType given is a valid reference type. If not, then |
40 | // return an exception to indicate a problem in the code. |
41 | if ( !array_key_exists( $referenceType, UserAgentClientHintsManager::IDENTIFIER_TO_TABLE_NAME_MAP ) ) { |
42 | throw new InvalidArgumentException( "Unrecognised reference type '$referenceType'" ); |
43 | } |
44 | // Check that the reference IDs to string index ID map has an |
45 | // entry for this reference ID and reference type. Otherwise, return null. |
46 | // If it does then also check that the index string array exists. Otherwise, return null. |
47 | if ( |
48 | !array_key_exists( $referenceType, $this->referenceIdsToFormattedClientHintsIndex ) || |
49 | !array_key_exists( $referenceId, $this->referenceIdsToFormattedClientHintsIndex[$referenceType] ) || |
50 | !array_key_exists( |
51 | $this->referenceIdsToFormattedClientHintsIndex[$referenceType][$referenceId], |
52 | $this->formattedClientHints |
53 | ) |
54 | ) { |
55 | return null; |
56 | } |
57 | // The reference ID matches a string in $this->formattedClientHints, so return it. |
58 | return $this->formattedClientHints[ |
59 | $this->referenceIdsToFormattedClientHintsIndex[$referenceType][$referenceId] |
60 | ]; |
61 | } |
62 | } |