Skip to content

Commit

Permalink
Strict BooleanType
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Sep 17, 2018
1 parent 3820cf0 commit 0fc156a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .docs/README.md
Expand Up @@ -243,8 +243,8 @@ Available data types are `scalar`, `string`, `int`, `float`, `bool` and `datetim
- Converts value to float.
- Accepts values which have decimals divided by comma `,` or dot `.`
- `bool`
- Converts `'true'` and `'1'` to `true`
- and `false` and `0` to `false`
- Converts `'true'` to `true`
- and `false` to `false`
- `datetime`
- Converts value to DateTimeImmutable.
- Each of the data types could return null if @RequestParameter(allowEmpty=true)
Expand Down
4 changes: 2 additions & 2 deletions src/Mapping/Parameter/BooleanTypeMapper.php
Expand Up @@ -20,11 +20,11 @@ public function normalize($value): ?bool
return $value;
}

if ($value === '1' || $value === 'true') {
if ($value === 'true') {
return true;
}

if ($value === '0' || $value === 'false') {
if ($value === 'false') {
return false;
}

Expand Down
10 changes: 8 additions & 2 deletions tests/cases/Mapping/Parameter/BooleanTypeMapper.phpt
Expand Up @@ -22,10 +22,8 @@ final class TestBooleanTypeMapper extends TestCase
Assert::same(null, $mapper->normalize(''));
Assert::same(true, $mapper->normalize(true));
Assert::same(true, $mapper->normalize('true'));
Assert::same(true, $mapper->normalize('1'));
Assert::same(false, $mapper->normalize(false));
Assert::same(false, $mapper->normalize('false'));
Assert::same(false, $mapper->normalize('0'));
}

public function testFail(): void
Expand All @@ -35,6 +33,14 @@ final class TestBooleanTypeMapper extends TestCase
Assert::exception(function () use ($mapper): void {
$mapper->normalize('string');
}, InvalidArgumentTypeException::class);

Assert::exception(function () use ($mapper): void {
$mapper->normalize('0');
}, InvalidArgumentTypeException::class);

Assert::exception(function () use ($mapper): void {
$mapper->normalize('1');
}, InvalidArgumentTypeException::class);
}

}
Expand Down

0 comments on commit 0fc156a

Please sign in to comment.