Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.30% covered (success)
91.30%
21 / 23
81.82% covered (warning)
81.82%
9 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Sense
91.30% covered (success)
91.30%
21 / 23
81.82% covered (warning)
81.82%
9 / 11
18.21
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGlosses
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStatements
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 copy
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __clone
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setId
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 isEmpty
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 equals
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 clear
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\Lexeme\Domain\Model;
4
5use LogicException;
6use Wikibase\DataModel\Entity\ClearableEntity;
7use Wikibase\DataModel\Entity\StatementListProvidingEntity;
8use Wikibase\DataModel\Statement\StatementList;
9use Wikibase\DataModel\Term\TermList;
10use Wikibase\Lexeme\Domain\DummyObjects\DummySenseId;
11use Wikibase\Lexeme\Domain\DummyObjects\NullSenseId;
12use Wikimedia\Assert\Assert;
13
14/**
15 * Mutable (e.g. the provided StatementList can be changed) implementation of a Lexeme's sense in
16 * the lexicographical data model.
17 *
18 * @see https://www.mediawiki.org/wiki/Extension:WikibaseLexeme/Data_Model#Sense
19 *
20 * @license GPL-2.0-or-later
21 */
22class Sense implements StatementListProvidingEntity, ClearableEntity {
23
24    public const ENTITY_TYPE = 'sense';
25
26    /**
27     * @var SenseId
28     */
29    protected $id;
30
31    /**
32     * @var TermList
33     */
34    protected $glossList;
35
36    /**
37     * @var StatementList
38     */
39    protected $statementList;
40
41    public function __construct(
42        SenseId $id,
43        TermList $glossList,
44        StatementList $statementList = null
45    ) {
46        $this->id = $id;
47        $this->glossList = $glossList; // TODO: check there is at least gloss in one language provided
48        $this->statementList = $statementList ?: new StatementList();
49    }
50
51    /**
52     * @return SenseId
53     */
54    public function getId() {
55        return $this->id;
56    }
57
58    /**
59     * @return TermList
60     */
61    public function getGlosses() {
62        return $this->glossList;
63    }
64
65    /**
66     * @return StatementList
67     */
68    public function getStatements() {
69        return $this->statementList;
70    }
71
72    /**
73     * @see EntityDocument::copy
74     *
75     * @since 0.1
76     *
77     * @return self
78     */
79    public function copy() {
80        return clone $this;
81    }
82
83    /**
84     * @see http://php.net/manual/en/language.oop5.cloning.php
85     *
86     * @since 5.1
87     */
88    public function __clone() {
89        // TermList is mutable, but Term is not. No deeper cloning necessary.
90        $this->glossList = clone $this->glossList;
91        $this->statementList = clone $this->statementList;
92    }
93
94    /**
95     * @return string
96     */
97    public function getType() {
98        return 'sense';
99    }
100
101    /**
102     * @param SenseId $id
103     */
104    public function setId( $id ) {
105        Assert::parameterType( SenseId::class, $id, '$id' );
106
107        if ( !( $this->id instanceof NullSenseId || $this->id instanceof DummySenseId ) ) {
108            throw new LogicException( 'Cannot override a real SenseId' );
109        }
110
111        $this->id = $id;
112    }
113
114    /**
115     * @see EntityDocument::isEmpty
116     *
117     * @return bool
118     */
119    public function isEmpty() {
120        return $this->glossList->isEmpty()
121            && $this->statementList->isEmpty();
122    }
123
124    /**
125     * @see EntityDocument::equals
126     *
127     * @param mixed $target
128     *
129     * @return bool True if the sense's contents are equal. Does not consider the ID.
130     */
131    public function equals( $target ) {
132        if ( $this === $target ) {
133            return true;
134        }
135
136        return $target instanceof self
137            && $this->glossList->equals( $target->glossList )
138            && $this->statementList->equals( $target->statementList );
139    }
140
141    /**
142     * Clears the glosses and statements of a sense.
143     * Note that this leaves the sense in an insufficiently initialized state.
144     */
145    public function clear() {
146        $this->glossList = new TermList();
147        $this->statementList = new StatementList();
148    }
149
150}