Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
55.00% covered (warning)
55.00%
11 / 20
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImageRecommendation
55.00% covered (warning)
55.00%
11 / 20
71.43% covered (warning)
71.43%
5 / 7
11.47
0.00% covered (danger)
0.00%
0 / 1
 fromArray
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getTitle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getImages
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDatasetId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 jsonSerialize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\NewcomerTasks\AddImage;
4
5use GrowthExperiments\NewcomerTasks\Recommendation;
6use JsonSerializable;
7use MediaWiki\Linker\LinkTarget;
8use MediaWiki\Title\TitleValue;
9
10/**
11 * Value object for machine-generated image recommendations. An image recommendation consists
12 * of a set of suggested ImageRecommendationImages for a given wiki page.
13 */
14class ImageRecommendation implements Recommendation, JsonSerializable {
15
16    /** @var LinkTarget */
17    private $title;
18
19    /** @var array */
20    private $images;
21
22    /** @var string */
23    private $datasetId;
24
25    /**
26     * Create an ImageRecommendation object from an array representation.
27     * This is the inverse of toArray().
28     * @param array $recommendationData
29     * @return self
30     */
31    public static function fromArray( array $recommendationData ): ImageRecommendation {
32        return new ImageRecommendation(
33            new TitleValue( $recommendationData['titleNamespace'], $recommendationData['titleText'] ),
34            array_map( [ ImageRecommendationImage::class, 'fromArray' ], $recommendationData['images'] ),
35            $recommendationData['datasetId']
36        );
37    }
38
39    /**
40     * @param LinkTarget $title Page for which the recommendations were generated.
41     * @param ImageRecommendationImage[] $images List of recommended images.
42     * @param string $datasetId Version of the recommendations dataset.
43     */
44    public function __construct(
45        LinkTarget $title,
46        array $images,
47        string $datasetId
48    ) {
49        $this->title = $title;
50        $this->images = $images;
51        $this->datasetId = $datasetId;
52    }
53
54    /** @inheritDoc */
55    public function getTitle(): LinkTarget {
56        return $this->title;
57    }
58
59    /**
60     * Get the images recommended for the article.
61     * @return ImageRecommendationImage[]
62     */
63    public function getImages(): array {
64        return $this->images;
65    }
66
67    /**
68     * Get the version of the recommendations dataset.
69     * @return string
70     */
71    public function getDatasetId(): string {
72        return $this->datasetId;
73    }
74
75    /**
76     * JSON-ifiable data that represents the state of the object.
77     * @return mixed[]
78     */
79    public function toArray(): array {
80        return [
81            'titleNamespace' => $this->title->getNamespace(),
82            'titleText' => $this->title->getText(),
83            'images' => array_map( static function ( ImageRecommendationImage $image ) {
84                return $image->toArray();
85            }, $this->images ),
86            'datasetId' => $this->datasetId,
87        ];
88    }
89
90    /** @inheritDoc */
91    public function jsonSerialize(): array {
92        return $this->toArray();
93    }
94}