Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
36 / 36 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
MultiFormatSerializerUtils | |
100.00% |
36 / 36 |
|
100.00% |
8 / 8 |
22 | |
100.00% |
1 / 1 |
assertArrayKeyExistsInSerialization | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
assertArrayValueIsArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
arrayIsSequential | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
assertArrayIsSequential | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
arrayContainsOnlyStrings | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
5 | |||
assertContainsOnlyStrings | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
guessDataFormat | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
assertFormatSuitableForRedirect | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace ProofreadPage; |
4 | |
5 | use MWContentSerializationException; |
6 | |
7 | /** |
8 | * Serializer that supports Wikitext and JSON, which is something supported |
9 | * by both Index and Page content models. |
10 | */ |
11 | trait MultiFormatSerializerUtils { |
12 | |
13 | /** |
14 | * @param string $key |
15 | * @param array $serialization |
16 | * @throws MWContentSerializationException |
17 | */ |
18 | protected static function assertArrayKeyExistsInSerialization( $key, array $serialization ) { |
19 | if ( !array_key_exists( $key, $serialization ) ) { |
20 | throw new MWContentSerializationException( |
21 | "The serialization should contain a '$key' entry." |
22 | ); |
23 | } |
24 | } |
25 | |
26 | /** |
27 | * @param array $serialization |
28 | * @param string $key |
29 | * @throws MWContentSerializationException |
30 | */ |
31 | protected static function assertArrayValueIsArray( array $serialization, $key ) { |
32 | if ( !is_array( $serialization[ $key ] ) ) { |
33 | throw new MWContentSerializationException( |
34 | "The serialization key '$key' should be an array." |
35 | ); |
36 | } |
37 | } |
38 | |
39 | /** |
40 | * Check if an array has only sequential integer keys |
41 | * @param array $array the array to check |
42 | * @return bool |
43 | */ |
44 | protected static function arrayIsSequential( array $array ): bool { |
45 | if ( $array === [] ) { |
46 | return true; |
47 | } |
48 | |
49 | // The array isn't empty, but has no '0' key, so it cannot be sequential |
50 | if ( !isset( $array[0] ) ) { |
51 | return false; |
52 | } |
53 | |
54 | return array_keys( $array ) === range( 0, count( $array ) - 1 ); |
55 | } |
56 | |
57 | /** |
58 | * @param array $array the array to check |
59 | * @param string $name name to use in the error |
60 | * @throws MWContentSerializationException |
61 | */ |
62 | protected static function assertArrayIsSequential( array $array, string $name ) { |
63 | if ( !self::arrayIsSequential( $array ) ) { |
64 | throw new MWContentSerializationException( |
65 | "The array '$name' should be a sequential array." |
66 | ); |
67 | } |
68 | } |
69 | |
70 | /** |
71 | * Check if an array has only string values |
72 | * @param array $array the array to check |
73 | * @param bool $emptyAllowed true if the array may contain empty strings |
74 | * @return bool |
75 | */ |
76 | protected static function arrayContainsOnlyStrings( array $array, bool $emptyAllowed ): bool { |
77 | foreach ( $array as $key => $value ) { |
78 | if ( !is_string( $value ) || ( !$emptyAllowed && $value === '' ) ) { |
79 | return false; |
80 | } |
81 | } |
82 | return true; |
83 | } |
84 | |
85 | /** |
86 | * @param array $array the array to check |
87 | * @param bool $emptyAllowed true if the array may contain empty strings |
88 | * @param string $name name to use in the error |
89 | * @throws MWContentSerializationException |
90 | */ |
91 | protected static function assertContainsOnlyStrings( array $array, bool $emptyAllowed, string $name ) { |
92 | if ( !self::arrayContainsOnlyStrings( $array, $emptyAllowed ) ) { |
93 | $strType = $emptyAllowed ? '' : 'non-empty '; |
94 | $msg = "The array '$name' should contain only {$strType}strings."; |
95 | throw new MWContentSerializationException( $msg ); |
96 | } |
97 | } |
98 | |
99 | /** |
100 | * Detect if a string contains valid json or not |
101 | * |
102 | * @param string $text the string to detect the format of |
103 | * @param bool $expectJsonArray true if the JSON data should look like an array |
104 | * @return string |
105 | */ |
106 | protected static function guessDataFormat( string $text, bool $expectJsonArray ): string { |
107 | $jsonData = json_decode( $text, true ); |
108 | |
109 | $suitableJson = $expectJsonArray ? |
110 | is_array( $jsonData ) : |
111 | $jsonData !== null; |
112 | |
113 | return $suitableJson |
114 | ? CONTENT_FORMAT_JSON |
115 | : CONTENT_FORMAT_WIKITEXT; |
116 | } |
117 | |
118 | /** |
119 | * Throw an exception is a redirect is being serialised in a format that |
120 | * doesn't support it. |
121 | * @param string $format the desired format |
122 | * @throws MWContentSerializationException |
123 | */ |
124 | protected static function assertFormatSuitableForRedirect( string $format ) { |
125 | if ( $format !== CONTENT_FORMAT_WIKITEXT ) { |
126 | throw new MWContentSerializationException( |
127 | "Redirects cannot be serialised as $format" |
128 | ); |
129 | } |
130 | } |
131 | } |