Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 89
0.00% covered (danger)
0.00%
0 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
PersistentMWAssociativeArraySerializer
0.00% covered (danger)
0.00%
0 / 89
0.00% covered (danger)
0.00%
0 / 15
462
0.00% covered (danger)
0.00%
0 / 1
 visitManuscript
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 visitManuscriptPrompt
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 visitRecording
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 visitLanguage
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 visitManuscriptDomain
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 visitRecordingAnnotation
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 visitRecordingAnnotations
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 visitRecordingReview
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 visitSkippedManuscriptPrompt
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 visitUser
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 visitUserDialect
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 visitUserLanguageProficiencyLevel
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 serializeTimestamp
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 serializeUuid
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 serializeJson
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\WikispeechSpeechDataCollector\Domain;
4
5/**
6 * @file
7 * @ingroup Extensions
8 * @license GPL-2.0-or-later
9 */
10
11use FormatJson;
12use MediaWiki\WikispeechSpeechDataCollector\Uuid;
13use MWException;
14use MWTimestamp;
15
16/**
17 * A {@link PersistentVisitor} that populates an associative array
18 * with data set in the persistent instance, to be used as an MW API response.
19 *
20 * $array = $persistent->accept( new PersistentMWAssociateArraySerializer() );
21 *
22 * @since 0.1.0
23 */
24class PersistentMWAssociativeArraySerializer implements PersistentVisitor {
25
26    /**
27     * @param Manuscript $manuscript
28     * @return array
29     */
30    public function visitManuscript(
31        Manuscript $manuscript
32    ): array {
33        $array = [];
34        $array['identity'] = $this->serializeUuid( $manuscript->getIdentity() );
35        $array['name'] = $manuscript->getName();
36        $array['created'] = $this->serializeTimestamp( $manuscript->getCreated() );
37        $array['disabled'] = $this->serializeTimestamp( $manuscript->getDisabled() );
38        $array['language'] = $this->serializeUuid( $manuscript->getLanguage() );
39        $array['domain'] = $this->serializeUuid( $manuscript->getDomain() );
40        return $array;
41    }
42
43    /**
44     * @param ManuscriptPrompt $manuscriptPrompt
45     * @return array
46     */
47    public function visitManuscriptPrompt(
48        ManuscriptPrompt $manuscriptPrompt
49    ): array {
50        $array = [];
51        $array['identity'] = $this->serializeUuid( $manuscriptPrompt->getIdentity() );
52        $array['manuscript'] = $this->serializeUuid( $manuscriptPrompt->getManuscript() );
53        $array['index'] = $manuscriptPrompt->getIndex();
54        $array['content'] = $manuscriptPrompt->getContent();
55        return $array;
56    }
57
58    /**
59     * @param Recording $recording
60     * @return array
61     */
62    public function visitRecording(
63        Recording $recording
64    ): array {
65        $array = [];
66        $array['identity'] = $this->serializeUuid( $recording->getIdentity() );
67        $array['recorded'] = $this->serializeTimestamp( $recording->getRecorded() );
68        $array['voiceOf'] = $this->serializeUuid( $recording->getVoiceOf() );
69        $array['spokenDialect'] = $this->serializeUuid( $recording->getSpokenDialect() );
70        $array['manuscriptPrompt'] = $this->serializeUuid( $recording->getManuscriptPrompt() );
71        $array['audioFileWikiPageIdentity'] = $recording->getAudioFileWikiPageIdentity();
72        return $array;
73    }
74
75    /**
76     * @param Language $language
77     * @return array
78     */
79    public function visitLanguage(
80        Language $language
81    ): array {
82        $array = [];
83        $array['identity'] = $this->serializeUuid( $language->getIdentity() );
84        $array['nativeName'] = $language->getNativeName();
85        $array['iso639a1'] = $language->getIso639a1();
86        $array['iso639a2b'] = $language->getIso639a2b();
87        $array['iso639a2t'] = $language->getIso639a2t();
88        $array['iso639a3'] = $language->getIso639a3();
89        return $array;
90    }
91
92    /**
93     * @param ManuscriptDomain $manuscriptDomain
94     * @return array
95     */
96    public function visitManuscriptDomain(
97        ManuscriptDomain $manuscriptDomain
98    ): array {
99        $array = [];
100        $array['identity'] = $this->serializeUuid( $manuscriptDomain->getIdentity() );
101        $array['name'] = $manuscriptDomain->getName();
102        $array['parent'] = $this->serializeUuid( $manuscriptDomain->getParent() );
103        return $array;
104    }
105
106    /**
107     * @param RecordingAnnotation $recordingAnnotation
108     * @return array
109     */
110    public function visitRecordingAnnotation(
111        RecordingAnnotation $recordingAnnotation
112    ): array {
113        $array = [];
114        $array['start'] = $recordingAnnotation->getStart();
115        $array['end'] = $recordingAnnotation->getEnd();
116        $array['stereotype'] = $recordingAnnotation->getStereotype();
117        $array['value'] = $recordingAnnotation->getValue();
118        return $array;
119    }
120
121    /**
122     * @param RecordingAnnotations $recordingAnnotations
123     * @return array
124     */
125    public function visitRecordingAnnotations(
126        RecordingAnnotations $recordingAnnotations
127    ): array {
128        $array = [];
129        $array['identity'] = $this->serializeUuid( $recordingAnnotations->getIdentity() );
130        $array['items'] = [];
131        if ( $recordingAnnotations->getItems() !== null &&
132            // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal
133            count( $recordingAnnotations->getItems() ) > 0
134        ) {
135            foreach ( $recordingAnnotations->getItems() as $recordingAnnotation ) {
136                $array['items'][] = $this->visitRecordingAnnotation( $recordingAnnotation );
137            }
138        }
139
140        return $array;
141    }
142
143    /**
144     * @param RecordingReview $recordingReview
145     * @return array
146     */
147    public function visitRecordingReview(
148        RecordingReview $recordingReview
149    ): array {
150        $array = [];
151        $array['identity'] = $this->serializeUuid( $recordingReview->getIdentity() );
152        $array['created'] = $this->serializeTimestamp( $recordingReview->getCreated() );
153        $array['value'] = $recordingReview->getValue();
154        $array['reviewer'] = $this->serializeUuid( $recordingReview->getReviewer() );
155        $array['recording'] = $this->serializeUuid( $recordingReview->getRecording() );
156        return $array;
157    }
158
159    /**
160     * @param SkippedManuscriptPrompt $skippedManuscriptPrompt
161     * @return array
162     */
163    public function visitSkippedManuscriptPrompt(
164        SkippedManuscriptPrompt $skippedManuscriptPrompt
165    ): array {
166        $array = [];
167        $array['identity'] = $this->serializeUuid( $skippedManuscriptPrompt->getIdentity() );
168        $array['manuscriptPrompt'] =
169            $this->serializeUuid( $skippedManuscriptPrompt->getManuscriptPrompt() );
170        $array['user'] = $this->serializeUuid( $skippedManuscriptPrompt->getUser() );
171        $array['skipped'] =    $this->serializeTimestamp( $skippedManuscriptPrompt->getSkipped() );
172        return $array;
173    }
174
175    /**
176     * @param User $user
177     * @return array
178     */
179    public function visitUser(
180        User $user
181    ): array {
182        $array = [];
183        $array['identity'] = $this->serializeUuid( $user->getIdentity() );
184        $array['mediaWikiUser'] = $user->getMediaWikiUser();
185        $array['yearBorn'] = $user->getYearBorn();
186        return $array;
187    }
188
189    /**
190     * @param UserDialect $userDialect
191     * @return array
192     */
193    public function visitUserDialect(
194        UserDialect $userDialect
195    ): array {
196        $array = [];
197        $array['identity'] = $this->serializeUuid( $userDialect->getIdentity() );
198        $array['user'] = $this->serializeUuid( $userDialect->getUser() );
199        $array['language'] = $this->serializeUuid( $userDialect->getLanguage() );
200        $array['spokenProficiencyLevel'] = $userDialect->getSpokenProficiencyLevel();
201        $array['location'] = $this->serializeJson( $userDialect->getLocation() );
202        return $array;
203    }
204
205    /**
206     * @param UserLanguageProficiencyLevel $languageProficiencyLevel
207     * @return array
208     */
209    public function visitUserLanguageProficiencyLevel(
210        UserLanguageProficiencyLevel $languageProficiencyLevel
211    ): array {
212        $array = [];
213        $array['identity'] = $this->serializeUuid( $languageProficiencyLevel->getIdentity() );
214        $array['user'] = $this->serializeUuid( $languageProficiencyLevel->getUser() );
215        $array['language'] = $this->serializeUuid( $languageProficiencyLevel->getLanguage() );
216        $array['proficiencyLevel'] = $languageProficiencyLevel->getProficiencyLevel();
217        return $array;
218    }
219
220    /**
221     * @param MWTimestamp|null $value
222     * @return string|null ISO 8601
223     */
224    private function serializeTimestamp(
225        ?MWTimestamp $value
226    ): ?string {
227        return $value === null ? null : $value->getTimestamp( TS_ISO_8601 );
228    }
229
230    /**
231     * @param string|null $value
232     * @return string|null Hex encoded UUID with dash separation.
233     */
234    private function serializeUuid(
235        ?string $value
236    ): ?string {
237        return Uuid::asHex( $value, true );
238    }
239
240    /**
241     * @param string|null $value
242     * @return mixed
243     * @throws MWException
244     */
245    private function serializeJson(
246        ?string $value
247    ) {
248        if ( $value === null ) {
249            return null;
250        }
251        $parsed = FormatJson::parse( $value, FormatJson::FORCE_ASSOC );
252        if ( !$parsed->isOK() ) {
253            throw new MWException( "Unable to parse JSON.\n" . $parsed->__toString() );
254        }
255        return $parsed->getValue();
256    }
257}