Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
FileRevision | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
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 | private array $fields; |
30 | |
31 | /** |
32 | * @throws InvalidArgumentException if incorrect fields are entered |
33 | */ |
34 | public function __construct( array $fields ) { |
35 | $this->throwExceptionIfMissingFields( $fields ); |
36 | $this->fields = $fields; |
37 | } |
38 | |
39 | private function throwExceptionIfMissingFields( array $fields ): void { |
40 | $diff = array_diff_key( array_flip( self::REQUIRED_FIELDS ), $fields ); |
41 | if ( $diff !== [] ) { |
42 | throw new InvalidArgumentException( |
43 | __CLASS__ . ': Missing ' . key( $diff ) . ' field on construction', |
44 | self::ERROR_MISSING_FIELD ); |
45 | } |
46 | } |
47 | |
48 | /** |
49 | * @return mixed|null Null if the field isn't known |
50 | */ |
51 | public function getField( string $name ) { |
52 | return $this->fields[$name] ?? null; |
53 | } |
54 | |
55 | /** |
56 | * @internal for debugging only |
57 | */ |
58 | public function getFields(): array { |
59 | return $this->fields; |
60 | } |
61 | |
62 | } |