Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| FileRevision | |
100.00% |
8 / 8 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| throwExceptionIfMissingFields | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace FileImporter\Data; |
| 4 | |
| 5 | use FileImporter\Exceptions\InvalidArgumentException; |
| 6 | |
| 7 | /** |
| 8 | * This class represents a single revision of a files, as recognized by MediaWiki. |
| 9 | * This data can all be retrieved from the API or the Database and can be used to copy |
| 10 | * the exact revision onto another site. |
| 11 | * |
| 12 | * @license GPL-2.0-or-later |
| 13 | * @author Addshore |
| 14 | */ |
| 15 | class FileRevision { |
| 16 | |
| 17 | private const ERROR_MISSING_FIELD = 'revisionMissingField'; |
| 18 | |
| 19 | private const REQUIRED_FIELDS = [ |
| 20 | 'name', |
| 21 | 'description', |
| 22 | 'user', |
| 23 | 'timestamp', |
| 24 | 'size', |
| 25 | 'thumburl', |
| 26 | 'url', |
| 27 | ]; |
| 28 | |
| 29 | /** |
| 30 | * @throws InvalidArgumentException if incorrect fields are entered |
| 31 | */ |
| 32 | public function __construct( |
| 33 | private readonly array $fields, |
| 34 | ) { |
| 35 | $this->throwExceptionIfMissingFields( $fields ); |
| 36 | } |
| 37 | |
| 38 | private function throwExceptionIfMissingFields( array $fields ): void { |
| 39 | $diff = array_diff_key( array_flip( self::REQUIRED_FIELDS ), $fields ); |
| 40 | if ( $diff !== [] ) { |
| 41 | throw new InvalidArgumentException( |
| 42 | __CLASS__ . ': Missing ' . key( $diff ) . ' field on construction', |
| 43 | self::ERROR_MISSING_FIELD ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return mixed|null Null if the field isn't known |
| 49 | */ |
| 50 | public function getField( string $name ) { |
| 51 | return $this->fields[$name] ?? null; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @internal for debugging only |
| 56 | */ |
| 57 | public function getFields(): array { |
| 58 | return $this->fields; |
| 59 | } |
| 60 | |
| 61 | } |