Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.24% |
20 / 21 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
Topic | |
95.24% |
20 / 21 |
|
87.50% |
7 / 8 |
10 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getGroupId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getGroupName | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
getViewData | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
toJsonArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
newFromJsonArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\NewcomerTasks\Topic; |
4 | |
5 | use LogicException; |
6 | use MediaWiki\Json\JsonUnserializable; |
7 | use MediaWiki\Json\JsonUnserializableTrait; |
8 | use MediaWiki\Json\JsonUnserializer; |
9 | use MediaWiki\Message\Message; |
10 | use MessageLocalizer; |
11 | |
12 | /** |
13 | * A topic represents a subgroup of tasks based on the topic of the associated page |
14 | * (such as biology-related tasks or tasks related to Japan). |
15 | * Topic objects should also contain all the configuration necessary for filtering |
16 | * to that topic in TaskSuggester. |
17 | */ |
18 | class Topic implements JsonUnserializable { |
19 | |
20 | use JsonUnserializableTrait; |
21 | |
22 | /** @var string */ |
23 | protected $id; |
24 | |
25 | /** @var string */ |
26 | private $groupId; |
27 | |
28 | /** |
29 | * @param string $id Topic ID, a string consisting of lowercase alphanumeric characters |
30 | * and dashes. E.g. 'biology'. |
31 | * @param string|null $groupId Topic group, for visual grouping. E.g. 'science'. |
32 | */ |
33 | public function __construct( string $id, ?string $groupId = null ) { |
34 | $this->id = $id; |
35 | $this->groupId = $groupId; |
36 | } |
37 | |
38 | /** |
39 | * Returns the topic ID, a string consisting of lowercase alphanumeric characters |
40 | * and dashes (e.g. 'biology'). |
41 | * @return string |
42 | */ |
43 | public function getId() { |
44 | return $this->id; |
45 | } |
46 | |
47 | /** |
48 | * Human-readable name of the topic. |
49 | * @param MessageLocalizer $messageLocalizer |
50 | * @return Message |
51 | */ |
52 | public function getName( MessageLocalizer $messageLocalizer ): Message { |
53 | return $messageLocalizer->msg( 'growthexperiments-homepage-suggestededits-topic-name-' |
54 | . $this->getId() ); |
55 | } |
56 | |
57 | /** |
58 | * Topic group ID. Topics in the same group are related; can be used e.g. for visual |
59 | * grouping of topics. |
60 | * @return string|null |
61 | */ |
62 | public function getGroupId() { |
63 | return $this->groupId; |
64 | } |
65 | |
66 | /** |
67 | * Human-readable name of the topic group. Must not be called when getGroupId() is null. |
68 | * @param MessageLocalizer $messageLocalizer |
69 | * @return Message |
70 | */ |
71 | public function getGroupName( MessageLocalizer $messageLocalizer ): Message { |
72 | if ( $this->groupId === null ) { |
73 | throw new LogicException( 'getGroupName should not be called when getGroupId is null' ); |
74 | } |
75 | return $messageLocalizer->msg( 'growthexperiments-homepage-suggestededits-topic-group-name-' |
76 | . $this->getGroupId() ); |
77 | } |
78 | |
79 | /** |
80 | * Return an array (JSON-ish) representation of the topic. |
81 | * @param MessageLocalizer $messageLocalizer |
82 | * @return array |
83 | */ |
84 | public function getViewData( MessageLocalizer $messageLocalizer ) { |
85 | return [ |
86 | 'id' => $this->getId(), |
87 | 'name' => $this->getName( $messageLocalizer )->text(), |
88 | 'groupId' => $this->getGroupId(), |
89 | 'groupName' => $this->getGroupId() ? $this->getGroupName( $messageLocalizer )->text() : null, |
90 | ]; |
91 | } |
92 | |
93 | /** @inheritDoc */ |
94 | protected function toJsonArray(): array { |
95 | return [ |
96 | 'id' => $this->getId(), |
97 | 'groupId' => $this->getGroupId(), |
98 | ]; |
99 | } |
100 | |
101 | /** @inheritDoc */ |
102 | public static function newFromJsonArray( JsonUnserializer $unserializer, array $json ) { |
103 | return new static( $json['id'], $json['groupId'] ); |
104 | } |
105 | |
106 | } |