Skip to content

Commit

Permalink
Simplify the Serializer feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 12, 2023
1 parent b688107 commit 43273df
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
34 changes: 12 additions & 22 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,23 @@ private function assertObjectIsInValidState(object $object): void
*
* @throws MappingFailed
*
* @return non-empty-array<string, PropertySetter>
* @return non-empty-array<PropertySetter>
*/
private function findPropertySetters(array $propertyNames): array
{
$check = [];
$propertySetters = [];
foreach ($this->class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
if ($property->isStatic()) {
continue;
}

$propertyName = $property->getName();
$attribute = $property->getAttributes(Cell::class, ReflectionAttribute::IS_INSTANCEOF);
if ([] !== $attribute) {
//the property can not be cast yet
//as casting will be set using the Cell attribute
continue;
}

/** @var int|false $offset */
$offset = array_search($propertyName, $propertyNames, true);
Expand All @@ -172,33 +177,19 @@ private function findPropertySetters(array $propertyNames): array
}

$type = $property->getType();
if (!$type instanceof ReflectionNamedType) {
throw new MappingFailed('The property `'.$propertyName.'` must be typed with a single nullable type.');
}

$attribute = $property->getAttributes(Cell::class, ReflectionAttribute::IS_INSTANCEOF);
if ([] !== $attribute) {
//the property can not be cast yet
//as casting will be set using the Cell attribute
$check['property:'.$propertyName] = $propertyName;
continue;
if (null === $type) {
throw new MappingFailed('The property `'.$propertyName.'` must be typed.');
}

$cast = $this->resolveTypeCasting($type);
if (null === $cast) {
throw new MappingFailed('No valid type casting was found for property `'.$propertyName.'`.');
}

$propertySetters['property:'.$propertyName] = new PropertySetter($property, $offset, $cast);
$propertySetters[] = new PropertySetter($property, $offset, $cast);
}

$propertySetters = [...$propertySetters, ...$this->findPropertySettersByCellAttributes($propertyNames)];
foreach ($check as $key => $propertyName) {
//if we still can not find how to cast the property we must throw
if (!isset($propertySetters[$key])) {
throw new MappingFailed('No valid type casting was found for property `'.$propertyName.'`.');
}
}

//if converters is empty it means the Serializer
//was unable to detect properties to assign
Expand All @@ -212,7 +203,7 @@ private function findPropertySetters(array $propertyNames): array
/**
* @param array<string> $propertyNames
*
* @return array<string, PropertySetter>
* @return array<PropertySetter>
*/
private function findPropertySettersByCellAttributes(array $propertyNames): array
{
Expand All @@ -222,8 +213,7 @@ private function findPropertySettersByCellAttributes(array $propertyNames): arra
return $carry;
}

$type = $accessor instanceof ReflectionProperty ? 'property' : 'method';
$carry[$type.':'.$accessor->getName()] = $propertySetter;
$carry[] = $propertySetter;

return $carry;
};
Expand Down
2 changes: 1 addition & 1 deletion src/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function testItWillThrowIfTheColumnAttributesCasterIsInvalid(): void
public function testItWillThrowBecauseTheObjectDoesNotHaveTypedProperties(): void
{
$this->expectException(MappingFailed::class);
$this->expectExceptionMessage('The property `temperature` must be typed with a single nullable type.');
$this->expectExceptionMessage('The property `temperature` must be typed.');

new Serializer(InvaliDWeatherWithRecordAttribute::class, ['temperature', 'foobar', 'observedOn']);
}
Expand Down

0 comments on commit 43273df

Please sign in to comment.