Skip to content

Commit

Permalink
Improve Cell DX usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 23, 2023
1 parent f542eb6 commit 5d2dfcf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
12 changes: 6 additions & 6 deletions docs/9.0/reader/record-mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ use League\Csv\Serializer;
use Carbon\CarbonImmutable;

#[Serializer\Cell(
offset:'date',
column:'date',
cast:Serializer\CastToDate::class,
castArguments: [
'format' => '!Y-m-d',
Expand All @@ -146,7 +146,7 @@ It can be used on class properties and methods regardless of their visibility an

The attribute can take up to three (3) arguments which are all optional:

- The `offset` argument tells the engine which record key to use via its numeric or name offset. If not present the property name or the name of the first argument of the `setter` method will be used. In such case, you are required to specify the property names information.
- The `column` argument tells the engine which record key to use via its numeric or name. If not present the property name or the name of the first argument of the `setter` method will be used. In such case, you are required to specify the property names information.
- The `cast` argument which accept the name of a class implementing the `TypeCasting` interface and responsible for type casting the record value. If not present, the mechanism will try to resolve the typecasting based on the property or method argument type.
- The `castArguments` argument enables controlling typecasting by providing extra arguments to the `TypeCasting` class constructor. The argument expects an associative array and relies on named arguments to inject its value to the `TypeCasting` implementing class constructor.

Expand Down Expand Up @@ -243,7 +243,7 @@ is not `null` and the value given is incorrect, the mechanism will throw an exce
use League\Csv\Serializer\Cell;

#[Cell(
offset:1,
column:1,
cast:Serializer\CastToEnum::class,
castArguments: ['default' => 'Abidjan', 'className' => Place::class]
)]
Expand All @@ -254,7 +254,7 @@ public function setPlace(mixed $place): void
}
```

> convert the value of the array whose offset is `1` into a `Place` Enum
> convert the value of the array whose key is `1` into a `Place` Enum
> if the value is null resolve the string `Abidjan` to `Place::Abidjan`. Once created,
> call the method `setPlace` with the created `Place` enum filling the `$place` argument.
Expand Down Expand Up @@ -369,7 +369,7 @@ To do so specify your casting with the attribute:
use App\Domain\Money
use League\Csv\Serializer;

#[Serializer\Cell(offset: 'amount', castArguments: ['default' => 1000_00])]
#[Serializer\Cell(column: 'amount', castArguments: ['default' => 1000_00])]
private ?Naira $amount;
```

Expand Down Expand Up @@ -426,7 +426,7 @@ use App\Domain\Money\Naira;
use League\Csv\Serializer;

#[Serializer\Cell(
offset: 'amount',
column: 'amount',
cast: App\Domain\Money\CastToNaira::class,
castArguments: ['default' => 20_00]
)]
Expand Down
2 changes: 1 addition & 1 deletion src/Serializer/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class Cell
* @param ?class-string $cast
*/
public function __construct(
public readonly string|int|null $offset = null,
public readonly string|int|null $column = null,
public readonly ?string $cast = null,
public readonly array $castArguments = []
) {
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/Denormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private function autoDiscoverPropertySetter(ReflectionMethod|ReflectionProperty
}

/** @var int|false $offset */
/** @var ReflectionParameter|ReflectionParameter $reflectionProperty */
/** @var ReflectionParameter|ReflectionProperty $reflectionProperty */
[$offset, $reflectionProperty] = match (true) {
$accessor instanceof ReflectionMethod => [array_search($accessor->getName(), $methodNames, true), $accessor->getParameters()[0]],
$accessor instanceof ReflectionProperty => [array_search($accessor->getName(), $propertyNames, true), $accessor],
Expand Down Expand Up @@ -275,7 +275,7 @@ private function findPropertySetter(Cell $cell, ReflectionMethod|ReflectionPrope
throw MappingFailed::dueToInvalidTypeCastingClass($typeCaster);
}

$offset = $cell->offset ?? match (true) {
$offset = $cell->column ?? match (true) {
$accessor instanceof ReflectionMethod => $this->getMethodFirstArgument($accessor)->getName(),
$accessor instanceof ReflectionProperty => $accessor->getName(),
};
Expand Down
40 changes: 20 additions & 20 deletions src/Serializer/DenormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(
public readonly float $temperature,
public readonly Place $place,
#[Cell(
offset: 'date',
column: 'date',
castArguments: ['format' => '!Y-m-d', 'timezone' => 'Africa/Kinshasa'],
)]
public readonly DateTimeInterface $observedOn
Expand Down Expand Up @@ -74,7 +74,7 @@ public function __construct(
public readonly float $temperature,
public readonly Place $place,
#[Cell(
offset: 'date',
column: 'date',
castArguments: ['format' => '!Y-m-d', 'timezone' => 'Africa/Kinshasa'],
)]
public readonly DateTimeInterface $observedOn
Expand All @@ -100,12 +100,12 @@ public function testItConvertsARecordsToAnObjectUsingProperties(): void

$foobar = new class (3, Place::Yamoussokro, new DateTimeImmutable()) {
public function __construct(
#[Cell(offset:'temperature')]
#[Cell(column:'temperature')]
public readonly float $temperature,
#[Cell(offset:2, cast: CastToEnum::class)]
#[Cell(column:2, cast: CastToEnum::class)]
public readonly Place $place,
#[Cell(
offset: 'date',
column: 'date',
cast: CastToDate::class,
castArguments: ['format' => '!Y-m-d', 'timezone' => 'Africa/Kinshasa'],
)]
Expand All @@ -128,18 +128,18 @@ public function testItConvertsARecordsToAnObjectUsingMethods(): void
private float $temperature;

public function __construct(
#[Cell(offset:2, cast: CastToEnum::class)]
#[Cell(column:2, cast: CastToEnum::class)]
public readonly Place $place,
#[Cell(
offset: 'date',
column: 'date',
cast: CastToDate::class,
castArguments: ['format' => '!Y-m-d', 'timezone' => 'Africa/Kinshasa'],
)]
private readonly DateTime $observedOn
) {
}

#[Cell(offset:'temperature')]
#[Cell(column:'temperature')]
public function setTemperature(float $temperature): void
{
$this->temperature = $temperature;
Expand Down Expand Up @@ -184,18 +184,18 @@ public function testItWillThrowIfTheHeaderIsMissingAndTheColumnOffsetIsAString()
private float $temperature;

public function __construct(
#[Cell(offset:2, cast: CastToEnum::class)]
#[Cell(column:2, cast: CastToEnum::class)]
public readonly Place $place,
#[Cell(
offset: 'date',
column: 'date',
cast: CastToDate::class,
castArguments: ['format' => '!Y-m-d', 'timezone' => 'Africa/Kinshasa'],
)]
private readonly DateTime $observedOn
) {
}

#[Cell(offset:'temperature')]
#[Cell(column:'temperature')]
public function setTemperature(float $temperature): void
{
$this->temperature = $temperature;
Expand Down Expand Up @@ -229,18 +229,18 @@ public function testItWillThrowIfTheHeaderContainsInvalidOffsetName(): void
private float $temperature;

public function __construct(
#[Cell(offset:2, cast: CastToEnum::class)]
#[Cell(column:2, cast: CastToEnum::class)]
public readonly Place $place,
#[Cell(
offset: 'date',
column: 'date',
cast: CastToDate::class,
castArguments: ['format' => '!Y-m-d', 'timezone' => 'Africa/Kinshasa'],
)]
private readonly DateTime $observedOn
) {
}

#[Cell(offset:'temperature')]
#[Cell(column:'temperature')]
public function setTemperature(float $temperature): void
{
$this->temperature = $temperature;
Expand Down Expand Up @@ -272,12 +272,12 @@ public function testItWillThrowIfTheColumnAttributesIsUsedMultipleTimeForTheSame
{
$class = new class (5, Place::Abidjan, new DateTimeImmutable()) {
public function __construct(
#[Cell(offset:'temperature'), Cell(offset:'date')] /* @phpstan-ignore-line */
#[Cell(column:'temperature'), Cell(column:'date')] /* @phpstan-ignore-line */
public readonly float $temperature,
#[Cell(offset:2, cast: CastToEnum::class)]
#[Cell(column:2, cast: CastToEnum::class)]
public readonly Place $place,
#[Cell(
offset: 'date',
column: 'date',
cast: CastToDate::class,
castArguments: ['format' => '!Y-m-d', 'timezone' => 'Africa/Kinshasa'],
)]
Expand All @@ -296,12 +296,12 @@ public function testItWillThrowIfTheColumnAttributesCasterIsInvalid(): void
{
$foobar = new class (5, Place::Yamoussokro, new DateTimeImmutable()) {
public function __construct(
#[Cell(offset:'temperature', cast: stdClass::class)]
#[Cell(column:'temperature', cast: stdClass::class)]
public readonly float $temperature,
#[Cell(offset:2, cast: CastToEnum::class)]
#[Cell(column:2, cast: CastToEnum::class)]
public readonly Place $place,
#[Cell(
offset: 'date',
column: 'date',
cast: CastToDate::class,
castArguments: ['format' => '!Y-m-d', 'timezone' => 'Africa/Kinshasa'],
)]
Expand Down

0 comments on commit 5d2dfcf

Please sign in to comment.