Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 16 |
CRAP | |
0.00% |
0 / 1 |
| LexiconEntry | |
0.00% |
0 / 38 |
|
0.00% |
0 / 16 |
650 | |
0.00% |
0 / 1 |
| findItemIndexByItem | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| findItemIndexBySpeechoidIdentity | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| findItemByItem | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| findItemBySpeechoidIdentity | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getItemAt | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addItem | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| replaceItem | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| replaceItemAt | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| deleteItem | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| deleteItemAt | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLanguage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setLanguage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getItems | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setItems | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Wikispeech\Lexicon; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | |
| 7 | /** |
| 8 | * @file |
| 9 | * @ingroup Extensions |
| 10 | * @license GPL-2.0-or-later |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * An entry in the pronunciation lexicon. |
| 15 | * |
| 16 | * An orthographic representation of word (key) in a given language, |
| 17 | * associated with a number of meanings that may have different pronunciations (items). |
| 18 | * |
| 19 | * @since 0.1.8 |
| 20 | */ |
| 21 | class LexiconEntry { |
| 22 | |
| 23 | /** @var string|null */ |
| 24 | private $language; |
| 25 | |
| 26 | /** @var string|null */ |
| 27 | private $key; |
| 28 | |
| 29 | /** @var LexiconEntryItem[] Lexicon entries sharing the key */ |
| 30 | private $items = []; |
| 31 | |
| 32 | // helper functions |
| 33 | |
| 34 | /** |
| 35 | * @since 0.1.9 |
| 36 | * @param LexiconEntryItem $item |
| 37 | * @return int|null Index of first item that match $speechoidIdentity |
| 38 | */ |
| 39 | public function findItemIndexByItem( LexiconEntryItem $item ): ?int { |
| 40 | $speechoidIdentity = $item->getSpeechoidIdentity(); |
| 41 | if ( $speechoidIdentity === null ) { |
| 42 | return null; |
| 43 | } |
| 44 | return $this->findItemIndexBySpeechoidIdentity( $speechoidIdentity ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @since 0.1.8 |
| 49 | * @param int $speechoidIdentity |
| 50 | * @return int|null Index of first item that match $speechoidIdentity |
| 51 | */ |
| 52 | public function findItemIndexBySpeechoidIdentity( int $speechoidIdentity ): ?int { |
| 53 | foreach ( $this->items as $itemIndex => $item ) { |
| 54 | if ( $speechoidIdentity === $item->getSpeechoidIdentity() ) { |
| 55 | return $itemIndex; |
| 56 | } |
| 57 | } |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @since 0.1.9 |
| 63 | * @param LexiconEntryItem $item |
| 64 | * @return LexiconEntryItem|null First item that match $item->speechoidIdentity |
| 65 | */ |
| 66 | public function findItemByItem( LexiconEntryItem $item ): ?LexiconEntryItem { |
| 67 | $speechoidIdentity = $item->getSpeechoidIdentity(); |
| 68 | if ( $speechoidIdentity === null ) { |
| 69 | return null; |
| 70 | } |
| 71 | return $this->findItemBySpeechoidIdentity( $speechoidIdentity ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @since 0.1.8 |
| 76 | * @param int $speechoidIdentity |
| 77 | * @return LexiconEntryItem|null First item that match $speechoidIdentity |
| 78 | */ |
| 79 | public function findItemBySpeechoidIdentity( int $speechoidIdentity ): ?LexiconEntryItem { |
| 80 | $index = $this->findItemIndexBySpeechoidIdentity( $speechoidIdentity ); |
| 81 | return $index === null ? null : $this->items[$index]; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @since 0.1.9 |
| 86 | * @param int $index |
| 87 | * @return LexiconEntryItem |
| 88 | */ |
| 89 | public function getItemAt( int $index ) { |
| 90 | return $this->items[$index]; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @since 0.1.9 |
| 95 | * @param LexiconEntryItem $item |
| 96 | */ |
| 97 | public function addItem( LexiconEntryItem $item ) { |
| 98 | $this->items[] = $item; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Replaces the item in the list of items that match Speechoid identity of the given item. |
| 103 | * @since 0.1.9 |
| 104 | * @param LexiconEntryItem $item |
| 105 | * @throws InvalidArgumentException If no item in the list match the identity of the given item. |
| 106 | * If item is missing speechoid identity. |
| 107 | */ |
| 108 | public function replaceItem( LexiconEntryItem $item ) { |
| 109 | $speechoidIdentity = $item->getSpeechoidIdentity(); |
| 110 | if ( $speechoidIdentity === null ) { |
| 111 | throw new InvalidArgumentException( 'Speechoid identity not set.' ); |
| 112 | } |
| 113 | $index = $this->findItemIndexBySpeechoidIdentity( $speechoidIdentity ); |
| 114 | if ( $index === null ) { |
| 115 | throw new InvalidArgumentException( 'Item is not a member' ); |
| 116 | } |
| 117 | $this->replaceItemAt( $index, $item ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @since 0.1.9 |
| 122 | * @param int $index |
| 123 | * @param LexiconEntryItem $item |
| 124 | */ |
| 125 | public function replaceItemAt( int $index, LexiconEntryItem $item ) { |
| 126 | $this->items[$index] = $item; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Removes the item in the list of items that match Speechoid identity of the given item. |
| 131 | * @since 0.1.9 |
| 132 | * @param LexiconEntryItem $item |
| 133 | * @throws InvalidArgumentException If no item in the list match the identity of the given item. |
| 134 | * If speechoid identity is not set in item. |
| 135 | */ |
| 136 | public function deleteItem( LexiconEntryItem $item ) { |
| 137 | $speechoidIdentity = $item->getSpeechoidIdentity(); |
| 138 | if ( $speechoidIdentity === null ) { |
| 139 | throw new InvalidArgumentException( 'Speechoid identity not set.' ); |
| 140 | } |
| 141 | $index = $this->findItemIndexBySpeechoidIdentity( $speechoidIdentity ); |
| 142 | if ( $index === null ) { |
| 143 | throw new InvalidArgumentException( 'Item is not a member' ); |
| 144 | } |
| 145 | $this->deleteItemAt( $index ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @since 0.1.9 |
| 150 | * @param int $index |
| 151 | */ |
| 152 | public function deleteItemAt( int $index ) { |
| 153 | unset( $this->items[$index] ); |
| 154 | } |
| 155 | |
| 156 | // getters and setters |
| 157 | |
| 158 | /** |
| 159 | * @since 0.1.8 |
| 160 | * @return string|null |
| 161 | */ |
| 162 | public function getLanguage(): ?string { |
| 163 | return $this->language; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @since 0.1.8 |
| 168 | * @param string $language |
| 169 | */ |
| 170 | public function setLanguage( string $language ): void { |
| 171 | $this->language = $language; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @since 0.1.8 |
| 176 | * @return string|null |
| 177 | */ |
| 178 | public function getKey(): ?string { |
| 179 | return $this->key; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @since 0.1.8 |
| 184 | * @param string $key |
| 185 | */ |
| 186 | public function setKey( string $key ): void { |
| 187 | $this->key = $key; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Do not use this method to modify the array from other classes than this! |
| 192 | * If you do, you will be modifying a clone of the array. |
| 193 | * |
| 194 | * @see deleteItemAt |
| 195 | * @see deleteItem |
| 196 | * @see replaceItemAt |
| 197 | * @see replaceItem |
| 198 | * @see addItem |
| 199 | * @see getItemAt |
| 200 | * @since 0.1.8 |
| 201 | * @return LexiconEntryItem[] |
| 202 | */ |
| 203 | public function getItems(): array { |
| 204 | return $this->items; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @since 0.1.8 |
| 209 | * @param LexiconEntryItem[] $items |
| 210 | */ |
| 211 | public function setItems( array $items ): void { |
| 212 | $this->items = $items; |
| 213 | } |
| 214 | |
| 215 | } |