summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Deserializers/DVDescriptionDeserializer/MonolingualTextValueDescriptionDeserializer.php
blob: 066953fd2cad90d870d2ea6e4a9c54d6cb8f7a31 (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
<?php

namespace SMW\Deserializers\DVDescriptionDeserializer;

use InvalidArgumentException;
use SMW\DataValueFactory;
use SMW\DataValues\MonolingualTextValue;
use SMW\Query\Language\Conjunction;
use SMW\Query\Language\SomeProperty;
use SMW\Query\Language\ThingDescription;
use SMW\Query\Language\ValueDescription;
use SMWDIBlob as DIBlob;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class MonolingualTextValueDescriptionDeserializer extends DescriptionDeserializer {

	/**
	 * @since 2.4
	 *
	 * {@inheritDoc}
	 */
	public function isDeserializerFor( $serialization ) {
		return $serialization instanceof MonolingualTextValue;
	}

	/**
	 * @since 2.4
	 *
	 * @param string $value
	 *
	 * @return Description
	 * @throws InvalidArgumentException
	 */
	public function deserialize( $value ) {

		if ( !is_string( $value ) ) {
			throw new InvalidArgumentException( 'Value needs to be a string' );
		}

		if ( $value === '' ) {
			$this->addError( wfMessage( 'smw_novalues' )->text() );
			return new ThingDescription();
		}

		$subdescriptions = [];
		list( $text, $languageCode ) = $this->dataValue->getValuesFromString( $value );

		foreach ( $this->dataValue->getPropertyDataItems() as $property ) {

			// If the DVFeature doesn't require a language code to be present then
			// allow to skip it as conjunctive condition when it is empty
			if (
				( $languageCode === '' ) &&
				( $property->getKey() === '_LCODE' ) &&
				( !$this->dataValue->isEnabledFeature( SMW_DV_MLTV_LCODE ) ) ) {
				continue;
			}

			$value = $property->getKey() === '_LCODE' ? $languageCode : $text;
			$comparator = SMW_CMP_EQ;

			$this->prepareValue( $value, $comparator );

			// Directly use the DI instead of going through the DVFactory to
			// avoid having ~zh-* being validated when building a DV
			// If one of the values is empty use, ? so queries can be arbitrary
			// in respect of the query condition
			$dataValue = DataValueFactory::getInstance()->newDataValueByItem(
				new DIBlob( $value === '' ? '?' : $value ),
				$property,
				false,
				$this->dataValue->getContextPage()
			);

			if ( !$dataValue->isValid() ) {
				$this->addError( $dataValue->getErrors() );
				continue;
			}

			$subdescriptions[] = $this->newSubdescription( $dataValue, $comparator );
		}

		return $this->getFinalDescriptionFor( $subdescriptions );
	}

	private function getFinalDescriptionFor( $subdescriptions ) {

		$count = count( $subdescriptions );

		if ( $count == 0 ) {
			return new ThingDescription();
		}

		if ( $count == 1 ) {
			return reset( $subdescriptions );
		}

		return new Conjunction( $subdescriptions );
	}

	private function newSubdescription( $dataValue, $comparator ) {

		$description = new ValueDescription(
			$dataValue->getDataItem(),
			$dataValue->getProperty(),
			$comparator
		);

		if ( $dataValue->getWikiValue() === '+' || $dataValue->getWikiValue() === '?' ) {
			$description = new ThingDescription();
		}

		return new SomeProperty(
			$dataValue->getProperty(),
			$description
		);
	}

}