Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 19 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
NewsletterValidator | |
0.00% |
0 / 19 |
|
0.00% |
0 / 2 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
validate | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
132 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Newsletter; |
4 | |
5 | use MediaWiki\Status\Status; |
6 | use MediaWiki\Title\Title; |
7 | |
8 | /** |
9 | * Handles validation for newsletters |
10 | */ |
11 | class NewsletterValidator { |
12 | |
13 | /** @var array */ |
14 | private $data; |
15 | |
16 | /** @var string[] */ |
17 | private static $requiredDataOnCreate = [ |
18 | 'Name', |
19 | 'Description', |
20 | 'MainPage', |
21 | ]; |
22 | |
23 | /** @var string[] */ |
24 | private static $requiredDataOnEdit = [ |
25 | 'Description', |
26 | 'MainPage', |
27 | ]; |
28 | |
29 | /** |
30 | * @param array $fields |
31 | */ |
32 | public function __construct( array $fields ) { |
33 | $this->data = $fields; |
34 | } |
35 | |
36 | /** |
37 | * Check whether all input have proper values |
38 | * |
39 | * @param bool $new |
40 | * @return Status fatal if invalid, good otherwise |
41 | */ |
42 | public function validate( $new ) { |
43 | $requiredFields = $new ? self::$requiredDataOnCreate : self::$requiredDataOnEdit; |
44 | // Check whether required fields are not empty |
45 | foreach ( $requiredFields as $field ) { |
46 | if ( !isset( $this->data[ $field ] ) || trim( $this->data[ $field ] ) === '' ) { |
47 | return Status::newFatal( 'newsletter-input-required' ); |
48 | } |
49 | } |
50 | |
51 | if ( $new ) { |
52 | // Prevents random nonsensical characters in newsletter names |
53 | // and also adds a length limit |
54 | // (uses Title's rules now - maybe use our own?) |
55 | $name = Title::makeTitleSafe( NS_MAIN, $this->data['Name'] ); |
56 | if ( !$name ) { |
57 | return Status::newFatal( 'newsletter-invalid-name' ); |
58 | } |
59 | } |
60 | if ( strlen( $this->data['Description'] ) < 30 ) { |
61 | // Should this limit be lowered? |
62 | return Status::newFatal( 'newsletter-create-short-description-error' ); |
63 | } |
64 | |
65 | $mainTitle = $this->data['MainPage']; |
66 | if ( !$mainTitle ) { |
67 | return Status::newFatal( 'newsletter-create-mainpage-error' ); |
68 | } |
69 | |
70 | if ( !$mainTitle->canExist() ) { |
71 | return Status::newFatal( 'newsletter-create-mainpage-error' ); |
72 | } |
73 | |
74 | if ( !$mainTitle->exists() ) { |
75 | return Status::newFatal( 'newsletter-mainpage-non-existent' ); |
76 | } |
77 | |
78 | return Status::newGood(); |
79 | } |
80 | |
81 | } |