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

Commit 85fb5aa

Browse files
committed
Merge branch 'hotfix/88'
Close #88 Fixes #87
2 parents 0deeb27 + 851181c commit 85fb5aa

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ All notable changes to this project will be documented in this file, in reverse
4343
required, and may lead to runtime failures.
4444
- [#66](https://github.com/zendframework/zend-validator/pull/66) fixed
4545
EmailAddress validator applying IDNA conversion to local part
46+
- [#88](https://github.com/zendframework/zend-validator/pull/88) fixed NotEmpty
47+
validator incorrectly applying types bitmaps
4648

4749

4850
## 2.8.2 - 2017-01-29

doc/book/validators/not-empty.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use Zend\Validator\NotEmpty;
6767
$validator = new NotEmpty(NotEmpty::INTEGER);
6868

6969
// Returns false on 0 or '0'
70-
$validator = new NotEmpty( NotEmpty::INTEGER + NotEmpty::ZERO);
70+
$validator = new NotEmpty( NotEmpty::INTEGER | NotEmpty::ZERO);
7171

7272
// Returns false on 0 or '0'
7373
$validator = new NotEmpty([ NotEmpty::INTEGER, NotEmpty::ZERO ]);

src/NotEmpty.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ class NotEmpty extends AbstractValidator
8383
*/
8484
public function __construct($options = null)
8585
{
86-
$this->setType($this->defaultType);
87-
8886
if ($options instanceof Traversable) {
8987
$options = ArrayUtils::iteratorToArray($options);
9088
}
@@ -99,20 +97,11 @@ public function __construct($options = null)
9997
$options = $temp;
10098
}
10199

102-
if (is_array($options)) {
103-
if (! array_key_exists('type', $options)) {
104-
$detected = 0;
105-
$found = false;
106-
foreach ($options as $option) {
107-
if (in_array($option, $this->constants, true)) {
108-
$found = true;
109-
$detected += array_search($option, $this->constants);
110-
}
111-
}
112-
113-
if ($found) {
114-
$options['type'] = $detected;
115-
}
100+
if (!isset($options['type'])) {
101+
if (($type = $this->calculateTypeValue($options)) != 0) {
102+
$options['type'] = $type;
103+
} else {
104+
$options['type'] = $this->defaultType;
116105
}
117106
}
118107

@@ -148,14 +137,14 @@ protected function calculateTypeValue($type)
148137
foreach ($type as $value) {
149138
if (is_int($value)) {
150139
$detected |= $value;
151-
} elseif (in_array($value, $this->constants)) {
152-
$detected |= array_search($value, $this->constants);
140+
} elseif (in_array($value, $this->constants, true)) {
141+
$detected |= array_search($value, $this->constants, true);
153142
}
154143
}
155144

156145
$type = $detected;
157-
} elseif (is_string($type) && in_array($type, $this->constants)) {
158-
$type = array_search($type, $this->constants);
146+
} elseif (is_string($type) && in_array($type, $this->constants, true)) {
147+
$type = array_search($type, $this->constants, true);
159148
}
160149

161150
return $type;

test/NotEmptyTest.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@ public function setUp()
2727
$this->validator = new NotEmpty();
2828
}
2929

30+
/**
31+
* Ensures that the validator follows expected behavior
32+
*
33+
* @param array $types Array of type strings or constants
34+
* @param integer $expected Expected value of calculated type
35+
*
36+
* @return void
37+
* @dataProvider constructorWithTypeArrayProvider
38+
*/
39+
public function testConstructorWithTypeArray($types, $expected)
40+
{
41+
$validator = new NotEmpty($types);
42+
$this->assertEquals($expected, $validator->getType());
43+
}
44+
45+
public function constructorWithTypeArrayProvider()
46+
{
47+
return [
48+
[['php', 'boolean'], NotEmpty::PHP],
49+
[['boolean', 'boolean'], NotEmpty::BOOLEAN],
50+
[[NotEmpty::PHP, NotEmpty::BOOLEAN], NotEmpty::PHP],
51+
[[NotEmpty::BOOLEAN, NotEmpty::BOOLEAN], NotEmpty::BOOLEAN],
52+
];
53+
}
54+
3055
/**
3156
* Ensures that the validator follows expected behavior
3257
*
@@ -729,14 +754,30 @@ public function duplicateStringSettingProvider()
729754
*
730755
* @dataProvider singleStringNotationProvider
731756
*/
732-
public function testSingleStringNotation($value, $valid)
757+
public function testSingleStringConstructorNotation($value, $valid)
733758
{
734759
$this->validator = new NotEmpty(
735760
'boolean'
736761
);
737762
$this->checkValidationValue($value, $valid);
738763
}
739764

765+
/**
766+
* Ensures that the validator follows expected behavior
767+
*
768+
* @param mixed $value Value to test
769+
* @param boolean $valid Expected validity of value
770+
*
771+
* @return void
772+
*
773+
* @dataProvider singleStringNotationProvider
774+
*/
775+
public function testSingleStringSetTypeNotation($value, $valid)
776+
{
777+
$this->validator->setType('boolean');
778+
$this->checkValidationValue($value, $valid);
779+
}
780+
740781
/**
741782
* Provides values and their expected validity for boolean empty
742783
*

0 commit comments

Comments
 (0)