Skip to content

Commit e3c4087

Browse files
rasstislavf3l1x
authored andcommitted
fix: fixed StringTypeMapper for non scalar values
1 parent f6f7d56 commit e3c4087

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Core/Mapping/Parameter/StringTypeMapper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
namespace Apitte\Core\Mapping\Parameter;
44

5+
use Apitte\Core\Exception\Runtime\InvalidArgumentTypeException;
6+
57
class StringTypeMapper implements ITypeMapper
68
{
79

810
public function normalize(mixed $value): ?string
911
{
12+
if (!is_scalar($value)) {
13+
throw new InvalidArgumentTypeException('string');
14+
}
15+
1016
return (string) $value;
1117
}
1218

tests/Cases/Core/Mapping/Parameter/StringTypeMapperTest.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_once __DIR__ . '/../../../../bootstrap.php';
44

5+
use Apitte\Core\Exception\Runtime\InvalidArgumentTypeException;
56
use Apitte\Core\Mapping\Parameter\StringTypeMapper;
67
use Apitte\Core\Schema\EndpointParameter;
78
use Tester\Assert;
@@ -21,6 +22,15 @@ final class StringTypeMapperTest extends TestCase
2122
Assert::same('-10', $mapper->normalize(-10, $parameter));
2223
}
2324

25+
public function testFail(): void
26+
{
27+
$mapper = new StringTypeMapper();
28+
29+
Assert::exception(function () use ($mapper): void {
30+
$mapper->normalize(['']);
31+
}, InvalidArgumentTypeException::class);
32+
}
33+
2434
}
2535

2636
(new StringTypeMapperTest())->run();

0 commit comments

Comments
 (0)