Skip to content

Commit

Permalink
v2.17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Savluk committed Oct 4, 2024
1 parent 856179e commit 9be8351
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/Exceptions/MappingFail.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ class MappingFail extends DTOException
{
public function __construct(
public readonly Mapper $mapper,
public readonly UnexpectedValue|InternalException|null $exception = null
public readonly UnexpectedValue|InternalException|string|null $exception = null
) {
parent::__construct(
sprintf(
'[%s]%s',
$mapper::class,
$exception ? " {$exception->getMessage()}" : ''
is_string($exception)
? $this->exception
: ($exception ? " {$exception->getMessage()}" : '')

),
previous: $exception
previous: ! is_string($exception) ? $exception : null
);
}
}
15 changes: 12 additions & 3 deletions src/Support/Mapping/Casts/KeyedArrayCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class KeyedArrayCast extends OptionalCast
*/
protected array|OneOfConstCast|EnumCast|StringCast|null $keyBy = null;

protected bool $nullIfEmpty = false;
protected bool $optionalIfEmpty = false;

protected bool $stdClassCastAllowed = false;

Expand Down Expand Up @@ -75,13 +75,22 @@ public function keySchema(OneOfConstCast|EnumCast|StringCast $cast): static

public function nullIfEmpty(): static
{
$this->nullIfEmpty = true;
$this->optionalIfEmpty = true;

$this->nullable();

return $this;
}

public function optionalIfEmpty(): static
{
$this->optionalIfEmpty = true;

$this->optional();

return $this;
}

protected function finalize(mixed $source, array $sourcesTrace): ?stdClass
{
$value = static::resolveValueFromAccessor(
Expand Down Expand Up @@ -145,7 +154,7 @@ protected function finalize(mixed $source, array $sourcesTrace): ?stdClass
}
}

return ! $hasValues && $this->nullIfEmpty ? null : $result;
return ! $hasValues && $this->optionalIfEmpty ? null : $result;
}

protected function types(): RecordType
Expand Down

0 comments on commit 9be8351

Please sign in to comment.