Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.14% |
34 / 35 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
ChangeOpGrammaticalFeatures | |
97.14% |
34 / 35 |
|
80.00% |
4 / 5 |
10 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
validate | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
apply | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getActions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
updateSummary | |
96.30% |
26 / 27 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\DataAccess\ChangeOp; |
4 | |
5 | use ValueValidators\Result; |
6 | use Wikibase\DataModel\Entity\EntityDocument; |
7 | use Wikibase\DataModel\Entity\ItemId; |
8 | use Wikibase\Lexeme\Domain\Model\Form; |
9 | use Wikibase\Lib\Summary; |
10 | use Wikibase\Repo\ChangeOp\ChangeOp; |
11 | use Wikibase\Repo\ChangeOp\DummyChangeOpResult; |
12 | use Wikibase\Repo\Store\EntityPermissionChecker; |
13 | use Wikimedia\Assert\Assert; |
14 | |
15 | /** |
16 | * @license GPL-2.0-or-later |
17 | */ |
18 | class ChangeOpGrammaticalFeatures implements ChangeOp { |
19 | |
20 | private const SUMMARY_ACTION_ADD = 'add-form-grammatical-features'; |
21 | private const SUMMARY_ACTION_REMOVE = 'remove-form-grammatical-features'; |
22 | private const SUMMARY_ACTION_UPDATE = 'update-form-grammatical-features'; |
23 | |
24 | /** |
25 | * @var ItemId[] |
26 | */ |
27 | private $grammaticalFeatures; |
28 | |
29 | public function __construct( array $grammaticalFeatures ) { |
30 | $this->grammaticalFeatures = $grammaticalFeatures; |
31 | } |
32 | |
33 | public function validate( EntityDocument $entity ) { |
34 | Assert::parameterType( Form::class, $entity, '$entity' ); |
35 | |
36 | return Result::newSuccess(); |
37 | } |
38 | |
39 | public function apply( EntityDocument $entity, ?Summary $summary = null ) { |
40 | Assert::parameterType( Form::class, $entity, '$entity' ); |
41 | '@phan-var Form $entity'; |
42 | |
43 | $this->updateSummary( $entity, $summary ); |
44 | |
45 | /** @var Form $entity */ |
46 | $entity->setGrammaticalFeatures( $this->grammaticalFeatures ); |
47 | |
48 | return new DummyChangeOpResult(); |
49 | } |
50 | |
51 | public function getActions() { |
52 | return [ EntityPermissionChecker::ACTION_EDIT ]; |
53 | } |
54 | |
55 | private function updateSummary( Form $form, ?Summary $summary = null ) { |
56 | if ( $summary === null ) { |
57 | return; |
58 | } |
59 | |
60 | $existingFeatures = $form->getGrammaticalFeatures(); |
61 | |
62 | $addedFeatures = array_diff( $this->grammaticalFeatures, $existingFeatures ); |
63 | $removedFeatures = array_diff( $existingFeatures, $this->grammaticalFeatures ); |
64 | |
65 | $formId = $form->getId(); |
66 | |
67 | if ( $addedFeatures && $removedFeatures ) { |
68 | $summary->setAction( self::SUMMARY_ACTION_UPDATE ); |
69 | $summary->setLanguage( null ); |
70 | $summary->addAutoCommentArgs( [ |
71 | $formId->getSerialization(), // TODO: use FormId not string? |
72 | ] ); |
73 | return; |
74 | } |
75 | |
76 | if ( $addedFeatures ) { |
77 | $summary->setAction( self::SUMMARY_ACTION_ADD ); |
78 | $summary->setLanguage( null ); |
79 | $summary->addAutoCommentArgs( [ |
80 | $formId->getSerialization(), // TODO: use FormId not string? |
81 | ] ); |
82 | $summary->addAutoSummaryArgs( $addedFeatures ); |
83 | } |
84 | |
85 | if ( $removedFeatures ) { |
86 | $summary->setAction( self::SUMMARY_ACTION_REMOVE ); |
87 | $summary->setLanguage( null ); |
88 | $summary->addAutoCommentArgs( [ |
89 | $formId->getSerialization(), // TODO: use FormId not string? |
90 | ] ); |
91 | $summary->addAutoSummaryArgs( $removedFeatures ); |
92 | } |
93 | } |
94 | |
95 | } |