Completed
Push — master ( d581f7...34ba15 )
by mw
02:38
created

ResponsePropertyList::newProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
ccs 7
cts 7
cp 1
rs 9.4286
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace SEQL\ByHttpRequest;
4
5
use SMW\DIProperty;
6
use SMW\DIWikiPage;
7
use RuntimeException;
8
9
/**
10
 * @license GNU GPL v2+
11
 * @since 1.0
12
 *
13
 * @author mwjames
14
 */
15
class ResponsePropertyList {
16
17
	/**
18
	 * @var string
19
	 */
20
	private $querySource = '';
21
22
	/**
23
	 * @var array
24
	 */
25
	private $propertyList = array();
26
27
	/**
28
	 * @var array
29
	 */
30
	private $internalLabelToKeyMap = array();
31
32
	/**
33
	 * @since 1.0
34
	 *
35
	 * @param string $querySource
36
	 */
37 12
	public function __construct( $querySource ) {
38 12
		$this->querySource = $querySource;
39 12
	}
40
41
	/**
42
	 * @since 1.0
43
	 *
44
	 * @return DIProperty[]
45
	 */
46 9
	public function getPropertyList() {
47 9
		return $this->propertyList;
48
	}
49
50
	/**
51
	 * @since 1.0
52
	 *
53
	 * @param string $key
54
	 *
55
	 * @return string
56
	 */
57 10
	public function findPropertyKey( $key ) {
58
59 10
		if ( isset( $this->internalLabelToKeyMap[$key] ) ) {
60 3
			return $this->internalLabelToKeyMap[$key];
61
		}
62
63 9
		return $key;
64
	}
65
66
	/**
67
	 * @since 1.0
68
	 *
69
	 * @param string $key
70
	 *
71
	 * @return boolean
72
	 */
73 8
	public function hasProperty( $key ) {
74 8
		return isset( $this->propertyList[$this->findPropertyKey( $key )] );
75
	}
76
77
	/**
78
	 * @since 1.0
79
	 *
80
	 * @param string $key
81
	 *
82
	 * @return DIProperty|null
83
	 */
84 10
	public function getProperty( $key ) {
85
86 10
		$key = $this->findPropertyKey( $key );
87
88 10
		if ( isset( $this->propertyList[$key] ) ) {
89 10
			return $this->propertyList[$key];
90
		}
91
92
		return null;
93
	}
94
95
	/**
96
	 * @since 1.0
97
	 *
98
	 * @param array $value
99
	 */
100 11
	public function addToPropertyList( array $value ) {
101
102 11
		if ( $value['label'] === '' ) {
103 6
			return;
104
		}
105
106 11
		if ( $value['mode'] == 0 ) {
107 2
			$property = new DIProperty( '_INST' );
108 2
			$this->internalLabelToKeyMap[$value['label']] = $property->getKey();
109 2
		} else {
110 9
			$property = $this->newProperty( $value );
111
		}
112
113 10
		if ( isset( $value['redi'] ) ) {
114 2
			$this->internalLabelToKeyMap[$value['redi']] = $property->getKey();
115 2
		}
116
117 10
		$property->setInterwiki( $this->querySource );
118 10
		$this->propertyList[$property->getKey()] = $property;
119 10
	}
120
121 9
	private function newProperty( $value ) {
122
123 9
		$property = DIProperty::newFromUserLabel( $value['label'] );
124
125 9
		if ( $property->isUserDefined() ) {
126 7
			return $property->setPropertyTypeId( $value['typeid'] );
127
		}
128
129 3
		if ( $property->findPropertyTypeID() === $value['typeid'] ) {
130 2
			return $property;
131
		}
132
133
		// Something like |Has foo=Text where `Text` is mapped to a DataType
134
		// cannot be redeclared when the type of `Has foo` (as `_wpg`) doesn't
135
		// correspond to the predefined property type
136 1
		throw new RuntimeException( 'Cannot redeclare type "' .  $value['typeid'] . '" for "' . $value['label'] . '" (as predefined property/type)' );
137
	}
138
139
}
140