summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/dataitems/SMW_DI_Bool.php
blob: 8bddf9fb8d8b927f4f867d253dbf6ba09f9be5b9 (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
<?php
/**
 * @ingroup SMWDataItems
 */

use SMW\Exception\DataItemException;

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

	/**
	 * Internal value.
	 * @var bool
	 */
	protected $m_boolean;

	public function __construct( $boolean ) {
		if ( !is_bool( $boolean ) ) {
			throw new DataItemException( "Initialization value '$boolean' is not a boolean." );
		}

		$this->m_boolean = ( $boolean == true );
	}

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

	public function getBoolean() {
		return $this->m_boolean;
	}

	public function getSerialization() {
		return $this->m_boolean ? 't' : 'f';
	}

	public function getSortKey() {
		return $this->m_boolean ? 1 : 0;
	}

	/**
	 * Create a data item from the provided serialization string and type
	 * ID.
	 * @return SMWDIBoolean
	 */
	public static function doUnserialize( $serialization ) {
		if ( $serialization == 't' ) {
			return new SMWDIBoolean( true );
		} elseif  ( $serialization == 'f' ) {
			return new SMWDIBoolean( false );
		} else {
			throw new DataItemException( "Boolean data item unserialised from illegal value '$serialization'" );
		}
	}

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