Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.19% covered (success)
92.19%
59 / 64
81.82% covered (warning)
81.82%
9 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
LexemeDeserializer
92.19% covered (success)
92.19%
59 / 64
81.82% covered (warning)
81.82%
9 / 11
25.30
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 deserialize
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 deserializeId
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 deserializeStatements
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 deserializeLemmas
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 deserializeLexicalCategory
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 deserializeLanguage
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 deserializeForms
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 deserializeForm
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
3.04
 deserializeSenses
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 deserializeNextSenseId
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
5.26
1<?php
2
3namespace Wikibase\Lexeme\Serialization;
4
5use Deserializers\Deserializer;
6use Deserializers\Exceptions\DeserializationException;
7use Deserializers\TypedObjectDeserializer;
8use Wikibase\DataModel\Deserializers\TermDeserializer;
9use Wikibase\DataModel\Deserializers\TermListDeserializer;
10use Wikibase\DataModel\Entity\ItemId;
11use Wikibase\DataModel\Statement\StatementList;
12use Wikibase\DataModel\Term\TermList;
13use Wikibase\Lexeme\Domain\Model\Form;
14use Wikibase\Lexeme\Domain\Model\FormId;
15use Wikibase\Lexeme\Domain\Model\FormSet;
16use Wikibase\Lexeme\Domain\Model\Lexeme;
17use Wikibase\Lexeme\Domain\Model\LexemeId;
18use Wikibase\Lexeme\Domain\Model\SenseSet;
19
20/**
21 * @license GPL-2.0-or-later
22 * @author Amir Sarabadani <ladsgroup@gmail.com>
23 */
24class LexemeDeserializer extends TypedObjectDeserializer {
25
26    /**
27     * @var Deserializer
28     */
29    private $entityIdDeserializer;
30
31    /**
32     * @var Deserializer
33     */
34    private $termListDeserializer;
35
36    /**
37     * @var Deserializer
38     */
39    private $statementListDeserializer;
40
41    /**
42     * @var SenseDeserializer
43     */
44    private $senseDeserializer;
45
46    public function __construct(
47        Deserializer $entityIdDeserializer,
48        Deserializer $statementListDeserializer
49    ) {
50        parent::__construct( 'lexeme', 'type' );
51
52        $this->entityIdDeserializer = $entityIdDeserializer;
53        $this->termListDeserializer = new TermListDeserializer( new TermDeserializer() );
54        $this->statementListDeserializer = $statementListDeserializer;
55        $this->senseDeserializer = new SenseDeserializer(
56            $this->termListDeserializer,
57            // @phan-suppress-next-line PhanTypeMismatchArgumentSuperType
58            $statementListDeserializer
59        );
60    }
61
62    /**
63     * @param array $serialization
64     *
65     * @throws DeserializationException
66     * @return Lexeme
67     */
68    public function deserialize( $serialization ) {
69        $this->assertCanDeserialize( $serialization );
70
71        return new Lexeme(
72            $this->deserializeId( $serialization ),
73            $this->deserializeLemmas( $serialization ),
74            $this->deserializeLexicalCategory( $serialization ),
75            $this->deserializeLanguage( $serialization ),
76            $this->deserializeStatements( $serialization ),
77            $serialization['nextFormId'],
78            $this->deserializeForms( $serialization ),
79            $this->deserializeNextSenseId( $serialization ),
80            $this->deserializeSenses( $serialization )
81        );
82    }
83
84    /**
85     * @param array $serialization
86     *
87     * @return LexemeId|null
88     */
89    private function deserializeId( array $serialization ) {
90        if ( array_key_exists( 'id', $serialization ) ) {
91            return new LexemeId( $serialization['id'] );
92        }
93
94        return null;
95    }
96
97    /**
98     * @param array $serialization
99     *
100     * @return StatementList|null
101     */
102    private function deserializeStatements( array $serialization ) {
103        if ( array_key_exists( 'claims', $serialization ) ) {
104            return $this->statementListDeserializer->deserialize( $serialization['claims'] );
105        }
106
107        return null;
108    }
109
110    /**
111     * @param array $serialization
112     *
113     * @return TermList|null
114     */
115    private function deserializeLemmas( array $serialization ) {
116        if ( array_key_exists( 'lemmas', $serialization ) ) {
117            return $this->termListDeserializer->deserialize( $serialization['lemmas'] );
118        }
119
120        return null;
121    }
122
123    /**
124     * @param array $serialization
125     *
126     * @return ItemId|null
127     */
128    private function deserializeLexicalCategory( array $serialization ) {
129        if ( array_key_exists( 'lexicalCategory', $serialization ) ) {
130            return $this->entityIdDeserializer->deserialize( $serialization['lexicalCategory'] );
131        }
132
133        return null;
134    }
135
136    /**
137     * @param array $serialization
138     *
139     * @return ItemId|null
140     */
141    private function deserializeLanguage( array $serialization ) {
142        if ( array_key_exists( 'language', $serialization ) ) {
143            return $this->entityIdDeserializer->deserialize( $serialization['language'] );
144        }
145
146        return null;
147    }
148
149    /**
150     * @param array $serialization
151     *
152     * @return FormSet
153     */
154    private function deserializeForms( array $serialization ) {
155        // TODO: Extract to a FormsDeserializer
156        $forms = new FormSet( [] );
157
158        if ( array_key_exists( 'forms', $serialization ) ) {
159            foreach ( $serialization['forms'] as $formSerialization ) {
160                $forms->add( $this->deserializeForm( $formSerialization ) );
161            }
162        }
163
164        return $forms;
165    }
166
167    /**
168     * @param array $serialization
169     *
170     * @throws DeserializationException
171     * @return Form
172     */
173    private function deserializeForm( array $serialization ) {
174        $id = null;
175
176        if ( array_key_exists( 'id', $serialization ) ) {
177            // We may want to use an EntityIdDeserializer here
178            $id = new FormId( $serialization['id'] );
179        } else {
180            throw new DeserializationException( "No id found in Form serialization" );
181        }
182
183        $representations = $this->termListDeserializer->deserialize(
184            $serialization['representations']
185        );
186
187        $grammaticalFeatures = [];
188        foreach ( $serialization['grammaticalFeatures'] as $featureId ) {
189            $grammaticalFeatures[] = $this->entityIdDeserializer->deserialize( $featureId );
190        }
191
192        $statements = $this->statementListDeserializer->deserialize( $serialization['claims'] );
193
194        return new Form( $id, $representations, $grammaticalFeatures, $statements );
195    }
196
197    private function deserializeSenses( array $serialization ) {
198        $senses = new SenseSet();
199
200        if ( array_key_exists( 'senses', $serialization ) ) {
201            foreach ( $serialization['senses'] as $senseSerialization ) {
202                $senses->add( $this->senseDeserializer->deserialize( $senseSerialization ) );
203            }
204        }
205
206        return $senses;
207    }
208
209    /**
210     * @param array $serialization
211     * @return int
212     */
213    private function deserializeNextSenseId( array $serialization ) {
214        if ( array_key_exists( 'nextSenseId', $serialization ) ) {
215            return $serialization['nextSenseId'];
216        } else {
217            if ( array_key_exists( 'senses', $serialization ) && $serialization['senses'] !== [] ) {
218                throw new DeserializationException(
219                    'Lexeme serialization has senses but no nextSenseId'
220                );
221            }
222            return 1;
223        }
224    }
225
226}