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

Commit

Permalink
Merge branch 'hotfix/51' into develop
Browse files Browse the repository at this point in the history
Forward port #51
  • Loading branch information
weierophinney committed May 17, 2017
2 parents 057f241 + ae1fab5 commit bccbf28
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.md
Expand Up @@ -54,10 +54,12 @@ All notable changes to this project will be documented in this file, in reverse
- [#36](https://github.com/zendframework/zend-filter/pull/36) fixes an issue in
the constructor whereby a discovered option was not removed from the options
list after being used to set the compression algorithm.
- [#49](https://github.com/zendframework/zend-filter/pull/49) fixes logic within
the `Boolean` filter to use boolean rather than arithmetic operations,
ensuring that if the same type is specified multiple times via the options, it
will be represented correctly.
- [#49](https://github.com/zendframework/zend-filter/pull/49) and
[#51](https://github.com/zendframework/zend-filter/pull/51) fix logic within
the `Boolean` and `ToNull` filters to use boolean rather than arithmetic
operations, ensuring that if the same type is specified multiple times via the
options, it will be aggregated correctly internally, and thus ensure correct
operation of the filter.

## 2.7.1 - 2016-04-18

Expand Down
22 changes: 8 additions & 14 deletions src/ToNull.php
Expand Up @@ -78,9 +78,9 @@ public function setType($type = null)
$detected = 0;
foreach ($type as $value) {
if (is_int($value)) {
$detected += $value;
$detected |= $value;
} elseif (in_array($value, $this->constants)) {
$detected += array_search($value, $this->constants);
$detected |= array_search($value, $this->constants);
}
}

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

// FLOAT (0.0)
if ($type >= self::TYPE_FLOAT) {
$type -= self::TYPE_FLOAT;
if ($type & self::TYPE_FLOAT) {
if (is_float($value) && ($value == 0.0)) {
return;
}
}

// STRING ZERO ('0')
if ($type >= self::TYPE_ZERO_STRING) {
$type -= self::TYPE_ZERO_STRING;
if ($type & self::TYPE_ZERO_STRING) {
if (is_string($value) && ($value == '0')) {
return;
}
}

// STRING ('')
if ($type >= self::TYPE_STRING) {
$type -= self::TYPE_STRING;
if ($type & self::TYPE_STRING) {
if (is_string($value) && ($value == '')) {
return;
}
}

// EMPTY_ARRAY (array())
if ($type >= self::TYPE_EMPTY_ARRAY) {
$type -= self::TYPE_EMPTY_ARRAY;
if ($type & self::TYPE_EMPTY_ARRAY) {
if (is_array($value) && ($value == [])) {
return;
}
}

// INTEGER (0)
if ($type >= self::TYPE_INTEGER) {
$type -= self::TYPE_INTEGER;
if ($type & self::TYPE_INTEGER) {
if (is_int($value) && ($value == 0)) {
return;
}
}

// BOOLEAN (false)
if ($type >= self::TYPE_BOOLEAN) {
$type -= self::TYPE_BOOLEAN;
if ($type & self::TYPE_BOOLEAN) {
if (is_bool($value) && ($value == false)) {
return;
}
Expand Down
34 changes: 34 additions & 0 deletions test/ToNullTest.php
Expand Up @@ -100,6 +100,40 @@ public function testGettingDefaultType()
$this->assertEquals(63, $filter->getType());
}

/**
* Ensures that providing a duplicate initializing type results in the expected type
*
* @param mixed $type Type to duplicate initialization
* @param mixed $expected Expected resulting type
*
* @dataProvider duplicateTypeProvider
*/
public function testDuplicateInitializationResultsInCorrectType($type, $expected)
{
$filter = new ToNullFilter([$type, $type]);
$this->assertEquals($expected, $filter->getType());
}

public static function duplicateTypeProvider()
{
return [
[ToNullFilter::TYPE_BOOLEAN, ToNullFilter::TYPE_BOOLEAN,],
[ToNullFilter::TYPE_INTEGER, ToNullFilter::TYPE_INTEGER,],
[ToNullFilter::TYPE_EMPTY_ARRAY, ToNullFilter::TYPE_EMPTY_ARRAY,],
[ToNullFilter::TYPE_STRING, ToNullFilter::TYPE_STRING,],
[ToNullFilter::TYPE_ZERO_STRING, ToNullFilter::TYPE_ZERO_STRING,],
[ToNullFilter::TYPE_FLOAT, ToNullFilter::TYPE_FLOAT,],
[ToNullFilter::TYPE_ALL, ToNullFilter::TYPE_ALL,],
['boolean', ToNullFilter::TYPE_BOOLEAN,],
['integer', ToNullFilter::TYPE_INTEGER,],
['array', ToNullFilter::TYPE_EMPTY_ARRAY,],
['string', ToNullFilter::TYPE_STRING,],
['zero', ToNullFilter::TYPE_ZERO_STRING,],
['float', ToNullFilter::TYPE_FLOAT,],
['all', ToNullFilter::TYPE_ALL,],
];
}

public static function defaultTestProvider()
{
return [
Expand Down

0 comments on commit bccbf28

Please sign in to comment.