-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoolean.php
169 lines (148 loc) · 3.88 KB
/
Boolean.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* ELSWAK Boolean
*
* @author Errol Sayre
*/
/**
* Represent a boolean
* @package ELSWAK
*/
class ELSWAK_Boolean {
protected $value;
public function __construct($value = null) {
$this->setValue($value);
}
public function setValue($value) {
$this->value = $this->valueAsBoolean($value);
return $this;
}
public function value() {
return $this->value;
}
/**
* Stringify the value
*
* Despite the fact that we have a static method to do this same task,
* perform this logic locally to avoid a redundant parsing of the
* boolean value.
*
* @param string $trueLabel
* @param string $falseLabel
* @return string
*/
public function label($trueLabel = 'yes', $falseLabel = 'no') {
if ($this->value) {
return $trueLabel;
}
return $falseLabel;
}
public function __toString() {
return $this->label();
}
/**
* Generic setter
*
* Allow any attempts to set a value on this object to succeed. I'm not
* sure why I added this, but it's probably mostly to allow setting the
* value as a property rather than a method to match the form of
* ELSWAK_Object.
*
* @param string $property
* @param mixed $value
* @return ELSWAK_Boolean self
*/
public function __set($property, $value) {
$this->setValue($value);
return $this;
}
public function __get($property) {
if (strtolower($property) == 'label') {
return $this->label();
}
return $this->value;
}
/**
* Booleanify a value
*
* Convert values to booleans in a sensible manner. PHP construes any
* non-null value as true but we want to rework that behavior to
* support logical translations of various values into booleans. For
* example, a string value of 'false' should translate to a boolean
* value of false but by default in PHP this is not the case.
*
* We could potentially check for values that are already specifically
* booleans, however this would only save two comparisons in that case
* while instead costing one additional comparison in all other cases.
*
* @mixed $value
* @return boolean
*/
public static function valueAsBoolean($value) {
// look for potential string matches
if (is_string($value)) {
$value = strtolower(trim($value));
if (in_array($value, self::acceptableTrueValues())) {
return true;
} else {
return false;
}
}
// look for a boolean masquerading as a number
if (is_numeric($value)) {
if ($value > 0) {
return true;
} else {
return false;
}
}
// perform one last casting using PHP's rules
if ($value) {
return true;
}
return false;
}
/**
* Stringify a boolean
*
* In order to ensure this method behaves as expected, we must first
* translate the provided value to a boolean.
*
* As opposed to the behavior of the instance method, this method opts
* to utilize the terms TRUE and FALSE. This is a purely arbitrary
* choice that has more to do with the particular use-case that
* instigated this method's addition.
*
* @param string $trueLabel
* @param string $falseLabel
* @return string
*/
public static function booleanAsString($value, $trueLabel = 'TRUE', $falseLabel = 'FALSE') {
// ensure the value has been "booleanized"
if (self::valueAsBoolean($value)) {
return $trueLabel;
}
return $falseLabel;
}
public static function acceptableTrueValues() {
// these values should be listed in order of decreasing likelihood to match in order to minimize comparisons
return array(
'yes',
'y',
'true',
'x', // SAP style boolean
);
}
public static function acceptableFalseValues() {
// please note that in the case of valueAsBoolean and valueAsNullBoolean ALL strings that are not null or true matches are considered false
// these values should be listed in order of decreasing likelihood to match in order to minimize comparisons
return array(
'no',
'n',
' ', // SAP style boolean
'',
'none',
'not',
);
}
}