Passed
Push — develop ( 319bd8...330c7a )
by Neill
16:41 queued 15s
created

Checkbox   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 17
c 2
b 0
f 1
dl 0
loc 103
ccs 9
cts 18
cp 0.5
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilterField() 0 5 1
A setCheckboxLabel() 0 4 1
A getValueDisplay() 0 3 2
A getCheckboxLabel() 0 3 1
A fake() 0 3 2
A getProperties() 0 3 1
A getData() 0 7 2
A getComponentDetails() 0 4 1
1
<?php
2
/**
3
 * @link http://www.newicon.net/neon
4
 * @copyright Copyright (c) 2016 Newicon Ltd
5
 * @license http://www.newicon.net/neon/license
6
 */
7
8
namespace neon\core\form\fields;
9
10
use \neon\core\form\fields\Field;
11
use \neon\core\helpers\Arr;
12
13
/**
14
 * Class Text
15
 * @package neon\core\form
16
 */
17
class Checkbox extends Field
18
{
19
20
	/**
21
	 * @inheritdoc
22
	 */
23
	public $ddsDataType = 'boolean';
24
25
	/**
26
	 * The label to display when true
27
	 * @var string
28
	 */
29
	public $trueLabel = 'True';
30
31
	/**
32
	 * The label to display when false
33
	 * @var string
34
	 */
35
	public $falseLabel = 'False';
36
37
	/**
38
	 * The text to display adjacent to the checkbox
39
	 *
40
	 * @var string
41
	 */
42
	private $_checkboxLabel;
43
44
	/**
45
	 * The text to display adjacent to the checkbox
46
	 *
47
	 * @param $value
48
	 * @return $this;
49
	 */
50 2
	public function setCheckboxLabel($value)
51
	{
52 2
		$this->_checkboxLabel = $value;
53 2
		return $this;
54
	}
55
56
	/**
57
	 * Get the text to display adjacent to the checkbox
58
	 *
59
	 * @return string
60
	 */
61 2
	public function getCheckboxLabel()
62
	{
63 2
		return $this->_checkboxLabel;
64
	}
65
66
	/**
67
	 * @inheritdoc
68
	 */
69 2
	public function getData()
70
	{
71
		// The value here is evaluating the user input
72
		// typically this is a string of 0 or 1, however we also support true and false strings
73
		// by using the truthy function a string of 'false' will return as boolean false
74
		// (a normal php if statement would evaluate a string of 'false' as true
75 2
		return truthy($this->getValue()) ? 1 : 0;
76
	}
77
78
	/**
79
	 * @inheritdoc
80
	 */
81
	public function getValueDisplay($context='')
82
	{
83
		return $this->getData() ? $this->trueLabel : $this->falseLabel;
84
	}
85
86
	/**
87
	 * @inheritdoc
88
	 */
89
	public function getFilterField()
90
	{
91
		return [
92
			'class' => 'neon\core\form\fields\Select',
93
			'items' => [0 => $this->falseLabel, 1 => $this->trueLabel]
94
		];
95
	}
96
97
	/**
98
	 * @inheritdoc
99
	 */
100 2
	public function getProperties()
101
	{
102 2
		return Arr::merge(parent::getProperties(), ['checkboxLabel']);
103
	}
104
105
	/**
106
	 * @inheritdoc
107
	 */
108
	public function fake()
109
	{
110
		return faker()->boolean ? 0 : 1;
111
	}
112
113
	/**
114
	 * @inheritdoc
115
	 */
116
	public function getComponentDetails()
117
	{
118
		return [
119
			'label' => 'Checkbox', 'group' => 'Choice', 'icon' => 'fa fa-check-square', 'order' => 440,
120
		];
121
	}
122
}