summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataValues/AbstractMultiValue.php
blob: 007aedb7947b30c7c4357c0f9df20383ba7b9181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php

namespace SMW\DataValues;

use SMW\ApplicationFactory;
use SMW\DIProperty;
use SMWDataValue as DataValue;
use SMWPropertyListValue as PropertyListValue;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
abstract class AbstractMultiValue extends DataValue {

	/**
	 * @since 2.5
	 *
	 * @param string $userValue
	 *
	 * @return array
	 */
	abstract public function getValuesFromString( $userValue );

	/**
	 * @since 2.5
	 *
	 * @param DIProperty[] $properties
	 *
	 * @return DIProperty[]|null
	 */
	abstract public function setFieldProperties( array $properties );

	/**
	 * @since 2.5
	 *
	 * @return DIProperty[]|null
	 */
	abstract public function getProperties();

	/**
	 * Return the array (list) of properties that the individual entries of
	 * this datatype consist of.
	 *
	 * @since 2.5
	 *
	 * @return DIProperty[]|null
	 */
	abstract public function getPropertyDataItems();

	/**
	 * Create a list (array with numeric keys) containing the datavalue
	 * objects that this SMWRecordValue object holds. Values that are not
	 * present are set to null. Note that the first index in the array is
	 * 0, not 1.
	 *
	 * @since 2.5
	 *
	 * @return DataItem[]|null
	 */
	public function getDataItems() {

		if ( !$this->isValid() ) {
			return [];
		}

		$dataItems = [];
		$index = 0;

		foreach ( $this->getPropertyDataItems() as $diProperty ) {
			$values = $this->getDataItem()->getSemanticData()->getPropertyValues( $diProperty );
			$dataItems[$index] = count( $values ) > 0 ? reset( $values ) : null;
			$index++;
		}

		return $dataItems;
	}

	/**
	 * @note called by SMWResultArray::loadContent for matching an index as denoted
	 * in |?Foo=Bar|+index=1 OR |?Foo=Bar|+index=Bar
	 *
	 * @see https://www.semantic-mediawiki.org/wiki/Help:Type_Record#Semantic_search
	 *
	 * @since 2.5
	 *
	 * @param string|integer $index
	 *
	 * @return DataItem[]|null
	 */
	public function getDataItemByIndex( $index ) {

		if ( is_numeric( $index ) ) {
			$pos = $index - 1;
			$dataItems = $this->getDataItems();
			return isset( $dataItems[$pos] ) ? $dataItems[$pos] : null;
		}

		if ( ( $property = $this->getPropertyDataItemByIndex( $index ) ) !== null ) {
			$values = $this->getDataItem()->getSemanticData()->getPropertyValues( $property );
			return reset( $values );
		}

		return null;
	}

	/**
	 * @note called by SMWResultArray::getNextDataValue to match an index
	 * that has been denoted using |?Foo=Bar|+index=1 OR |?Foo=Bar|+index=Bar
	 *
	 * @since 2.5
	 *
	 * @param string|integer $index
	 *
	 * @return DIProperty|null
	 */
	public function getPropertyDataItemByIndex( $index ) {

		$properties = $this->getPropertyDataItems();

		if ( is_numeric( $index ) ) {
			$pos = $index - 1;
			return isset( $properties[$pos] ) ? $properties[$pos] : null;
		}

		foreach ( $properties as $property ) {
			if ( $property->getLabel() === $index ) {
				return $property;
			}
		}

		return null;
	}

	/**
	 * Return the array (list) of properties that the individual entries of
	 * this datatype consist of.
	 *
	 * @since 2.5
	 *
	 * @param DIProperty|null $property
	 *
	 * @return DIProperty[]|[]
	 */
	protected function getFieldProperties( DIProperty $property = null ) {

		if ( $property === null || $property->getDiWikiPage() === null ) {
			return [];
		}

		$dataItem = ApplicationFactory::getInstance()->getPropertySpecificationLookup()->getFieldListBy( $property );

		if ( !$dataItem ) {
			return [];
		}

		$propertyListValue = new PropertyListValue( '__pls' );
		$propertyListValue->setDataItem( $dataItem );

		if ( !$propertyListValue->isValid() ) {
			return [];
		}

		return $propertyListValue->getPropertyDataItems();
	}

}