Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.14% covered (success)
97.14%
34 / 35
88.89% covered (warning)
88.89%
8 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageImageCandidate
97.14% covered (success)
97.14%
34 / 35
88.89% covered (warning)
88.89%
8 / 9
13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 newFromFileAndParams
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
3.01
 newFromArray
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 getFileName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFullWidth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFullHeight
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHandlerWidth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFrameClass
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 jsonSerialize
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace PageImages;
4
5use JsonSerializable;
6use MediaWiki\FileRepo\File\File;
7
8/**
9 * Value object to hold information about page image candidates.
10 */
11class PageImageCandidate implements JsonSerializable {
12
13    private string $fileName;
14    private int $fullWidth = 0;
15    private int $fullHeight = 0;
16    private int $handlerWidth = 0;
17    private string $frameClass = '';
18
19    /**
20     * Private constructor.
21     * Use self::newFromFileAndParams to instantiate.
22     */
23    private function __construct() {
24    }
25
26    /**
27     * @param File $file
28     * @param array $fileParams from ParserMakeImageParams hook.
29     * @return self
30     */
31    public static function newFromFileAndParams( File $file, array $fileParams ): self {
32        $instance = new self();
33        $instance->fileName = $file->getTitle()->getDBkey();
34        $instance->fullWidth = $file->getWidth() ?? 0;
35        $instance->fullHeight = $file->getHeight() ?? 0;
36        if ( isset( $fileParams['handler']['width'] ) ) {
37            $instance->handlerWidth = (int)( $fileParams['handler']['width'] ?? 0 );
38        }
39        if ( isset( $fileParams['frame']['class'] ) ) {
40            // $fileParams['frame']['class'] is set in Parser::makeImage
41            $instance->frameClass = $fileParams['frame']['class'] ?? '';
42        }
43        return $instance;
44    }
45
46    /**
47     * Instantiate PageImageCandidate from $json created with self::jsonSerialize
48     *
49     * @internal
50     */
51    public static function newFromArray( array $array ): self {
52        $instance = new self();
53        $instance->fileName = $array['filename'];
54        $instance->fullWidth = $array['fullwidth'] ?? 0;
55        $instance->fullHeight = $array['fullheight'] ?? 0;
56        if ( isset( $array['handler']['width'] ) ) {
57            $instance->handlerWidth = $array['handler']['width'] ?? 0;
58        }
59        if ( isset( $array['frame']['class'] ) ) {
60            $instance->frameClass = $array['frame']['class'] ?? '';
61        }
62        return $instance;
63    }
64
65    public function getFileName(): string {
66        return $this->fileName;
67    }
68
69    public function getFullWidth(): int {
70        return $this->fullWidth;
71    }
72
73    public function getFullHeight(): int {
74        return $this->fullHeight;
75    }
76
77    public function getHandlerWidth(): int {
78        return $this->handlerWidth;
79    }
80
81    public function getFrameClass(): string {
82        return $this->frameClass;
83    }
84
85    /**
86     * @internal
87     */
88    public function jsonSerialize(): array {
89        return [
90            'filename' => $this->getFileName(),
91            'fullwidth' => $this->getFullWidth(),
92            'fullheight' => $this->getFullHeight(),
93            // Wrap in handler array for backwards-compatibility.
94            'handler' => [
95                'width' => $this->getHandlerWidth()
96            ],
97            'frame' => [
98                'class' => $this->getFrameClass()
99            ]
100        ];
101    }
102}