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 File;
6use JsonSerializable;
7
8/**
9 * Value object to hold information about page image candidates.
10 * @package PageImages
11 */
12class PageImageCandidate implements JsonSerializable {
13
14    /** @var string */
15    private $fileName;
16
17    /** @var int */
18    private $fullWidth = 0;
19
20    /** @var int */
21    private $fullHeight = 0;
22
23    /** @var int */
24    private $handlerWidth = 0;
25
26    /** @var string */
27    private $frameClass = '';
28
29    /**
30     * Private constructor.
31     * Use self::newFromFileAndParams to instantiate.
32     */
33    private function __construct() {
34    }
35
36    /**
37     * @param File $file
38     * @param array $fileParams from ParserMakeImageParams hook.
39     * @return PageImageCandidate
40     */
41    public static function newFromFileAndParams( File $file, array $fileParams ): self {
42        $instance = new self();
43        $instance->fileName = $file->getTitle()->getDBkey();
44        $instance->fullWidth = $file->getWidth() ?? 0;
45        $instance->fullHeight = $file->getHeight() ?? 0;
46        if ( isset( $fileParams['handler']['width'] ) ) {
47            $instance->handlerWidth = (int)( $fileParams['handler']['width'] ?? 0 );
48        }
49        if ( isset( $fileParams['frame']['class'] ) ) {
50            // $fileParams['frame']['class'] is set in Parser::makeImage
51            $instance->frameClass = $fileParams['frame']['class'] ?? '';
52        }
53        return $instance;
54    }
55
56    /**
57     * Instantiate PageImageCandidate from $json created with self::jsonSerialize
58     *
59     * @param array $array
60     * @return PageImageCandidate
61     * @internal
62     */
63    public static function newFromArray( array $array ): self {
64        $instance = new self();
65        $instance->fileName = $array['filename'];
66        $instance->fullWidth = $array['fullwidth'] ?? 0;
67        $instance->fullHeight = $array['fullheight'] ?? 0;
68        if ( isset( $array['handler']['width'] ) ) {
69            $instance->handlerWidth = $array['handler']['width'] ?? 0;
70        }
71        if ( isset( $array['frame']['class'] ) ) {
72            $instance->frameClass = $array['frame']['class'] ?? '';
73        }
74        return $instance;
75    }
76
77    /**
78     * @return string
79     */
80    public function getFileName(): string {
81        return $this->fileName;
82    }
83
84    /**
85     * @return int
86     */
87    public function getFullWidth(): int {
88        return $this->fullWidth;
89    }
90
91    /**
92     * @return int
93     */
94    public function getFullHeight(): int {
95        return $this->fullHeight;
96    }
97
98    /**
99     * @return int
100     */
101    public function getHandlerWidth(): int {
102        return $this->handlerWidth;
103    }
104
105    /**
106     * @return string
107     */
108    public function getFrameClass(): string {
109        return $this->frameClass;
110    }
111
112    /**
113     * @internal
114     * @return array
115     */
116    public function jsonSerialize(): array {
117        return [
118            'filename' => $this->getFileName(),
119            'fullwidth' => $this->getFullWidth(),
120            'fullheight' => $this->getFullHeight(),
121            // Wrap in handler array for backwards-compatibility.
122            'handler' => [
123                'width' => $this->getHandlerWidth()
124            ],
125            'frame' => [
126                'class' => $this->getFrameClass()
127            ]
128        ];
129    }
130}