Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
24 / 24 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
LexemeMerger | |
100.00% |
24 / 24 |
|
100.00% |
3 / 3 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
merge | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
validate | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\Domain\Merge; |
4 | |
5 | use Exception; |
6 | use Wikibase\Lexeme\Domain\Merge\Exceptions\ConflictingLemmaValueException; |
7 | use Wikibase\Lexeme\Domain\Merge\Exceptions\CrossReferencingException; |
8 | use Wikibase\Lexeme\Domain\Merge\Exceptions\DifferentLanguagesException; |
9 | use Wikibase\Lexeme\Domain\Merge\Exceptions\DifferentLexicalCategoriesException; |
10 | use Wikibase\Lexeme\Domain\Merge\Exceptions\MergingException; |
11 | use Wikibase\Lexeme\Domain\Merge\Exceptions\ModificationFailedException; |
12 | use Wikibase\Lexeme\Domain\Merge\Exceptions\ReferenceSameLexemeException; |
13 | use Wikibase\Lexeme\Domain\Merge\Validator\NoConflictingTermListValues; |
14 | use Wikibase\Lexeme\Domain\Model\Lexeme; |
15 | use Wikibase\Repo\Merge\StatementsMerger; |
16 | |
17 | /** |
18 | * @license GPL-2.0-or-later |
19 | */ |
20 | class LexemeMerger { |
21 | |
22 | /** |
23 | * @var StatementsMerger |
24 | */ |
25 | private $statementsMerger; |
26 | |
27 | /** |
28 | * @var LexemeFormsMerger |
29 | */ |
30 | private $formsMerger; |
31 | |
32 | /** |
33 | * @var LexemeSensesMerger |
34 | */ |
35 | private $sensesMerger; |
36 | |
37 | /** |
38 | * @var NoCrossReferencingLexemeStatements |
39 | */ |
40 | private $noCrossReferencingLexemeStatementsValidator; |
41 | |
42 | public function __construct( |
43 | StatementsMerger $statementsMerger, |
44 | LexemeFormsMerger $formsMerger, |
45 | LexemeSensesMerger $sensesMerger, |
46 | NoCrossReferencingLexemeStatements $noCrossReferencingLexemeStatementsValidator |
47 | ) { |
48 | $this->statementsMerger = $statementsMerger; |
49 | $this->formsMerger = $formsMerger; |
50 | $this->sensesMerger = $sensesMerger; |
51 | $this->noCrossReferencingLexemeStatementsValidator = $noCrossReferencingLexemeStatementsValidator; |
52 | } |
53 | |
54 | /** |
55 | * @param Lexeme $source |
56 | * @param Lexeme $target Will be modified by reference |
57 | */ |
58 | public function merge( Lexeme $source, Lexeme $target ) { |
59 | $this->validate( $source, $target ); |
60 | |
61 | $target->getLemmas()->addAll( $source->getLemmas() ); |
62 | |
63 | try { |
64 | $this->formsMerger->merge( $source, $target ); |
65 | $this->sensesMerger->merge( $source, $target ); |
66 | $this->statementsMerger->merge( $source, $target ); |
67 | } catch ( MergingException $e ) { |
68 | throw $e; |
69 | } catch ( Exception $e ) { |
70 | throw new ModificationFailedException( '', 0, $e ); |
71 | } |
72 | } |
73 | |
74 | private function validate( Lexeme $source, Lexeme $target ) { |
75 | if ( $source->getId()->equals( $target->getId() ) ) { |
76 | throw new ReferenceSameLexemeException(); |
77 | } |
78 | |
79 | if ( !$source->getLanguage()->equals( $target->getLanguage() ) ) { |
80 | throw new DifferentLanguagesException(); |
81 | } |
82 | |
83 | if ( !$source->getLexicalCategory()->equals( $target->getLexicalCategory() ) ) { |
84 | throw new DifferentLexicalCategoriesException(); |
85 | } |
86 | |
87 | $conflictingTermListValues = new NoConflictingTermListValues(); |
88 | if ( !$conflictingTermListValues->validate( $source->getLemmas(), $target->getLemmas() ) ) { |
89 | throw new ConflictingLemmaValueException(); |
90 | } |
91 | |
92 | if ( !$this->noCrossReferencingLexemeStatementsValidator->validate( $source, $target ) ) { |
93 | throw new CrossReferencingException(); |
94 | } |
95 | } |
96 | |
97 | } |