Skip to content

Commit 1c09a08

Browse files
committed
Adjustment for property context
1 parent fc3db88 commit 1c09a08

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

src/Exceptions/UnknownTypeException.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@
77

88
class UnknownTypeException extends Exception
99
{
10-
final protected function __construct(string $message = "")
11-
{
12-
parent::__construct($message);
13-
}
10+
protected const int UNKNOWN_TYPE_EXCEPTION_CODE = 0;
1411

15-
public static function from(
16-
Type $type,
17-
Type ...$additionalTypes
18-
): static {
19-
$types = implode(', ', array_column([$type, ...$additionalTypes], 'value'));
12+
public static function unknownType(Type $type, Type ...$types): self
13+
{
14+
$types = array_map(
15+
static fn (Type $type): string => $type->value,
16+
$types
17+
);
2018

21-
return new static(
22-
"Unknown type '{$types}'"
19+
return new self(
20+
sprintf(
21+
'Unknown type "%s". Known types are: %s.',
22+
$type->value,
23+
implode(', ', $types)
24+
),
25+
self::UNKNOWN_TYPE_EXCEPTION_CODE
2326
);
2427
}
2528
}

src/Support/Traits/HasSerializers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function getSerializersFromPropertyContext(
3636
array_column($serializer::supportedTypes(), 'value')
3737
)) ? new $serializer() : null,
3838
self::serializersList()
39-
))) ?: throw UnknownTypeException::from(...$propertyContext->types);
39+
))) ?: throw UnknownTypeException::unknownType(...$propertyContext->types);
4040
}
4141

4242
/**

tests/Unit/Contexts/PropertyContextTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use Nuxtifyts\PhpDto\Attributes\Property\Computed;
66
use Nuxtifyts\PhpDto\Attributes\Property\WithRefiner;
7-
use Nuxtifyts\PhpDto\Contexts\ClassContext;
87
use Nuxtifyts\PhpDto\Contexts\PropertyContext;
98
use Nuxtifyts\PhpDto\Contexts\TypeContext;
109
use Nuxtifyts\PhpDto\Data;
1110
use Nuxtifyts\PhpDto\DataRefiners\DateTimeRefiner;
1211
use Nuxtifyts\PhpDto\Enums\Property\Type;
12+
use Nuxtifyts\PhpDto\Exceptions\UnsupportedTypeException;
1313
use Nuxtifyts\PhpDto\Serializers\ScalarTypeSerializer;
1414
use Nuxtifyts\PhpDto\Tests\Dummies\ComputedPropertiesData;
1515
use Nuxtifyts\PhpDto\Tests\Dummies\Enums\YesNoBackedEnum;
@@ -31,6 +31,7 @@
3131
#[CoversClass(TypeContext::class)]
3232
#[CoversClass(Computed::class)]
3333
#[CoversClass(WithRefiner::class)]
34+
#[CoversClass(UnsupportedTypeException::class)]
3435
#[UsesClass(ComputedPropertiesData::class)]
3536
#[UsesClass(ScalarTypeSerializer::class)]
3637
#[UsesClass(PersonData::class)]
@@ -80,6 +81,24 @@ public function can_resolve_serializers_of_property(): void
8081
self::assertInstanceOf(ScalarTypeSerializer::class, $serializers[0]);
8182
}
8283

84+
/**
85+
* @throws Throwable
86+
*/
87+
#[Test]
88+
public function will_throw_an_exception_if_property_type_is_not_supported(): void
89+
{
90+
$object = new readonly class ('') extends Data {
91+
public function __construct(
92+
public mixed $value
93+
) {
94+
}
95+
};
96+
97+
$reflectionProperty = new ReflectionProperty($object::class, 'value');
98+
self::expectException(UnsupportedTypeException::class);
99+
PropertyContext::getInstance($reflectionProperty);
100+
}
101+
83102
/**
84103
* @throws Throwable
85104
*/

0 commit comments

Comments
 (0)