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 { |
||
16 | |||
17 | /** |
||
18 | * Internal value. |
||
19 | * @var bool |
||
20 | */ |
||
21 | protected $m_boolean; |
||
22 | |||
23 | 16 | public function __construct( $boolean ) { |
|
24 | 16 | if ( !is_bool( $boolean ) ) { |
|
25 | throw new DataItemException( "Initialization value '$boolean' is not a boolean." ); |
||
26 | } |
||
27 | |||
28 | 16 | $this->m_boolean = ( $boolean == true ); |
|
0 ignored issues
–
show
|
|||
29 | 16 | } |
|
30 | |||
31 | 14 | public function getDIType() { |
|
32 | 14 | return SMWDataItem::TYPE_BOOLEAN; |
|
33 | } |
||
34 | |||
35 | 14 | public function getBoolean() { |
|
36 | 14 | return $this->m_boolean; |
|
37 | } |
||
38 | |||
39 | 14 | public function getSerialization() { |
|
40 | 14 | 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 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.