Skip to content

Commit 94f7f76

Browse files
claudef3l1x
authored andcommitted
Add options array parameter to ITypeMapper interface
- Add 2nd parameter array \$options = [] to ITypeMapper::normalize() - Update all type mapper implementations with @inheritdoc - Make EnumTypeMapper implement ITypeMapper interface - Pass EndpointParameter via options['endpoint'] in RequestParameterMapping - Remove enum values from exception message for cleaner output
1 parent a422bea commit 94f7f76

File tree

9 files changed

+39
-13
lines changed

9 files changed

+39
-13
lines changed

src/Core/Mapping/Parameter/BooleanTypeMapper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
class BooleanTypeMapper implements ITypeMapper
88
{
99

10-
public function normalize(mixed $value): ?bool
10+
/**
11+
* @inheritDoc
12+
*/
13+
public function normalize(mixed $value, array $options = []): ?bool
1114
{
1215
if ($value === 'true' || $value === true) {
1316
return true;

src/Core/Mapping/Parameter/DateTimeTypeMapper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
class DateTimeTypeMapper implements ITypeMapper
1010
{
1111

12-
public function normalize(mixed $value): ?DateTimeImmutable
12+
/**
13+
* @inheritDoc
14+
*/
15+
public function normalize(mixed $value, array $options = []): ?DateTimeImmutable
1316
{
1417
try {
1518
$value = DateTimeImmutable::createFromFormat(DATE_ATOM, $value);

src/Core/Mapping/Parameter/EnumTypeMapper.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
namespace Apitte\Core\Mapping\Parameter;
44

55
use Apitte\Core\Exception\Runtime\InvalidArgumentTypeException;
6+
use Apitte\Core\Schema\EndpointParameter;
67

78
class EnumTypeMapper implements ITypeMapper
89
{
910

10-
public function normalize(mixed $value, ?array $enumValues = null): string
11+
/**
12+
* @inheritDoc
13+
*/
14+
public function normalize(mixed $value, array $options = []): string|int
1115
{
12-
if (!in_array($value, $enumValues)) {
16+
/** @var EndpointParameter|null $endpoint */
17+
$endpoint = $options['endpoint'] ?? null;
18+
$enumValues = $endpoint?->getEnum() ?? [];
19+
20+
if ($enumValues === [] || !in_array($value, $enumValues, true)) {
1321
throw new InvalidArgumentTypeException(InvalidArgumentTypeException::TYPE_ENUM);
1422
}
1523

src/Core/Mapping/Parameter/FloatTypeMapper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
class FloatTypeMapper implements ITypeMapper
88
{
99

10-
public function normalize(mixed $value): ?float
10+
/**
11+
* @inheritDoc
12+
*/
13+
public function normalize(mixed $value, array $options = []): ?float
1114
{
1215
if (is_string($value)) {
1316
$value = str_replace(',', '.', $value); // Accept also comma as decimal separator

src/Core/Mapping/Parameter/ITypeMapper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ interface ITypeMapper
88
{
99

1010
/**
11+
* @param array<string, mixed> $options
1112
* @throws InvalidArgumentTypeException
1213
*/
13-
public function normalize(mixed $value/*, ?array $enumValues = null*/): mixed;
14+
public function normalize(mixed $value, array $options = []): mixed;
1415

1516
}

src/Core/Mapping/Parameter/IntegerTypeMapper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
class IntegerTypeMapper implements ITypeMapper
88
{
99

10-
public function normalize(mixed $value): int
10+
/**
11+
* @inheritDoc
12+
*/
13+
public function normalize(mixed $value, array $options = []): int
1114
{
1215
if (is_int($value) || (is_string($value) && preg_match('#^[+-]?[0-9]+\z#', $value) === 1)) {
1316
return (int) $value;

src/Core/Mapping/Parameter/StringTypeMapper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
class StringTypeMapper implements ITypeMapper
88
{
99

10-
public function normalize(mixed $value): ?string
10+
/**
11+
* @inheritDoc
12+
*/
13+
public function normalize(mixed $value, array $options = []): ?string
1114
{
1215
if (!is_scalar($value)) {
1316
throw new InvalidArgumentTypeException('string');

src/Core/Mapping/RequestParameterMapping.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class RequestParameterMapping
2222
InvalidArgumentTypeException::TYPE_FLOAT => '%s request parameter "%s" should be of type float or integer.',
2323
InvalidArgumentTypeException::TYPE_BOOLEAN => '%s request parameter "%s" should be of type boolean. Pass "true" for true or "false" for false.',
2424
InvalidArgumentTypeException::TYPE_DATETIME => '%s request parameter "%s" should be of type datetime in format ISO 8601 (Y-m-d\TH:i:sP).',
25-
InvalidArgumentTypeException::TYPE_ENUM => '%s request parameter "%s" is not in the allowed values `[%s]`.',
25+
InvalidArgumentTypeException::TYPE_ENUM => '%s request parameter "%s" is not in the allowed values.',
2626
];
2727

2828
protected static string $customException = '%s request parameter "%s" should be of type %s.%s';
@@ -211,14 +211,13 @@ protected function normalize(mixed $value, EndpointParameter $parameter, ITypeMa
211211
}
212212

213213
try {
214-
return $mapper->normalize($value, $parameter->getEnum());
214+
return $mapper->normalize($value, ['endpoint' => $parameter]);
215215
} catch (InvalidArgumentTypeException $e) {
216216
if (array_key_exists($e->getType(), self::$exceptions)) {
217217
throw new ClientErrorException(sprintf(
218218
self::$exceptions[$e->getType()],
219219
ucfirst($parameter->getIn()),
220-
$parameter->getName(),
221-
$parameter->getEnum() ? implode(', ', $parameter->getEnum()) : ''
220+
$parameter->getName()
222221
));
223222
} else {
224223
throw new ClientErrorException(sprintf(

tests/Fixtures/Mapping/Parameter/MyEmailTypeMapper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
class MyEmailTypeMapper implements ITypeMapper
99
{
1010

11-
public function normalize(mixed $value): string
11+
/**
12+
* @inheritDoc
13+
*/
14+
public function normalize(mixed $value, array $options = []): string
1215
{
1316
if (is_string($value) && filter_var($value, FILTER_VALIDATE_EMAIL)) {
1417
return $value;

0 commit comments

Comments
 (0)