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

Commit a7466e6

Browse files
AndrolGenhaldXerkus
authored andcommitted
Removed duplicate logic from NotEmpty constructor, made array_search strict
1 parent 568c8bc commit a7466e6

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

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: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,8 @@ public function __construct($options = null)
100100
}
101101

102102
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-
}
103+
if (($type = $this->calculateTypeValue($options)) != 0) {
104+
$options['type'] = $type;
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ public function testConstructorWithTypeArray()
4040
'boolean'
4141
]);
4242
$this->assertEquals(NotEmpty::BOOLEAN, $validator->getType());
43+
44+
$validator = new NotEmpty([
45+
NotEmpty::PHP,
46+
NotEmpty::BOOLEAN
47+
]);
48+
$this->assertEquals(NotEmpty::PHP, $validator->getType());
49+
50+
$validator = new NotEmpty([
51+
NotEmpty::BOOLEAN,
52+
NotEmpty::BOOLEAN
53+
]);
54+
$this->assertEquals(NotEmpty::BOOLEAN, $validator->getType());
4355
}
4456

4557
/**

0 commit comments

Comments
 (0)