Completed
Push — master ( d2d28e...1c2760 )
by mw
35:37
created

includes/dataitems/SMW_DI_Bool.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @ingroup SMWDataItems
4
 */
5
use SMW\DataItemException;
6
7
/**
8
 * This class implements Boolean data items.
9
 *
10
 * @since 1.6
11
 *
12
 * @author Markus Krötzsch
13
 * @ingroup SMWDataItems
14
 */
15
class SMWDIBoolean extends SMWDataItem {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
16
17
	/**
18
	 * Internal value.
19
	 * @var bool
20
	 */
21
	protected $m_boolean;
22
23 18
	public function __construct( $boolean ) {
24 18
		if ( !is_bool( $boolean ) ) {
25
			throw new DataItemException( "Initialization value '$boolean' is not a boolean." );
26
		}
27
28 18
		$this->m_boolean = ( $boolean == true );
29 18
	}
30
31 16
	public function getDIType() {
32 16
		return SMWDataItem::TYPE_BOOLEAN;
33
	}
34
35 16
	public function getBoolean() {
36 16
		return $this->m_boolean;
37
	}
38
39 16
	public function getSerialization() {
40 16
		return $this->m_boolean ? 't' : 'f';
41
	}
42
43 2
	public function getSortKey() {
44 2
		return $this->m_boolean ? 1 : 0;
45
	}
46
47
	/**
48
	 * Create a data item from the provided serialization string and type
49
	 * ID.
50
	 * @return SMWDIBoolean
51
	 */
52 2
	public static function doUnserialize( $serialization ) {
53 2
		if ( $serialization == 't' ) {
54 1
			return new SMWDIBoolean( true );
55 1
		} elseif  ( $serialization == 'f' ) {
56 1
			return new SMWDIBoolean( false );
57
		} else {
58
			throw new DataItemException( "Boolean data item unserialised from illegal value '$serialization'" );
59
		}
60
	}
61
62 4
	public function equals( SMWDataItem $di ) {
63 4
		if ( $di->getDIType() !== SMWDataItem::TYPE_BOOLEAN ) {
64 2
			return false;
65
		}
66 2
		return $di->getBoolean() === $this->m_boolean;
67
	}
68
}
69