SWLPropertyChanges::getProperties()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * A collection of semantic properties and changes changes made to them.
5
 * This class is based on SMWSemanticData and can be seen as a simplified
6
 * version with SWLPropertyChange objects, each holding 2 SMWDataItem objects,
7
 * instead of SMWDataItem objects.
8
 * 
9
 * @since 0.1
10
 * 
11
 * @file SWL_PropertyChange.php
12
 * @ingroup SemanticWatchlist
13
 * 
14
 * @licence GNU GPL v3 or later
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class SWLPropertyChanges implements Iterator {
18
19
	private $pos = 0;
20
	private $currentRow = null;	
21
	
22
	/**
23
	 * Cache for the localized version of the namespace prefix "Property:".
24
	 *
25
	 * @var string
26
	 */
27
	static private $propertyPrefix = '';	
28
	
29
	/**
30
	 * Array mapping property keys (string) to arrays of SWLPropertyChange.
31
	 * 
32
	 * @var array of SWLPropertyChange
33
	 */
34
	private $changes = array();
35
	
36
	/**
37
	 * Array mapping property keys (string) to SMWDIProperty objects.
38
	 *
39
	 * @var array of SMWDIProperty
40
	 */
41
	private $properties = array();
42
	
43
	/**
44
	 * Indicates if there are changes in the list.
45
	 * 
46
	 * @var boolean
47
	 */
48
	private $hasChanges = false;
49
	
50
	/**
51
	 * Get the array of all properties that have changes.
52
	 *
53
	 * @return array of SMWDIProperty
54
	 */
55
	public function getProperties() {
56
		return $this->properties;
57
	}
58
	
59
	/**
60
	 * Returns if the list contains any changes.
61
	 * This info is cached, so the call is cheaper then doing a count.
62
	 * 
63
	 * @return boolean
64
	 */
65
	public function hasChanges() {
66
		return $this->hasChanges;
67
	}
68
	
69
	/**
70
	 * Get the array of all stored values for some property.
71
	 *
72
	 * @param $property SMWDIProperty
73
	 * 
74
	 * @return array of SWLPropertyChange
75
	 */
76
	public function getPropertyChanges( SMWDIProperty $property ) {
77
		if ( array_key_exists( $property->getKey(), $this->changes ) ) {
78
			return $this->changes[$property->getKey()];
79
		} else {
80
			return array();
81
		}  
82
	}
83
	
84
	/**
85
	 * Store a value for a property identified by its SMWDataItem object.
86
	 *
87
	 * @note There is no check whether the type of the given data item
88
	 * agrees with the type of the property. Since property types can
89
	 * change, all parts of SMW are prepared to handle mismatched data item
90
	 * types anyway.
91
	 *
92
	 * @param SMWDIProperty $property
93
	 * @param SWLPropertyChange $change
94
	 */
95
	public function addPropertyObjectChange( SMWDIProperty $property, SWLPropertyChange $change ) {
96
		if ( $property->isInverse() ) { // inverse properties cannot be used for annotation
97
			return;
98
		}
99
100
		if ( !array_key_exists( $property->getKey(), $this->changes ) ) {
101
			$this->changes[$property->getKey()] = array();
102
			$this->properties[$property->getKey()] = $property;
103
		}
104
105
		$this->changes[$property->getKey()][] = $change;
106
		
107
		$this->hasChanges = true;
108
	}
109
110
	/**
111
	 * Store a value for a given property identified by its text label
112
	 * (without namespace prefix).
113
	 *
114
	 * @param string $propertyName
115
	 * @param SWLPropertyChange $change
116
	 */
117
	public function addPropertyChange( $propertyName, SWLPropertyChange $change ) {
118
		$propertyKey = smwfNormalTitleDBKey( $propertyName );
119
120
		if ( array_key_exists( $propertyKey, $this->properties ) ) {
121
			$property = $this->properties[$propertyKey];
122
		} else {
123
			if ( self::$propertyPrefix == '' ) {
124
				global $wgContLang;
125
				self::$propertyPrefix = $wgContLang->getNsText( SMW_NS_PROPERTY ) . ':';
126
			} // explicitly use prefix to cope with things like [[Property:User:Stupid::somevalue]]
127
128
			$propertyDV = SMWPropertyValue::makeUserProperty( self::$propertyPrefix . $propertyName );
129
130
			if ( !$propertyDV->isValid() ) { // error, maybe illegal title text
131
				return;
132
			}
133
			
134
			$property = $propertyDV->getDataItem();
135
		}
136
137
		$this->addPropertyObjectChange( $property, $change );
138
	}
139
	
140
	/**
141
	 * Removes all changes for a certian property.
142
	 * 
143
	 * @param SMWDIProperty $property
144
	 */
145
	public function removeChangesForProperty( SMWDIProperty $property ) {
146
		if ( array_key_exists( $property->getKey(), $this->changes ) ) {
147
			unset( $this->changes[$property->getKey()] );
148
			unset( $this->properties[$property->getKey()] );
149
		}
150
	}
151
	
152
	function rewind() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
153
		$this->pos = 0;
154
		$this->currentRow = null;
155
	}
156
157
	function current() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
158
		if ( is_null( $this->currentRow ) ) {
159
			$this->next();
160
		}
161
		return $this->currentRow;
162
	}
163
164
	function key() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
165
		return $this->pos;
166
	}
167
168
	function next() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
169
		$this->pos++;
170
		$this->currentRow = array_key_exists( $this->pos, $this->changes ) ? $this->changes[$this->pos] : false;
171
		return $this->currentRow;
172
	}
173
174
	function valid() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
175
		return $this->current() !== false;
176
	}
177
	
178
}