BooleanParameter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameterType() 0 3 1
A setValue() 0 22 4
A getDefault() 0 11 2
1
<?php
2
	
3
/**
4
	* BooleanParameter.php
5
	*/
6
	
7
namespace netfocusinc\argh;
8
9
use netfocusinc\argh\Parameter;
10
11
/**
12
	* Boolean parameter.
13
	*
14
	* Subtype of Parameter that represents a boolean value.
15
	* Boolean parameters values are restricted to boolean literal values.
16
	*
17
	* @api
18
	*
19
	* @author Benjamin Hough
20
	*
21
	* @since 1.0.0
22
	*/
23
class BooleanParameter extends Parameter
24
{
25
	
26
	//
27
	// STATIC FUNCTIONS
28
	//
29
	
30
	
31
	//
32
	// PUBLIC FUNCTIONS
33
	//
34
35
	/**
36
		* Returns a boolean default value for this Parameter.
37
		*
38
		* Overrides Parameter::getDefault() to ensure a boolean return value
39
		*
40
		* @since 1.0.0
41
		*
42
		* @return bool
43
		*/	
44
	public function getDefault()
45
	{
46
		// Interpret any non-empty value as TRUE
47
		// Note that FALSE is considered to be empty()
48
		if( !empty( parent::getDefault() ) )
49
		{
50
			return TRUE;
51
		}	
52
		else
53
		{
54
			return FALSE;
55
		}
56
	}
57
	
58
	/**
59
		* Returns ARGH_TYPE_BOOLEAN
60
		*
61
		* @since 1.0.0
62
		*
63
		* @return int
64
		*/
65
	public function getParameterType(): int
66
	{
67
		return Parameter::ARGH_TYPE_BOOLEAN;
68
	}
69
70
	/**
71
		* Sets the boolean value of this Parameter.
72
		*
73
		* Translates any non boolean values to their boolean equivalents.
74
		*
75
		* @since 1.0.0
76
		*
77
		* @return int
78
		*/
79
	public function setValue($value)
80
	{
81
				
82
		if( null === $value )
83
		{
84
			// null (no value) considered TRUE (this is how flags work; e.g. the presence of a boolean flag -x without value means TRUE)
85
			$this->value = TRUE;
86
		}
87
		else if( FALSE == $value )
88
		{
89
			// 'Falsey' (boolean) FALSE, (int) 0, (float 0.0), (string) '0', (string) '', NULL
90
			$this->value = FALSE;
91
		}
92
		else if( in_array($value, array('0', 'false', 'False', 'FALSE', 'off', 'Off', 'OFF')) )
93
		{
94
			// Certain character values should be considered to mean FALSE
95
			$this->value = FALSE;
96
		}
97
		else
98
		{
99
			// Everything else considered TRUE
100
			$this->value = TRUE;
101
		}
102
	}
103
	
104
}
105