Completed
Push — master ( 2472c5...a814e8 )
by mw
35:03
created

includes/dataitems/SMW_DataItem.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * This group contains all parts of SMW that relate to the processing of dataitems
5
 * of various types.
6
 *
7
 * @defgroup SMWDataItems SMWDataItems
8
 * @ingroup SMW
9
 */
10
11
/**
12
 * Objects of this type represent all that is known about a certain piece of
13
 * data that could act as the value of some property. Data items only represent
14
 * the stored data, and are thus at the core of SMW's data model. Data items
15
 * are always immutable, i.e. they must not be changed after creation (and this
16
 * is mostly enforced by the API with some minor exceptions).
17
 *
18
 * The set of available data items is fixed and cannot be extended. These are
19
 * the kinds of information that SMW can process. Their concrete use and
20
 * handling might depend on the context in which they are used. In particular,
21
 * property values may be influences by settings made for their property. This
22
 * aspect, however, is not part of the data item API.
23
 *
24
 * @since 1.6
25
 *
26
 * @ingroup SMWDataItems
27
 */
28
abstract class SMWDataItem {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
30
	/// Data item ID that can be used to indicate that no data item class is appropriate
31
	const TYPE_NOTYPE    = 0;
32
	/// Data item ID for SMWDINumber
33
	const TYPE_NUMBER    = 1;
34
	/**
35
	 * Data item ID for SMWDIString.
36
	 * @deprecated Will vanish after SMW 1.9; use TYPE_BLOB instead.
37
	 */
38
	const TYPE_STRING    = 2;
39
	/// Data item ID for SMWDIBlob
40
	const TYPE_BLOB      = 2;
41
	///  Data item ID for SMWDIBoolean
42
	const TYPE_BOOLEAN   = 4;
43
	///  Data item ID for SMWDIUri
44
	const TYPE_URI       = 5;
45
	///  Data item ID for SMWDITimePoint
46
	const TYPE_TIME      = 6;
47
	///  Data item ID for SMWDIGeoCoord
48
	const TYPE_GEO       = 7;
49
	///  Data item ID for SMWDIContainer
50
	const TYPE_CONTAINER = 8;
51
	///  Data item ID for SMWDIWikiPage
52
	const TYPE_WIKIPAGE  = 9;
53
	///  Data item ID for SMWDIConcept
54
	const TYPE_CONCEPT   = 10;
55
	///  Data item ID for SMWDIProperty
56
	const TYPE_PROPERTY  = 11;
57
	///  Data item ID for SMWDIError
58
	const TYPE_ERROR     = 12;
59
60
	/**
61
	 * Convenience method that returns a constant that defines the concrete
62
	 * class that implements this data item. Used to switch when processing
63
	 * data items.
64
	 * @return integer that specifies the basic type of data item
65
	 */
66
	abstract public function getDIType();
67
68
	/**
69
	 * Return a value that can be used for sorting data of this type.
70
	 * If the data is of a numerical type, the sorting must be done in
71
	 * numerical order. If the data is a string, the data must be sorted
72
	 * alphabetically.
73
	 *
74
	 * @note Every data item returns a sort key, even if there is no
75
	 * natural linear order for the type. SMW must order listed data
76
	 * in some way in any case. If there is a natural order (e.g. for
77
	 * Booleans where false < true), then the sortkey must agree with
78
	 * this order (e.g. for Booleans where false maps to 0, and true
79
	 * maps to 1).
80
	 *
81
	 * @note Wiki pages are a special case in SMW. They are ordered by a
82
	 * sortkey that is assigned to them as a property value. When pages are
83
	 * sorted, this data should be used if possible.
84
	 *
85
	 * @return float|string
86
	 */
87
	abstract public function getSortKey();
88
89
	/**
90
	 * Method to compare two SMWDataItems
91
	 * This should result true only if they are of the same DI type
92
	 * and have the same internal value
93
	 *
94
	 * @since 1.8
95
	 *
96
	 * @param SMWDataItem $di
97
	 * @return boolean
98
	 */
99
	abstract public function equals( SMWDataItem $di );
100
101
	/**
102
	 * Create a data item that represents the sortkey, i.e. either an
103
	 * SMWDIBlob or an SMWDINumber. For efficiency, these subclasses
104
	 * overwrite this method to return themselves.
105
	 *
106
	 * @return SMWDataItem
107
	 */
108
	public function getSortKeyDataItem() {
109
		$sortKey = $this->getSortKey();
110
111
		if ( is_numeric( $sortKey ) ) {
112
			return new SMWDINumber( $sortKey );
113
		}
114
115
		return new SMWDIBlob( $sortKey );
116
	}
117
118
	/**
119
	 * Get a UTF-8 encoded string serialization of this data item.
120
	 * The serialisation should be concise and need not be pretty, but it
121
	 * must allow unserialization. Each subclass of SMWDataItem implements
122
	 * a static method doUnserialize() for this purpose.
123
	 * @return string
124
	 */
125
	abstract public function getSerialization();
126
127
	/**
128
	 * Get a hash string for this data item. Might be overwritten in
129
	 * subclasses to obtain shorter or more efficient hashes.
130
	 *
131
	 * @return string
132
	 */
133 284
	public function getHash() {
134 284
		return $this->getSerialization();
135
	}
136
137
	/**
138
	 * @since 2.1
139
	 *
140
	 * @return string
141
	 */
142 4
	public function __toString() {
143 4
		return $this->getSerialization();
144
	}
145
146
	/**
147
	 * Create a data item of the given dataitem ID based on the the
148
	 * provided serialization string and (optional) typeid.
149
	 *
150
	 * @param $diType integer dataitem ID
151
	 * @param $serialization string
152
	 *
153
	 * @return SMWDataItem
154
	 */
155 13
	public static function newFromSerialization( $diType, $serialization ) {
156 13
		$diClass = self::getDataItemClassNameForId( $diType );
157 13
		return call_user_func( array( $diClass, 'doUnserialize' ), $serialization );
158
	}
159
160
	/**
161
	 * Gets the class name of the data item that has the provided type id.
162
	 *
163
	 * @param integer $diType Element of the SMWDataItem::TYPE_ enum
164
	 *
165
	 * @throws InvalidArgumentException
166
	 *
167
	 * @return string
168
	 */
169 13
	public static function getDataItemClassNameForId( $diType ) {
170
		switch ( $diType ) {
171 13
			case self::TYPE_NUMBER:
172
				return 'SMWDINumber';
173 13
			case self::TYPE_BLOB:
174 5
				return 'SMWDIBlob';
175 11
			case self::TYPE_BOOLEAN:
176
				return 'SMWDIBoolean';
177 11
			case self::TYPE_URI:
178 1
				return 'SMWDIUri';
179 10
			case self::TYPE_TIME:
180
				return 'SMWDITime';
181 10
			case self::TYPE_GEO:
182
				return 'SMWDIGeoCoord';
183 10
			case self::TYPE_CONTAINER:
184
				return 'SMWDIContainer';
185 10
			case self::TYPE_WIKIPAGE:
186 10
				return 'SMWDIWikiPage';
187
			case self::TYPE_CONCEPT:
188
				return 'SMWDIConcept';
189
			case self::TYPE_PROPERTY:
190
				return 'SMWDIProperty';
191
			case self::TYPE_ERROR:
192
				return 'SMWDIError';
193
			case self::TYPE_NOTYPE: default:
0 ignored issues
show
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
194
				throw new InvalidArgumentException( "The value \"$diType\" is not a valid dataitem ID." );
195
		}
196
	}
197
198
}
199