Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.95% covered (warning)
80.95%
17 / 21
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
FormPatcher
80.95% covered (warning)
80.95%
17 / 21
50.00% covered (danger)
50.00%
2 / 4
5.17
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
1
 canPatchEntityType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 patchEntity
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 patch
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\Lexeme\Domain\Diff;
4
5use Diff\Patcher\ListPatcher;
6use InvalidArgumentException;
7use Wikibase\DataModel\Entity\EntityDocument;
8use Wikibase\DataModel\Services\Diff\EntityDiff;
9use Wikibase\DataModel\Services\Diff\EntityPatcherStrategy;
10use Wikibase\DataModel\Services\Diff\StatementListPatcher;
11use Wikibase\DataModel\Services\Diff\TermListPatcher;
12use Wikibase\Lexeme\Domain\Model\Form;
13
14/**
15 * @license GPL-2.0-or-later
16 */
17class FormPatcher implements EntityPatcherStrategy {
18
19    private $termListPatcher;
20
21    private $statementListPatcher;
22
23    private $listPatcher;
24
25    public function __construct() {
26        $this->termListPatcher = new TermListPatcher();
27        $this->statementListPatcher = new StatementListPatcher();
28        $this->listPatcher = new ListPatcher();
29    }
30
31    /**
32     * @param string $entityType
33     *
34     * @return bool
35     */
36    public function canPatchEntityType( $entityType ) {
37        return $entityType === Form::ENTITY_TYPE;
38    }
39
40    /**
41     * @param EntityDocument $entity
42     * @param EntityDiff $patch
43     *
44     * @throws InvalidArgumentException
45     */
46    public function patchEntity( EntityDocument $entity, EntityDiff $patch ) {
47        if ( !( $entity instanceof Form ) ) {
48            throw new InvalidArgumentException( 'Can only patch Forms' );
49        }
50
51        // @phan-suppress-next-line PhanTypeMismatchArgumentSuperType
52        return $this->patch( $entity, $patch );
53    }
54
55    /**
56     * @deprecated use self::patchEntity instead
57     *
58     * @param Form $form
59     * @param ChangeFormDiffOp $diff
60     */
61    public function patch( Form $form, ChangeFormDiffOp $diff ) {
62        $this->termListPatcher->patchTermList(
63            $form->getRepresentations(),
64            $diff->getRepresentationDiff()
65        );
66        $grammaticalFeatures = $form->getGrammaticalFeatures();
67        $patchedGrammaticalFeatures = $this->listPatcher->patch(
68            $grammaticalFeatures,
69            $diff->getGrammaticalFeaturesDiff()
70        );
71        $form->setGrammaticalFeatures( $patchedGrammaticalFeatures );
72
73        $this->statementListPatcher->patchStatementList(
74            $form->getStatements(),
75            $diff->getStatementsDiff()
76        );
77    }
78
79}