EntityTypeAwareDiffOpFactory::newFromArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff;
4
5
use Diff\DiffOp\DiffOp;
6
use Diff\DiffOpFactory;
7
use InvalidArgumentException;
8
9
/**
10
 * Class for changes that can be represented as a Diff.
11
 *
12
 * @since 1.2
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Daniel Kinzler
16
 */
17
class EntityTypeAwareDiffOpFactory extends DiffOpFactory {
18
19
	/**
20
	 * @param array $diffOp
21
	 *
22
	 * @return DiffOp
23
	 * @throws InvalidArgumentException
24
	 */
25 3
	public function newFromArray( array $diffOp ) {
26 3
		$this->assertHasKey( 'type', $diffOp );
27
28
		// see EntityDiff::getType() and ItemDiff::getType()
29 2
		if ( preg_match( '!^diff/(.*)$!', $diffOp['type'], $matches ) ) {
30 1
			$itemType = $matches[1];
31 1
			$this->assertHasKey( 'operations', $diffOp );
32
33 1
			$operations = $this->createOperations( $diffOp['operations'] );
34 1
			$diff = EntityDiff::newForType( $itemType, $operations );
35
36 1
			return $diff;
37
		}
38
39 1
		return parent::newFromArray( $diffOp );
40
	}
41
42
	/**
43
	 * Converts a list of diff operations represented by arrays into a list of
44
	 * DiffOp objects.
45
	 *
46
	 * @param array $data the input data
47
	 * @return DiffOp[] The diff ops
48
	 */
49 1
	private function createOperations( array $data ) {
50 1
		$operations = [];
51
52 1
		foreach ( $data as $key => $operation ) {
53
			$operations[$key] = $this->newFromArray( $operation );
54 1
		}
55
56 1
		return $operations;
57
	}
58
59
}
60