Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.25% covered (warning)
81.25%
13 / 16
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SenseDeserializer
81.25% covered (warning)
81.25%
13 / 16
40.00% covered (danger)
40.00%
2 / 5
8.42
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 deserialize
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 deserializeId
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 deserializeGlossList
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 deserializeStatementList
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
1<?php
2
3namespace Wikibase\Lexeme\Serialization;
4
5use Deserializers\Deserializer;
6use Deserializers\Exceptions\DeserializationException;
7use Wikibase\DataModel\Deserializers\StatementListDeserializer;
8use Wikibase\DataModel\Deserializers\TermListDeserializer;
9use Wikibase\DataModel\Statement\StatementList;
10use Wikibase\DataModel\Term\TermList;
11use Wikibase\Lexeme\Domain\Model\Sense;
12use Wikibase\Lexeme\Domain\Model\SenseId;
13
14/**
15 * @license GPL-2.0-or-later
16 */
17class SenseDeserializer implements Deserializer {
18
19    /**
20     * @var TermListDeserializer
21     */
22    private $termListDeserializer;
23
24    /**
25     * @var StatementListDeserializer
26     */
27    private $statementListDeserializer;
28
29    public function __construct(
30        TermListDeserializer $termListDeserializer,
31        StatementListDeserializer $statementListDeserializer
32    ) {
33        $this->termListDeserializer = $termListDeserializer;
34        $this->statementListDeserializer = $statementListDeserializer;
35    }
36
37    /**
38     * @param array $serialization
39     * @return Sense
40     */
41    public function deserialize( $serialization ) {
42        return new Sense(
43            $this->deserializeId( $serialization ),
44            $this->deserializeGlossList( $serialization ),
45            $this->deserializeStatementList( $serialization )
46        );
47    }
48
49    /**
50     * @param array $serialization
51     * @return SenseId
52     */
53    private function deserializeId( array $serialization ) {
54        if ( !array_key_exists( 'id', $serialization ) ) {
55            throw new DeserializationException( 'Sense is missing id' );
56        }
57
58        return new SenseId( $serialization['id'] );
59    }
60
61    /**
62     * @param array $serialization
63     * @return TermList
64     */
65    private function deserializeGlossList( array $serialization ) {
66        if ( !array_key_exists( 'glosses', $serialization ) ) {
67            throw new DeserializationException( 'Sense is missing glosses' );
68        }
69
70        return $this->termListDeserializer->deserialize( $serialization['glosses'] );
71    }
72
73    /**
74     * @param array $serialization
75     * @return StatementList
76     */
77    private function deserializeStatementList( array $serialization ) {
78        if ( !array_key_exists( 'claims', $serialization ) ) {
79            throw new DeserializationException( 'Sense is missing claims' );
80        }
81
82        return $this->statementListDeserializer->deserialize( $serialization['claims'] );
83    }
84
85}