Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
66.67% |
2 / 3 |
CRAP | |
92.00% |
23 / 25 |
ConflictFormValidator | |
0.00% |
0 / 1 |
|
66.67% |
2 / 3 |
15.12 | |
92.00% |
23 / 25 |
validateRequest | |
100.00% |
1 / 1 |
5 | |
100.00% |
11 / 11 |
|||
validateSideSelection | |
100.00% |
1 / 1 |
4 | |
100.00% |
5 / 5 |
|||
validateSingleColumnForm | |
0.00% |
0 / 1 |
6.40 | |
77.78% |
7 / 9 |
<?php | |
namespace TwoColConflict; | |
use WebRequest; | |
/** | |
* @license GPL-2.0-or-later | |
*/ | |
class ConflictFormValidator { | |
/** | |
* Check whether inputs are valid. Note that a POST without conflict fields is considered | |
* valid. | |
* | |
* @param WebRequest $request | |
* @return bool True when valid | |
*/ | |
public function validateRequest( WebRequest $request ): bool { | |
$contentRows = $request->getArray( 'mw-twocolconflict-split-content' ); | |
if ( $contentRows === null ) { | |
// Not a conflict form. | |
return true; | |
} | |
if ( $contentRows === [] ) { | |
// Empty conflict form is bad. | |
return false; | |
} | |
$sideSelection = $request->getArray( 'mw-twocolconflict-side-selector', [] ); | |
if ( $sideSelection ) { | |
return $this->validateSideSelection( $contentRows, $sideSelection ); | |
} | |
if ( $request->getBool( 'mw-twocolconflict-single-column-view' ) ) { | |
return $this->validateSingleColumnForm( $contentRows ); | |
} | |
return false; | |
} | |
/** | |
* @param array[] $contentRows | |
* @param string[] $sideSelection | |
* | |
* @return bool | |
*/ | |
private function validateSideSelection( array $contentRows, array $sideSelection ): bool { | |
foreach ( $contentRows as $num => $row ) { | |
$side = $sideSelection[$num] ?? 'copy'; | |
if ( !isset( $row[$side] ) || !is_string( $row[$side] ) ) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* @param array[] $contentRows | |
* | |
* @return bool | |
*/ | |
private function validateSingleColumnForm( array $contentRows ): bool { | |
foreach ( $contentRows as $num => $row ) { | |
if ( !is_array( $row ) || count( $row ) !== 1 ) { | |
// Must be an array with exactly one column. | |
return false; | |
} | |
$key = key( $row ); | |
if ( !in_array( $key, [ 'copy', 'other', 'your' ] ) ) { | |
// Illegal key. | |
return false; | |
} | |
if ( !is_string( $row[$key] ) ) { | |
// Contents must be a plain string. | |
return false; | |
} | |
} | |
return true; | |
} | |
} |