Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.26% covered (warning)
78.26%
36 / 46
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
StorageLexemeSerializer
78.26% covered (warning)
78.26%
36 / 46
66.67% covered (warning)
66.67%
4 / 6
13.48
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 isSerializerFor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 serialize
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
3.19
 getSerialized
75.00% covered (warning)
75.00%
18 / 24
0.00% covered (danger)
0.00%
0 / 1
4.25
 serializeForms
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 serializeSenses
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Wikibase\Lexeme\Serialization;
4
5use Serializers\DispatchableSerializer;
6use Serializers\Exceptions\SerializationException;
7use Serializers\Exceptions\UnsupportedObjectException;
8use Serializers\Serializer;
9use UnexpectedValueException;
10use Wikibase\Lexeme\Domain\Model\FormSet;
11use Wikibase\Lexeme\Domain\Model\Lexeme;
12use Wikibase\Lexeme\Domain\Model\SenseSet;
13
14/**
15 * @license GPL-2.0-or-later
16 * @author Amir Sarabadani <ladsgroup@gmail.com>
17 */
18class StorageLexemeSerializer implements DispatchableSerializer {
19
20    /**
21     * @var Serializer
22     */
23    private $termListSerializer;
24
25    /**
26     * @var Serializer
27     */
28    private $statementListSerializer;
29
30    /**
31     * @var Serializer
32     */
33    private $formSerializer;
34
35    /**
36     * @var Serializer
37     */
38    private $senseSerializer;
39
40    public function __construct(
41        Serializer $termListSerializer,
42        Serializer $statementListSerializer
43    ) {
44        $this->termListSerializer = $termListSerializer;
45        $this->statementListSerializer = $statementListSerializer;
46        $this->formSerializer = new FormSerializer( $termListSerializer, $statementListSerializer );
47        $this->senseSerializer = new SenseSerializer(
48            $termListSerializer,
49            $statementListSerializer
50        );
51    }
52
53    /**
54     * @see DispatchableSerializer::isSerializerFor
55     *
56     * @param mixed $object
57     *
58     * @return bool
59     */
60    public function isSerializerFor( $object ) {
61        return $object instanceof Lexeme;
62    }
63
64    /**
65     * @see Serializer::serialize
66     *
67     * @param Lexeme $object
68     *
69     * @throws SerializationException
70     * @return array
71     */
72    public function serialize( $object ) {
73        if ( !$this->isSerializerFor( $object ) ) {
74            throw new UnsupportedObjectException(
75                $object,
76                'LexemeSerializer can only serialize Lexeme objects.'
77            );
78        }
79
80        return $this->getSerialized( $object );
81    }
82
83    /**
84     * @param Lexeme $lexeme
85     *
86     * @throws SerializationException
87     * @return array
88     */
89    private function getSerialized( Lexeme $lexeme ) {
90        $serialization = [ 'type' => $lexeme->getType() ];
91
92        $id = $lexeme->getId();
93
94        if ( $id !== null ) { // FIXME: Should fail if ID is not present
95            $serialization['id'] = $id->getSerialization();
96        }
97
98        // FIXME: Should always present
99        if ( !$lexeme->getLemmas()->isEmpty() ) {
100            $serialization['lemmas'] = $this->termListSerializer->serialize(
101                $lexeme->getLemmas()
102            );
103        }
104
105        try {
106            $serialization['lexicalCategory'] = $lexeme->getLexicalCategory()->getSerialization();
107            $serialization['language'] = $lexeme->getLanguage()->getSerialization();
108        } catch ( UnexpectedValueException $ex ) {
109            throw new UnsupportedObjectException(
110                $lexeme,
111                'Can not serialize incomplete Lexeme',
112                $ex
113            );
114        }
115
116        $serialization['claims'] = $this->statementListSerializer->serialize(
117            $lexeme->getStatements()
118        );
119
120        $serialization['nextFormId'] = $lexeme->getNextFormId();
121        $serialization['nextSenseId'] = $lexeme->getNextSenseId();
122
123        $serialization['forms'] = $this->serializeForms( $lexeme->getForms() );
124        $serialization['senses'] = $this->serializeSenses( $lexeme->getSenses() );
125
126        return $serialization;
127    }
128
129    /**
130     * @param FormSet $forms
131     *
132     * @return array[]
133     */
134    private function serializeForms( FormSet $forms ) {
135        $serialization = [];
136
137        foreach ( $forms->toArray() as $form ) {
138            $serialization[] = $this->formSerializer->serialize( $form );
139        }
140
141        return $serialization;
142    }
143
144    /**
145     * @param SenseSet $senses
146     *
147     * @return array[]
148     */
149    private function serializeSenses( SenseSet $senses ) {
150        $serialization = [];
151
152        foreach ( $senses->toArray() as $sense ) {
153            $serialization[] = $this->senseSerializer->serialize( $sense );
154        }
155
156        return $serialization;
157    }
158
159}