Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
LexiconEntryItem
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 9
272
0.00% covered (danger)
0.00%
0 / 1
 __toString
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getProperties
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setProperties
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTranscription
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getPreferred
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 removePreferred
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 copyFrom
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSpeechoidIdentity
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 toJson
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Wikispeech\Lexicon;
4
5/**
6 * @file
7 * @ingroup Extensions
8 * @license GPL-2.0-or-later
9 */
10
11use FormatJson;
12
13/**
14 * Multiple items can exist for the same word. E.g:
15 * Records: FooBar Records, the name of a recording studio.
16 * Records: John records the problem in the notebook.
17 *
18 * This class only contains a single deserialized JSON-blob,
19 * but in the future this allows for easy extending to
20 * a future ad hoc data model.
21 *
22 * @since 0.1.8
23 */
24class LexiconEntryItem {
25    /** @var \stdClass|null stdClass - object. Deserialized Speechoid JSON entry */
26    private $properties;
27
28    /**
29     * @since 0.1.9
30     * @return string
31     */
32    public function __toString(): string {
33        return $this->toJson();
34    }
35
36    /**
37     * @since 0.1.8
38     * @return \stdClass|null
39     */
40    public function getProperties(): ?\stdClass {
41        return $this->properties;
42    }
43
44    /**
45     * @since 0.1.8
46     * @param \stdClass|null $properties
47     */
48    public function setProperties( ?\stdClass $properties ): void {
49        $this->properties = $properties;
50    }
51
52    /**
53     * Get the first transcription for this item
54     *
55     * It's assumed that there is only one transcription. While it's
56     * technically possible to have multiple transcriptions in
57     * Speechoid, it's unclear when this would happen.
58     *
59     * @since 0.1.10
60     * @return string
61     */
62    public function getTranscription(): string {
63        if ( $this->properties === null ) {
64            return '';
65        }
66
67        return $this->properties->transcriptions[0]->strn;
68    }
69
70    /**
71     * Get preferred for this item
72     *
73     * @since 0.1.10
74     * @return bool The value of preferred or false if not present.
75     */
76    public function getPreferred(): bool {
77        if (
78            $this->properties === null ||
79            !property_exists( $this->properties, 'preferred' )
80        ) {
81            return false;
82        }
83        return $this->properties->preferred;
84    }
85
86    /**
87     * Remove preferred for this item
88     *
89     * @since 0.1.10
90     */
91    public function removePreferred() {
92        if ( $this->properties === null ) {
93            return;
94        }
95
96        unset( $this->properties->preferred );
97    }
98
99    // access helpers.
100
101    /**
102     * Makes this item look exactly like the source item.
103     *
104     * @since 0.1.8
105     * @param LexiconEntryItem $source
106     */
107    public function copyFrom( LexiconEntryItem $source ): void {
108        // this might look silly,
109        // but will make it easier to migrate to a future ad hoc data model
110        $this->setProperties( $source->getProperties() );
111    }
112
113    /**
114     * @since 0.1.8
115     * @return int|null
116     */
117    public function getSpeechoidIdentity(): ?int {
118        $properties = $this->getProperties();
119        return $properties !== null && property_exists( $properties, 'id' )
120            ? $properties->id : null;
121    }
122
123    /**
124     * @since 0.1.9
125     * @return string Empty if JSON encoding failed.
126     */
127    public function toJson(): string {
128        $json = FormatJson::encode( $this->properties, true );
129        return $json ?: '';
130    }
131}