Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
76.92% |
30 / 39 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ImageRecommendationDataValidator | |
76.92% |
30 / 39 |
|
50.00% |
1 / 2 |
13.77 | |
0.00% |
0 / 1 |
isValidTitle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
validate | |
76.32% |
29 / 38 |
|
0.00% |
0 / 1 |
12.61 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\NewcomerTasks\AddImage; |
4 | |
5 | use File; |
6 | use MediaWiki\Language\RawMessage; |
7 | use StatusValue; |
8 | |
9 | /** |
10 | * Validate image recommendation data |
11 | */ |
12 | class ImageRecommendationDataValidator { |
13 | |
14 | /** |
15 | * Check whether the specified title is valid |
16 | * |
17 | * @param string $filename |
18 | * @return bool |
19 | */ |
20 | private static function isValidTitle( string $filename ): bool { |
21 | return (bool)File::normalizeTitle( $filename ); |
22 | } |
23 | |
24 | /** |
25 | * Return StatusValue with validation errors in the recommendation data |
26 | * |
27 | * @param string $titleTextSafe |
28 | * @param ImageRecommendationData $imageRecommendationData |
29 | * |
30 | * @return StatusValue |
31 | */ |
32 | public static function validate( |
33 | string $titleTextSafe, ImageRecommendationData $imageRecommendationData |
34 | ): StatusValue { |
35 | $filename = $imageRecommendationData->getFilename(); |
36 | if ( !is_string( $filename ) || !self::isValidTitle( $filename ) ) { |
37 | $invalidFilename = is_string( $filename ) ? |
38 | strip_tags( $filename ) : |
39 | '[type] ' . gettype( $filename ); |
40 | return StatusValue::newFatal( new RawMessage( |
41 | 'Invalid filename format for $1: $2', |
42 | [ $titleTextSafe, $invalidFilename ] |
43 | ) ); |
44 | } |
45 | |
46 | $source = $imageRecommendationData->getSource(); |
47 | if ( !in_array( $source, ImageRecommendationImage::KNOWN_SOURCES, true ) ) { |
48 | return StatusValue::newFatal( new RawMessage( |
49 | 'Invalid source type for $1: $2', |
50 | [ $titleTextSafe, strip_tags( $source ) ] |
51 | ) ); |
52 | } |
53 | |
54 | if ( !is_string( $imageRecommendationData->getProjects() ) ) { |
55 | return StatusValue::newFatal( new RawMessage( |
56 | 'Invalid projects format for $1', |
57 | [ $titleTextSafe ] |
58 | ) ); |
59 | } |
60 | |
61 | if ( !is_string( $imageRecommendationData->getDatasetId() ) ) { |
62 | return StatusValue::newFatal( new RawMessage( |
63 | 'Invalid datasetId format for $1', |
64 | [ $titleTextSafe ] |
65 | ) ); |
66 | } |
67 | |
68 | if ( !is_int( $imageRecommendationData->getSectionNumber() ) |
69 | && $imageRecommendationData->getSectionNumber() !== null |
70 | ) { |
71 | return StatusValue::newFatal( new RawMessage( |
72 | 'Invalid section number for $1', |
73 | [ $titleTextSafe ] |
74 | ) ); |
75 | } |
76 | |
77 | if ( !is_string( $imageRecommendationData->getSectionTitle() ) |
78 | && $imageRecommendationData->getSectionTitle() !== null |
79 | ) { |
80 | return StatusValue::newFatal( new RawMessage( |
81 | 'Invalid section title for $1', |
82 | [ $titleTextSafe ] |
83 | ) ); |
84 | } |
85 | |
86 | return StatusValue::newGood(); |
87 | } |
88 | } |