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

namespace SMW\Deserializers\DVDescriptionDeserializer;

use SMWDINumber as DINumber;
use SMWNumberValue as NumberValue;

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

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

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

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

		if( $comparator !== SMW_CMP_LIKE && $comparator !== SMW_CMP_PRIM_LIKE ) {

			$this->dataValue->setUserValue( $value );

			if ( $this->dataValue->isValid() ) {
				return $this->descriptionFactory->newValueDescription(
					$this->dataValue->getDataItem(),
					$this->dataValue->getProperty(),
					$comparator
				);
			} else {
				return $this->descriptionFactory->newThingDescription();
			}
		}

		// Remove things that belong to SMW_CMP_LIKE
		$value = str_replace( [ '~', '*', '!' ], '', $value );

		$this->dataValue->setUserValue( $value );

		if ( !$this->dataValue->isValid() ) {
			return $this->descriptionFactory->newThingDescription();
		}

		$dataItem = $this->dataValue->getDataItem();
		$property = $this->dataValue->getProperty();

		if ( $this->getErrors() !== [] ) {
			return $this->descriptionFactory->newThingDescription();
		}

		// in:/~ signals a range request for a number context
		if ( $dataItem->getNumber() >= 0 ) {
			// `[[Has number::in:99]]` -> `[[Has number:: [[≥0]] [[≤99]] ]]`)
			$description = $this->descriptionFactory->newConjunction(
				[
					$this->descriptionFactory->newValueDescription( new DINumber( 0 ), $property, SMW_CMP_GEQ ),
					$this->descriptionFactory->newValueDescription( $dataItem, $property, SMW_CMP_LEQ )
				]
			);
		} else {
			// `[[Has number::in:-100]]` -> `[[Has number:: [[≥-100]] [[≤0]] ]]`
			$description = $this->descriptionFactory->newConjunction(
				[
					$this->descriptionFactory->newValueDescription( $dataItem, $property, SMW_CMP_GEQ ),
					$this->descriptionFactory->newValueDescription( new DINumber( 0 ), $property,SMW_CMP_LEQ  )
				]
			);
		}

		return $description;
	}

}