Completed
Push — master ( f77362...00c6d5 )
by
unknown
06:40
created

ApiV2ContextCursor::getStatementArray()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 15
nc 12
nop 1
1
<?php
2
3
namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Context;
4
5
/**
6
 * Abstract superclass of all context cursors for the second version of the API output format.
7
 * (This is currently the only supported format.)
8
 *
9
 * This output format is modeled after the Wikibase entity JSON format,
10
 * where an object with the members 'hash' and a list of 'reports' can appear
11
 * wherever the entity JSON format contains snaks.
12
 *
13
 * That is, the container is keyed by entity ID and contains objects with a 'claims' member,
14
 * which holds an object that is keyed by property ID and contains lists of statement objects,
15
 * which have an 'id' member, a 'mainsnak' snak,
16
 * 'qualifiers' keyed by property ID holding a list of snaks,
17
 * and a list of 'references' each having a 'hash' and 'snaks'
18
 * which are keyed by property ID and then hold a list of snaks.
19
 *
20
 * @license GPL-2.0-or-later
21
 */
22
abstract class ApiV2ContextCursor implements ContextCursor {
23
24
	/**
25
	 * Returns the 'claims' subcontainer.
26
	 *
27
	 * @param array[] &$container
28
	 * @return array
29
	 */
30
	protected function &getClaimsArray( array &$container ) {
31
		$entityId = $this->getEntityId();
32
33
		if ( !array_key_exists( $entityId, $container ) ) {
34
			$container[$entityId] = [];
35
		}
36
		$entityContainer = &$container[$entityId];
37
38
		if ( !array_key_exists( 'claims', $entityContainer ) ) {
39
			$entityContainer['claims'] = [];
40
		}
41
		$claimsArray = &$entityContainer['claims'];
42
43
		return $claimsArray;
44
	}
45
46
	/**
47
	 * Returns the statement subcontainer.
48
	 *
49
	 * @param array[] &$container
50
	 * @return array
51
	 */
52
	protected function &getStatementArray( array &$container ) {
53
		$statementPropertyId = $this->getStatementPropertyId();
54
		$statementGuid = $this->getStatementGuid();
55
56
		$claimsContainer = &$this->getClaimsArray( $container );
57
58
		if ( !array_key_exists( $statementPropertyId, $claimsContainer ) ) {
59
			$claimsContainer[$statementPropertyId] = [];
60
		}
61
		$propertyContainer = &$claimsContainer[$statementPropertyId];
62
63
		foreach ( $propertyContainer as &$statement ) {
64
			if ( $statement['id'] === $statementGuid ) {
65
				$statementArray = &$statement;
66
				break;
67
			}
68
		}
69
		if ( !isset( $statementArray ) ) {
70
			$statementArray = [ 'id' => $statementGuid ];
71
			$propertyContainer[] = &$statementArray;
72
		}
73
74
		return $statementArray;
75
	}
76
77
	/**
78
	 * This method returns the array with the 'hash' and 'reports' member.
79
	 * It should locate the array in $container or,
80
	 * if the array doesn’t exist yet, create it and emplace it there.
81
	 *
82
	 * @param array[] &$container
83
	 * @return array
84
	 */
85
	abstract protected function &getMainArray( array &$container );
86
87
	/**
88
	 * @param array|null $result
89
	 * @param array[] &$container
90
	 */
91
	public function storeCheckResultInArray( array $result = null, array &$container ) {
92
		$mainArray = &$this->getMainArray( $container );
93
		if ( !array_key_exists( 'results', $mainArray ) ) {
94
			$mainArray['results'] = [];
95
		}
96
97
		if ( $result !== null ) {
98
			$mainArray['results'][] = $result;
99
		}
100
	}
101
102
}
103