Completed
Push — master ( 3abc67...80e892 )
by mw
207:38 queued 172:37
created

includes/dataitems/SMW_DI_Container.php (1 issue)

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
 * @ingroup SMWDataItems
4
 */
5
6
use SMW\Exception\DataItemException;
7
use SMW\DIProperty;
8
use SMWDIBlob as DIBlob;
9
10
/**
11
 * This class implements container data items that can store SMWSemanticData
12
 * objects. Containers are not dataitems in the proper sense: they do not
13
 * represent a single, opaque value that can be assigned to a property. Rather,
14
 * a container represents a "subobject" with a number of property-value
15
 * assignments. When a container is stored, these individual data assignments
16
 * are stored -- the data managed by SMW never contains any "container", just
17
 * individual property assignments for the subobject. Likewise, when a container
18
 * is used in search, it is interpreted as a patterns of possible property
19
 * assignments, and this pattern is searched for.
20
 *
21
 * The data encapsulated in a container data item is essentially an
22
 * SMWSemanticData object of class SMWContainerSemanticData. This class allows
23
 * the subject to be kept anonymous if not known (if no context page is
24
 * available for finding a suitable subobject name). See the repsective
25
 * documentation for details.
26
 *
27
 * Being a mere placeholder/template for other data, an SMWDIContainer is not
28
 * immutable as the other basic data items. New property-value pairs can always
29
 * be added to the internal SMWContainerSemanticData.
30
 *
31
 * @since 1.6
32
 *
33
 * @author Markus Krötzsch
34
 * @ingroup SMWDataItems
35
 */
36
class SMWDIContainer extends SMWDataItem {
37
38
	/**
39
	 * Internal value.
40
	 *
41
	 * @var SMWSemanticData
42
	 */
43
	protected $m_semanticData;
44
45
	/**
46
	 * Constructor. The given SMWContainerSemanticData object will be owned
47
	 * by the constructed object afterwards, and in particular will not
48
	 * allow further changes.
49
	 *
50
	 * @param $semanticData SMWContainerSemanticData
51
	 */
52 164
	public function __construct( SMWContainerSemanticData $semanticData ) {
53 164
		$this->m_semanticData = $semanticData;
0 ignored issues
show
Documentation Bug introduced by
It seems like $semanticData of type object<SMWContainerSemanticData> is incompatible with the declared type object<SMWSemanticData> of property $m_semanticData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54 164
	}
55
56 5
	public function getDIType() {
57 5
		return SMWDataItem::TYPE_CONTAINER;
58
	}
59
60 164
	public function getSemanticData() {
61 164
		return $this->m_semanticData;
62
	}
63
64 6
	public function getSortKey() {
65 6
		return '';
66
	}
67
68
	/**
69
	 * @since 2.5
70
	 *
71
	 * @param string $sortKey
72
	 */
73 34
	public function setSortKey( $sortKey ) {
74 34
		$this->m_semanticData->addPropertyObjectValue(
75 34
			new DIProperty( '_SKEY' ),
76 34
			new DIBlob( $this->m_semanticData->getSubject()->getSortKey() . '#' . $sortKey )
77
		);
78 34
	}
79
80
	public function getSerialization() {
81
		return serialize( $this->m_semanticData );
82
	}
83
84
	/**
85
	 * Get a hash string for this data item.
86
	 *
87
	 * @return string
88
	 */
89 1
	public function getHash() {
90
91 1
		$hash = $this->getValueHash( $this->m_semanticData );
92 1
		sort( $hash );
93
94 1
		return md5( implode( '#', $hash ) );
95
96
		// We want a value hash, not an entity hash!!
97
		// return $this->m_semanticData->getHash();
98
	}
99
100 1
	private function getValueHash( $semanticData ) {
101
102 1
		$hash = array();
103
104 1
		foreach ( $semanticData->getProperties() as $property ) {
105 1
			$hash[] = $property->getKey();
106
107 1
			foreach ( $semanticData->getPropertyValues( $property ) as $di ) {
108 1
				$hash[] = $di->getHash();
109
			}
110
		}
111
112 1
		foreach ( $semanticData->getSubSemanticData() as $data ) {
113
			$hash[] = $this->getValueHash( $data );
114
		}
115
116 1
		return $hash;
117
	}
118
119
	/**
120
	 * Create a data item from the provided serialization string and type
121
	 * ID.
122
	 *
123
	 * @return SMWDIContainer
124
	 */
125
	public static function doUnserialize( $serialization ) {
126
		/// TODO May issue an E_NOTICE when problems occur; catch this
127
		$data = unserialize( $serialization );
128
		if ( !( $data instanceof SMWContainerSemanticData ) ) {
129
			throw new DataItemException( "Could not unserialize SMWDIContainer from the given string." );
130
		}
131
		return new SMWDIContainer( $data );
132
	}
133
134
	public function equals( SMWDataItem $di ) {
135
		if ( $di->getDIType() !== SMWDataItem::TYPE_CONTAINER ) {
136
			return false;
137
		}
138
139
		return $di->getSerialization() === $this->getSerialization();
140
	}
141
}
142