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
ChangeOpFormClone
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%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 apply
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 validate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getActions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\Lexeme\DataAccess\ChangeOp;
4
5use ValueValidators\Result;
6use Wikibase\DataModel\Entity\EntityDocument;
7use Wikibase\DataModel\Services\Statement\GuidGenerator;
8use Wikibase\Lexeme\Domain\Model\Form;
9use Wikibase\Lib\Summary;
10use Wikibase\Repo\ChangeOp\ChangeOp;
11use Wikibase\Repo\ChangeOp\DummyChangeOpResult;
12use Wikibase\Repo\Store\EntityPermissionChecker;
13use Wikimedia\Assert\Assert;
14
15/**
16 * Copy the properties of the existing Form ($sourceForm) into the passed BlankForm ($entity)
17 *
18 * @license GPL-2.0-or-later
19 */
20class ChangeOpFormClone implements ChangeOp {
21
22    /**
23     * @var Form
24     */
25    private $sourceForm;
26
27    /**
28     * @var GuidGenerator
29     */
30    private $guidGenerator;
31
32    /**
33     * @param Form $sourceForm
34     */
35    public function __construct( Form $sourceForm, GuidGenerator $guidGenerator ) {
36        $this->sourceForm = $sourceForm->copy();
37        $this->guidGenerator = $guidGenerator;
38    }
39
40    public function apply( EntityDocument $entity, Summary $summary = null ) {
41        Assert::parameterType( Form::class, $entity, '$entity' );
42        '@phan-var Form $entity';
43        /** @var Form $entity */
44
45        $entity->setRepresentations( $this->sourceForm->getRepresentations() );
46        $entity->setGrammaticalFeatures( $this->sourceForm->getGrammaticalFeatures() );
47
48        foreach ( $this->sourceForm->getStatements() as $index => $statement ) {
49            // update statements to have a suitable GUID based on the form id
50            // fixme Maybe this can find a new home in a more central place, e.g. StatementList
51            $statement->setGuid( $this->guidGenerator->newGuid( $entity->getId() ) );
52
53            $entity->getStatements()->addStatement( $statement, $index );
54        }
55
56        // TODO summary; This is currently only used as part of merging to copy forms
57        // from the source lexemes onto the target.
58        // Generating a summary here is not necessary as of now.
59
60        return new DummyChangeOpResult();
61    }
62
63    public function validate( EntityDocument $entity ): Result {
64        Assert::parameterType( Form::class, $entity, '$entity' );
65
66        return Result::newSuccess();
67    }
68
69    public function getActions() {
70        return [ EntityPermissionChecker::ACTION_EDIT ];
71    }
72
73}