Skip to content

Commit

Permalink
improve cast doc
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 20, 2025
1 parent 0bf31e9 commit 11ce817
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@
use Throwable;
use function Chevere\Message\message;

function cast(mixed $argument, string|int ...$key): CastInterface
/**
* Cast a variable to a CastInterface instance.
*
* @param mixed $variable The variable to cast.
* @param string|int ...$key The key to access in the array (array reduce)
*/
function cast(mixed $variable, string|int ...$key): CastInterface
{
if ($key !== []) {
if (! ($argument instanceof ArrayAccess || is_array($argument))) {
if (! ($variable instanceof ArrayAccess || is_array($variable))) {
throw new BadMethodCallException(
(string) message(
'Argument must be array-accessible, %type% provided',
type: gettype($argument)
type: gettype($variable)
)
);
}
Expand All @@ -65,10 +71,10 @@ function cast(mixed $argument, string|int ...$key): CastInterface
)
);
};
$argument = array_reduce($key, $fn, $argument);
$variable = array_reduce($key, $fn, $variable);
}

return new Cast($argument);
return new Cast($variable);
}

function null(
Expand Down

0 comments on commit 11ce817

Please sign in to comment.