Skip to content

Commit fc79c3f

Browse files
authored
Merge pull request #11 from Laurgrin/getBoolFix
Resolve booleans from "boolean-y" values
2 parents 804b64e + 169a294 commit fc79c3f

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
# Changelog
22

3+
## 0.3.1
4+
5+
### Fixed
6+
7+
- Try to extract boolean value from non-boolean type values if expected type is a boolean.
8+
39
## 0.3.0
410

511
### Added
12+
613
- Support for php 8.0
714

815
### Removed
16+
917
- Removed (temporary) paysera/lib-php-cs-fixer-config
1018

1119
## 0.2.2
@@ -34,4 +42,4 @@
3442
## 0.1.0
3543

3644
### Fixed
37-
- Fixes strange test case with PHP 7.0
45+
- Fixes strange test case with PHP 7.0

src/ObjectWrapper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ private function assertValueType($value, string $expectedType, string $key)
240240
$givenType = 'float';
241241
}
242242

243+
if ($expectedType === 'boolean') {
244+
$boolean = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
245+
if ($boolean === null) {
246+
throw new InvalidItemTypeException($expectedType, $givenType, $this->buildKey($key));
247+
}
248+
249+
return $boolean;
250+
}
251+
243252
if ($givenType !== $expectedType && !(is_int($value) && $expectedType === 'float')) {
244253
throw new InvalidItemTypeException($expectedType, $givenType, $this->buildKey($key));
245254
}

tests/ObjectWrapperTest.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ public function testGetRequiredStringWithNoItem()
9494

9595
public function testGetRequiredBool()
9696
{
97-
$object = new ObjectWrapper((object)['a' => true, 'b' => 'other type']);
97+
$object = new ObjectWrapper((object)['a' => true, 'b' => 'other type', 'c' => 'false', 'd' => '1', 'e' => 0]);
9898
$this->assertTrue($object->getRequiredBool('a'));
99+
$this->assertFalse($object->getRequiredBool('c'));
100+
$this->assertTrue($object->getRequiredBool('d'));
101+
$this->assertFalse($object->getRequiredBool('e'));
99102
$this->expectException(InvalidItemTypeException::class);
100103
$object->getRequiredBool('b');
101104
}
@@ -228,18 +231,12 @@ public function testGetArrayOfBoolWithDifferentType()
228231

229232
public function testGetArrayOfBoolWithDifferentItemType()
230233
{
231-
$array = [false, false, 0, true];
234+
$array = [false, 'false', 0, true, 1, 'true'];
232235
$object = new ObjectWrapper((object)['a' => $array]);
233-
$this->expectException(InvalidItemTypeException::class);
234-
$object->getArrayOfBool('a');
235-
}
236-
237-
public function testGetArrayOfBoolWithNullItem()
238-
{
239-
$array = [false, false, null, true];
240-
$object = new ObjectWrapper((object)['a' => $array]);
241-
$this->expectException(InvalidItemTypeException::class);
242-
$object->getArrayOfBool('a');
236+
$this->assertSame(
237+
[false, false, false, true, true, true],
238+
$object->getArrayOfBool('a')
239+
);
243240
}
244241

245242
public function testGetArrayOfFloat()

0 commit comments

Comments
 (0)