Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
LexiconEntryItem | |
0.00% |
0 / 21 |
|
0.00% |
0 / 9 |
306 | |
0.00% |
0 / 1 |
__toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getProperties | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setProperties | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTranscription | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getPreferred | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
removePreferred | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
copyFrom | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSpeechoidIdentity | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
toJson | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Wikispeech\Lexicon; |
4 | |
5 | /** |
6 | * @file |
7 | * @ingroup Extensions |
8 | * @license GPL-2.0-or-later |
9 | */ |
10 | |
11 | use 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 | */ |
24 | class LexiconEntryItem { |
25 | /** @var array|null Associative array. 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 array|null |
39 | */ |
40 | public function getProperties(): ?array { |
41 | return $this->properties; |
42 | } |
43 | |
44 | /** |
45 | * @since 0.1.8 |
46 | * @param array|null $properties |
47 | */ |
48 | public function setProperties( ?array $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 | !array_key_exists( 'preferred', $this->properties ) |
80 | ) { |
81 | return false; |
82 | } |
83 | |
84 | return $this->properties['preferred']; |
85 | } |
86 | |
87 | /** |
88 | * Remove preferred for this item |
89 | * |
90 | * @since 0.1.10 |
91 | */ |
92 | public function removePreferred() { |
93 | if ( $this->properties === null ) { |
94 | return; |
95 | } |
96 | |
97 | unset( $this->properties['preferred'] ); |
98 | } |
99 | |
100 | // access helpers. |
101 | |
102 | /** |
103 | * Makes this item look exactly like the source item. |
104 | * |
105 | * @since 0.1.8 |
106 | * @param LexiconEntryItem $source |
107 | */ |
108 | public function copyFrom( LexiconEntryItem $source ): void { |
109 | // this might look silly, |
110 | // but will make it easier to migrate to a future ad hoc data model |
111 | $this->setProperties( $source->getProperties() ); |
112 | } |
113 | |
114 | /** |
115 | * @since 0.1.8 |
116 | * @return int|null |
117 | */ |
118 | public function getSpeechoidIdentity(): ?int { |
119 | $properties = $this->getProperties(); |
120 | return $properties !== null && array_key_exists( 'id', $properties ) |
121 | ? $properties['id'] : null; |
122 | } |
123 | |
124 | /** |
125 | * @since 0.1.9 |
126 | * @return string Empty if JSON encoding failed. |
127 | */ |
128 | public function toJson(): string { |
129 | // @todo Handle empty objects properly TT279916 |
130 | if ( isset( $this->properties['lemma'] ) ) { |
131 | $this->properties['lemma'] = (object)$this->properties['lemma']; |
132 | } |
133 | $json = FormatJson::encode( $this->properties, true ); |
134 | return $json ?: ''; |
135 | } |
136 | } |