summaryrefslogtreecommitdiff
blob: 8e63bb3f8832f76bd344655d0aa3376056c1f6b2 (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
<?php
/**
 * @file
 * @ingroup SMWDataItems
 */

use SMW\DataItemException;

/**
 * This class implements number data items.
 *
 * @since 1.6
 *
 * @author Markus Krötzsch
 * @ingroup SMWDataItems
 */
class SMWDINumber extends SMWDataItem {

	/**
	 * Internal value.
	 * @var numeric
	 */
	protected $m_number;

	public function __construct( $number ) {
		if ( !is_numeric( $number ) ) {
			throw new DataItemException( "Initialization value '$number' is not a number." );
		}
		$this->m_number = $number;
	}

	public function getDIType() {
		return SMWDataItem::TYPE_NUMBER;
	}

	public function getNumber() {
		return $this->m_number;
	}

	public function getSortKey() {
		return $this->m_number;
	}

	/**
	 * @see SMWDataItem::getSortKeyDataItem()
	 * @return SMWDataItem
	 */
	public function getSortKeyDataItem() {
		return $this;
	}

	public function getSerialization() {
		return strval( $this->m_number );
	}

	/**
	 * Create a data item from the provided serialization string and type
	 * ID.
	 * @note PHP can convert any string to some number, so we do not do
	 * validation here (because this would require less efficient parsing).
	 * @return SMWDINumber
	 */
	public static function doUnserialize( $serialization ) {
		return new SMWDINumber( floatval( $serialization ) );
	}

	public function equals( SMWDataItem $di ) {
		if ( $di->getDIType() !== SMWDataItem::TYPE_NUMBER ) {
			return false;
		}

		return $di->getNumber() === $this->m_number;
	}

}