diff --git a/composer.json b/composer.json index c6ad539..713b3b9 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ } }, "scripts": { - "test": "./vendor/bin/phpunit", + "test": "./vendor/bin/pest", "cs-fix": "./vendor/bin/php-cs-fixer fix -v --show-progress=dots", "phpstan": "./vendor/bin/phpstan analyse" }, diff --git a/src/GoodPhp/Serialization/TypeAdapter/Primitive/ClassProperties/ClassPropertiesPrimitiveTypeAdapter.php b/src/GoodPhp/Serialization/TypeAdapter/Primitive/ClassProperties/ClassPropertiesPrimitiveTypeAdapter.php index ba6dfd0..01de773 100644 --- a/src/GoodPhp/Serialization/TypeAdapter/Primitive/ClassProperties/ClassPropertiesPrimitiveTypeAdapter.php +++ b/src/GoodPhp/Serialization/TypeAdapter/Primitive/ClassProperties/ClassPropertiesPrimitiveTypeAdapter.php @@ -31,7 +31,7 @@ public function serialize(mixed $value): mixed $this->properties, true, fn (BoundClassProperty $property) => PropertyMappingException::rethrow( - $property->reflection, + $property, fn () => $property->serialize($value) ) ); @@ -47,7 +47,7 @@ public function deserialize(mixed $value): mixed MultipleMappingException::map( $this->properties, false, - fn (BoundClassProperty $property) => PropertyMappingException::rethrow($property->reflection, function () use ($object, $property, $value) { + fn (BoundClassProperty $property) => PropertyMappingException::rethrow($property, function () use ($object, $property, $value) { $property->deserialize($value, $object); }) ); diff --git a/src/GoodPhp/Serialization/TypeAdapter/Primitive/ClassProperties/ClassPropertiesPrimitiveTypeAdapterFactory.php b/src/GoodPhp/Serialization/TypeAdapter/Primitive/ClassProperties/ClassPropertiesPrimitiveTypeAdapterFactory.php index 1602aa5..2689037 100644 --- a/src/GoodPhp/Serialization/TypeAdapter/Primitive/ClassProperties/ClassPropertiesPrimitiveTypeAdapterFactory.php +++ b/src/GoodPhp/Serialization/TypeAdapter/Primitive/ClassProperties/ClassPropertiesPrimitiveTypeAdapterFactory.php @@ -39,16 +39,17 @@ public function create(string $typeAdapterType, Type $type, array $attributes, S return new ClassPropertiesPrimitiveTypeAdapter( fn () => $this->objectClassFactory->create($reflection->qualifiedName()), $reflection->properties()->map(function (PropertyReflection $property) use ($serializer, $typeAdapterType, $attributes) { - $attributes = $property->attributes()->toArray(); + $attributes = $property->attributes()->all(); + $serializedName = $this->namingStrategy->translate($property->name(), $attributes); - return PropertyMappingException::rethrow($property, fn () => new BoundClassProperty( + return PropertyMappingException::rethrow($serializedName, fn () => new BoundClassProperty( reflection: $property, typeAdapter: $serializer->adapter( $typeAdapterType, $property->type(), $attributes ), - serializedName: $this->namingStrategy->translate($property->name(), $attributes), + serializedName: $serializedName, optional: $property->type() instanceof NamedType && $property->type()->name === Optional::class, hasDefaultValue: $property->hasDefaultValue(), )); diff --git a/src/GoodPhp/Serialization/TypeAdapter/Primitive/ClassProperties/PropertyMappingException.php b/src/GoodPhp/Serialization/TypeAdapter/Primitive/ClassProperties/PropertyMappingException.php index db836cd..87aa608 100644 --- a/src/GoodPhp/Serialization/TypeAdapter/Primitive/ClassProperties/PropertyMappingException.php +++ b/src/GoodPhp/Serialization/TypeAdapter/Primitive/ClassProperties/PropertyMappingException.php @@ -3,7 +3,6 @@ namespace GoodPhp\Serialization\TypeAdapter\Primitive\ClassProperties; use Exception; -use GoodPhp\Reflection\Reflector\Reflection\PropertyReflection; use GoodPhp\Serialization\TypeAdapter\Exception\CollectionItemMappingException; use RuntimeException; use Throwable; @@ -17,16 +16,18 @@ public function __construct( parent::__construct("Could not map property at path '{$path}': {$previous->getMessage()}", 0, $previous); } - public static function rethrow(PropertyReflection $property, callable $callback): mixed + public static function rethrow(BoundClassProperty|string $serializedName, callable $callback): mixed { + $serializedName = $serializedName instanceof BoundClassProperty ? $serializedName->serializedName : $serializedName; + try { return $callback(); } catch (PropertyMappingException $e) { - throw new self($property->name() . '.' . $e->path, $e->getPrevious()); + throw new self($serializedName . '.' . $e->path, $e->getPrevious()); } catch (CollectionItemMappingException $e) { - throw new self($property->name() . '.' . $e->key, $e->getPrevious()); + throw new self($serializedName . '.' . $e->key, $e->getPrevious()); } catch (Exception $e) { - throw new self($property->name(), $e); + throw new self($serializedName, $e); } } }