Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ExternalLexemeSerializer
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
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
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getSerialized
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\Lexeme\Serialization;
4
5use Serializers\DispatchableSerializer;
6use Serializers\Exceptions\SerializationException;
7use Serializers\Exceptions\UnsupportedObjectException;
8use Wikibase\Lexeme\Domain\Model\Lexeme;
9
10/**
11 * Serializer of Lexeme entities to be used to serializer entities for any external output
12 * (i.e. API, Special pages, dumps etc).
13 * For serialization to be used in the internal Wikibase storage layer
14 * use {@link StorageLexemeSerializer} instead.
15 *
16 * @license GPL-2.0-or-later
17 */
18class ExternalLexemeSerializer implements DispatchableSerializer {
19
20    /**
21     * @var StorageLexemeSerializer
22     */
23    private $internalSerializer;
24
25    public function __construct( StorageLexemeSerializer $internalSerializer ) {
26        $this->internalSerializer = $internalSerializer;
27    }
28
29    /**
30     * @see DispatchableSerializer::isSerializerFor
31     *
32     * @param mixed $object
33     *
34     * @return bool
35     */
36    public function isSerializerFor( $object ) {
37        return $object instanceof Lexeme;
38    }
39
40    /**
41     * @see Serializer::serialize
42     *
43     * @param Lexeme $object
44     *
45     * @throws SerializationException
46     * @return array
47     */
48    public function serialize( $object ) {
49        if ( !$this->isSerializerFor( $object ) ) {
50            throw new UnsupportedObjectException(
51                $object,
52                'ExternalLexemeSerializer can only serialize Lexeme objects.'
53            );
54        }
55
56        return $this->getSerialized( $object );
57    }
58
59    private function getSerialized( Lexeme $lexeme ): array {
60        $internalSerialization = $this->internalSerializer->serialize( $lexeme );
61
62        unset( $internalSerialization['nextFormId'] );
63        unset( $internalSerialization['nextSenseId'] );
64
65        return $internalSerialization;
66    }
67
68}