Skip to content

Commit

Permalink
Improve Serializer internal codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 13, 2023
1 parent 339c89e commit a00ef2c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs/9.0/reader/record-mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ To allow your object to cast the cell value to your liking it needs to implement
To do so, you must define a `toVariable` method that will return the correct value once converted.

<p class="message-warning"><strong>Of note</strong> The class constructor method must take the property type value as
one of its argument with the name <code>$propertyTyoe</code>. This means you <strong>can not</strong> use the
one of its argument with the name <code>$propertyType</code>. This means you <strong>can not</strong> use the
<code>propertyType</code> as a possible key of the associative array given to <code>castArguments</code></p>

```php
Expand Down
41 changes: 18 additions & 23 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,52 +157,47 @@ private function findPropertySetters(array $propertyNames): array
$propertySetters = [];
foreach ($this->class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
if ($property->isStatic()) {
//the property can not be cast yet
//as casting may be set using the Cell attribute
continue;
}

$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;
}

$propertyName = $property->getName();
/** @var int|false $offset */
$offset = array_search($propertyName, $propertyNames, true);
$offset = array_search($property->getName(), $propertyNames, true);
if (false === $offset) {
//the property is not in the record header
//we can not throw as it may be set via a
//setter method using the Cell attribute
continue;
}

$type = $property->getType();
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 for `'.$type.'` was found for property `'.$propertyName.'`.');
}

$propertySetters[] = new PropertySetter($property, $offset, $cast);
$propertySetters[] = $this->autoDiscoverPropertySetter($property, $offset);
}

$propertySetters = [...$propertySetters, ...$this->findPropertySettersByCellAttribute($propertyNames)];

//if converters is empty it means the Serializer
//was unable to detect properties to assign
if ([] === $propertySetters) {
throw new MappingFailed('No properties or method setters were found eligible on the class `'.$this->class->getName().'` to be used for type casting.');
}

return $propertySetters;
}

private function autoDiscoverPropertySetter(ReflectionProperty $property, int $offset): PropertySetter
{
$propertyName = $property->getName();
$type = $property->getType();
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 for `'.$type.' $'.$propertyName.'`.');
}

return new PropertySetter($property, $offset, $cast);
}

/**
* @param array<string> $propertyNames
*
Expand Down
2 changes: 1 addition & 1 deletion src/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function testItWillThrowBecauseTheObjectDoesNotHaveTypedProperties(): voi
public function testItWillFailForLackOfTypeCasting(): void
{
$this->expectException(MappingFailed::class);
$this->expectExceptionMessage('No valid type casting for `SplFileObject` was found for property `observedOn`');
$this->expectExceptionMessage('No valid type casting for `SplFileObject $observedOn`.');

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

0 comments on commit a00ef2c

Please sign in to comment.