Skip to content

Commit

Permalink
v2.19.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Savluk committed Oct 11, 2024
1 parent 0a1d064 commit 4eb3710
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
32 changes: 22 additions & 10 deletions src/Support/Mapping/Casts/ScopeCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ class ScopeCast extends Cast implements ForwardedCast
{
public function __construct(
protected readonly Cast $cast,
protected readonly string|Closure $accessor
protected readonly string|Closure|null $accessor
) {
}

public function nestedCast(): Cast
public function nestedCast(?Closure $callback = null): Cast|static
{
if ($callback) {
$callback(
$this->nestedCast()
);

return $this;
}

return $this->cast;
}

Expand All @@ -29,14 +37,18 @@ public function resolve(mixed $source, array $sourcesTrace = []): mixed

protected function finalize(mixed $source, array $sourcesTrace): mixed
{
$value = static::resolveValueFromAccessor(
$this->accessor,
$source,
$sourcesTrace
);

if ($this->accessor && last($sourcesTrace) !== $source) {
$sourcesTrace[] = $source;
if ($this->accessor) {
$value = static::resolveValueFromAccessor(
$this->accessor,
$source,
$sourcesTrace
);

if (last($sourcesTrace) !== $source) {
$sourcesTrace[] = $source;
}
} else {
$value = $source;
}

return $this->cast->resolve($value, $sourcesTrace);
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Mapping/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static function oneOfConst(array $values, string|Closure|null $accessor =
return new OneOfConstCast($values, $accessor);
}

public static function scope(Cast $cast, string|Closure $accessor): ScopeCast
public static function scope(Cast $cast, string|Closure|null $accessor): ScopeCast
{
return new ScopeCast($cast, $accessor);
}
Expand Down

0 comments on commit 4eb3710

Please sign in to comment.