Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
19 / 19 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
ChangeOpLemmaEdit | |
100.00% |
19 / 19 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
validate | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
apply | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\DataAccess\ChangeOp; |
4 | |
5 | use InvalidArgumentException; |
6 | use ValueValidators\Result; |
7 | use Wikibase\DataModel\Entity\EntityDocument; |
8 | use Wikibase\Lexeme\DataAccess\ChangeOp\Validation\LemmaTermValidator; |
9 | use Wikibase\Lexeme\Domain\Model\Lexeme; |
10 | use Wikibase\Lib\Summary; |
11 | use Wikibase\Repo\ChangeOp\ChangeOpBase; |
12 | use Wikibase\Repo\ChangeOp\DummyChangeOpResult; |
13 | use Wikimedia\Assert\Assert; |
14 | |
15 | /** |
16 | * @license GPL-2.0-or-later |
17 | */ |
18 | class ChangeOpLemmaEdit extends ChangeOpBase { |
19 | |
20 | private const SUMMARY_ACTION_ADD = 'add'; |
21 | private const SUMMARY_ACTION_UPDATE = 'set'; |
22 | |
23 | /** |
24 | * @var string |
25 | */ |
26 | private $language; |
27 | |
28 | /** |
29 | * @var string |
30 | */ |
31 | private $lemma; |
32 | |
33 | /** |
34 | * @var LemmaTermValidator |
35 | */ |
36 | private $lemmaTermValidator; |
37 | |
38 | /** |
39 | * @param string $language |
40 | * @param string $lemma |
41 | * @param LemmaTermValidator $lemmaTermValidator |
42 | * |
43 | * @throws InvalidArgumentException |
44 | */ |
45 | public function __construct( $language, $lemma, LemmaTermValidator $lemmaTermValidator ) { |
46 | Assert::parameterType( 'string', $language, '$language' ); |
47 | Assert::parameterType( 'string', $lemma, '$lemma' ); |
48 | |
49 | $this->language = $language; |
50 | $this->lemma = $lemma; |
51 | $this->lemmaTermValidator = $lemmaTermValidator; |
52 | } |
53 | |
54 | /** |
55 | * @param EntityDocument $entity |
56 | * |
57 | * @return Result |
58 | * @throws InvalidArgumentException |
59 | */ |
60 | public function validate( EntityDocument $entity ) { |
61 | Assert::parameterType( Lexeme::class, $entity, '$entity' ); |
62 | |
63 | return $this->lemmaTermValidator->validate( $this->lemma ); |
64 | } |
65 | |
66 | /** |
67 | * @param EntityDocument $entity |
68 | * @param Summary|null $summary |
69 | * |
70 | * @throws InvalidArgumentException |
71 | */ |
72 | public function apply( EntityDocument $entity, ?Summary $summary = null ) { |
73 | Assert::parameterType( Lexeme::class, $entity, '$entity' ); |
74 | '@phan-var Lexeme $entity'; |
75 | |
76 | /** @var Lexeme $entity */ |
77 | $lemmas = $entity->getLemmas(); |
78 | |
79 | $this->updateSummary( |
80 | $summary, |
81 | $lemmas->hasTermForLanguage( $this->language ) ? |
82 | self::SUMMARY_ACTION_UPDATE : |
83 | self::SUMMARY_ACTION_ADD, |
84 | $this->language, |
85 | $this->lemma |
86 | ); |
87 | $lemmas->setTextForLanguage( $this->language, $this->lemma ); |
88 | |
89 | return new DummyChangeOpResult(); |
90 | } |
91 | |
92 | } |