Completed
Push — master ( f552f8...7247aa )
by
unknown
03:16
created

TermListPatcher::patchTerm()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 4.909
cc 9
eloc 19
nc 7
nop 3
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff\Internal;
4
5
use Diff\DiffOp\AtomicDiffOp;
6
use Diff\DiffOp\Diff\Diff;
7
use Diff\DiffOp\DiffOpAdd;
8
use Diff\DiffOp\DiffOpChange;
9
use Diff\DiffOp\DiffOpRemove;
10
use Diff\Patcher\PatcherException;
11
use Wikibase\DataModel\Term\TermList;
12
13
/**
14
 * Package private.
15
 *
16
 * @since 3.6
17
 *
18
 * @license GPL-2.0+
19
 * @author Jeroen De Dauw < [email protected] >
20
 * @author Thiemo Mättig
21
 */
22
class TermListPatcher {
23
24
	/**
25
	 * @since 3.6
26
	 *
27
	 * @param TermList $terms
28
	 * @param Diff $patch
29
	 *
30
	 * @throws PatcherException
31
	 */
32
	public function patchTermList( TermList $terms, Diff $patch ) {
33
		foreach ( $patch as $lang => $diffOp ) {
34
			$this->patchTerm( $terms, $lang, $diffOp );
35
		}
36
	}
37
38
	/**
39
	 * @see MapPatcher
40
	 *
41
	 * @param TermList $terms
42
	 * @param string $lang
43
	 * @param AtomicDiffOp $diffOp
44
	 *
45
	 * @throws PatcherException
46
	 */
47
	private function patchTerm( TermList $terms, $lang, AtomicDiffOp $diffOp ) {
48
		$hasLang = $terms->hasTermForLanguage( $lang );
49
50
		switch ( true ) {
51
			case $diffOp instanceof DiffOpAdd:
52
				/** @var $diffOp DiffOpAdd */
53
				if ( !$hasLang ) {
54
					$terms->setTextForLanguage( $lang, $diffOp->getNewValue() );
55
				}
56
				break;
57
58
			case $diffOp instanceof DiffOpChange:
59
				/** @var $diffOp DiffOpChange */
60
				if ( $hasLang
61
					&& $terms->getByLanguage( $lang )->getText() === $diffOp->getOldValue()
62
				) {
63
					$terms->setTextForLanguage( $lang, $diffOp->getNewValue() );
64
				}
65
				break;
66
67
			case $diffOp instanceof DiffOpRemove:
68
				/** @var $diffOp DiffOpRemove */
69
				if ( $hasLang
70
					&& $terms->getByLanguage( $lang )->getText() === $diffOp->getOldValue()
71
				) {
72
					$terms->removeByLanguage( $lang );
73
				}
74
				break;
75
76
			default:
77
				throw new PatcherException( 'Invalid terms diff' );
78
		}
79
	}
80
81
}
82