Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit edd712f

Browse files
author
David Stockton
committed
Treat bit map values in ToNull filter as boolean rather than numbers
Fix for issue #50
1 parent c4bb581 commit edd712f

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/ToNull.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ public function setType($type = null)
7878
$detected = 0;
7979
foreach ($type as $value) {
8080
if (is_int($value)) {
81-
$detected += $value;
81+
$detected |= $value;
8282
} elseif (in_array($value, $this->constants)) {
83-
$detected += array_search($value, $this->constants);
83+
$detected |= array_search($value, $this->constants);
8484
}
8585
}
8686

@@ -125,48 +125,42 @@ public function filter($value)
125125
$type = $this->getType();
126126

127127
// FLOAT (0.0)
128-
if ($type >= self::TYPE_FLOAT) {
129-
$type -= self::TYPE_FLOAT;
128+
if ($type & self::TYPE_FLOAT) {
130129
if (is_float($value) && ($value == 0.0)) {
131130
return;
132131
}
133132
}
134133

135134
// STRING ZERO ('0')
136-
if ($type >= self::TYPE_ZERO_STRING) {
137-
$type -= self::TYPE_ZERO_STRING;
135+
if ($type & self::TYPE_ZERO_STRING) {
138136
if (is_string($value) && ($value == '0')) {
139137
return;
140138
}
141139
}
142140

143141
// STRING ('')
144-
if ($type >= self::TYPE_STRING) {
145-
$type -= self::TYPE_STRING;
142+
if ($type & self::TYPE_STRING) {
146143
if (is_string($value) && ($value == '')) {
147144
return;
148145
}
149146
}
150147

151148
// EMPTY_ARRAY (array())
152-
if ($type >= self::TYPE_EMPTY_ARRAY) {
153-
$type -= self::TYPE_EMPTY_ARRAY;
149+
if ($type & self::TYPE_EMPTY_ARRAY) {
154150
if (is_array($value) && ($value == [])) {
155151
return;
156152
}
157153
}
158154

159155
// INTEGER (0)
160-
if ($type >= self::TYPE_INTEGER) {
161-
$type -= self::TYPE_INTEGER;
156+
if ($type & self::TYPE_INTEGER) {
162157
if (is_int($value) && ($value == 0)) {
163158
return;
164159
}
165160
}
166161

167162
// BOOLEAN (false)
168-
if ($type >= self::TYPE_BOOLEAN) {
169-
$type -= self::TYPE_BOOLEAN;
163+
if ($type & self::TYPE_BOOLEAN) {
170164
if (is_bool($value) && ($value == false)) {
171165
return;
172166
}

test/ToNullTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,40 @@ public function testGettingDefaultType()
100100
$this->assertEquals(63, $filter->getType());
101101
}
102102

103+
/**
104+
* Ensures that providing a duplicate initializing type results in the expected type
105+
*
106+
* @param mixed $type Type to duplicate initialization
107+
* @param mixed $expected Expected resulting type
108+
*
109+
* @dataProvider duplicateTypeProvider
110+
*/
111+
public function testDuplicateInitializationResultsInCorrectType($type, $expected)
112+
{
113+
$filter = new ToNullFilter([$type, $type]);
114+
$this->assertEquals($expected, $filter->getType());
115+
}
116+
117+
public static function duplicateTypeProvider()
118+
{
119+
return [
120+
[ToNullFilter::TYPE_BOOLEAN, ToNullFilter::TYPE_BOOLEAN,],
121+
[ToNullFilter::TYPE_INTEGER, ToNullFilter::TYPE_INTEGER,],
122+
[ToNullFilter::TYPE_EMPTY_ARRAY, ToNullFilter::TYPE_EMPTY_ARRAY,],
123+
[ToNullFilter::TYPE_STRING, ToNullFilter::TYPE_STRING,],
124+
[ToNullFilter::TYPE_ZERO_STRING, ToNullFilter::TYPE_ZERO_STRING,],
125+
[ToNullFilter::TYPE_FLOAT, ToNullFilter::TYPE_FLOAT,],
126+
[ToNullFilter::TYPE_ALL, ToNullFilter::TYPE_ALL,],
127+
['boolean', ToNullFilter::TYPE_BOOLEAN,],
128+
['integer', ToNullFilter::TYPE_INTEGER,],
129+
['array', ToNullFilter::TYPE_EMPTY_ARRAY,],
130+
['string', ToNullFilter::TYPE_STRING,],
131+
['zero', ToNullFilter::TYPE_ZERO_STRING,],
132+
['float', ToNullFilter::TYPE_FLOAT,],
133+
['all', ToNullFilter::TYPE_ALL,],
134+
];
135+
}
136+
103137
public static function defaultTestProvider()
104138
{
105139
return [

0 commit comments

Comments
 (0)