| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Carbon; |
| 15 |
|
| 16 |
use Closure; |
| 17 |
use DateInterval; |
| 18 |
use DatePeriod; |
| 19 |
use DateTime; |
| 20 |
use DateTimeInterface; |
| 21 |
use DateTimeZone; |
| 22 |
use ReflectionFunction; |
| 23 |
use ReflectionNamedType; |
| 24 |
use ReflectionType; |
| 25 |
|
| 26 |
final class Callback |
| 27 |
{ |
| 28 |
private ?ReflectionFunction $function; |
| 29 |
|
| 30 |
private function __construct(private readonly Closure $closure) |
| 31 |
{ |
| 32 |
} |
| 33 |
|
| 34 |
public static function fromClosure(Closure $closure): self |
| 35 |
{ |
| 36 |
return new self($closure); |
| 37 |
} |
| 38 |
|
| 39 |
public static function parameter(mixed $closure, mixed $value, string|int $index = 0): mixed |
| 40 |
{ |
| 41 |
if ($closure instanceof Closure) { |
| 42 |
return self::fromClosure($closure)->prepareParameter($value, $index); |
| 43 |
} |
| 44 |
|
| 45 |
return $value; |
| 46 |
} |
| 47 |
|
| 48 |
public function getReflectionFunction(): ReflectionFunction |
| 49 |
{ |
| 50 |
return $this->function ??= new ReflectionFunction($this->closure); |
| 51 |
} |
| 52 |
|
| 53 |
public function prepareParameter(mixed $value, string|int $index = 0): mixed |
| 54 |
{ |
| 55 |
$type = $this->getParameterType($index); |
| 56 |
|
| 57 |
if (!($type instanceof ReflectionNamedType)) { |
| 58 |
return $value; |
| 59 |
} |
| 60 |
|
| 61 |
$name = $type->getName(); |
| 62 |
|
| 63 |
if ($name === CarbonInterface::class) { |
| 64 |
$name = $value instanceof DateTime ? Carbon::class : CarbonImmutable::class; |
| 65 |
} |
| 66 |
|
| 67 |
if (!class_exists($name) || is_a($value, $name)) { |
| 68 |
return $value; |
| 69 |
} |
| 70 |
|
| 71 |
$class = $this->getPromotedClass($value); |
| 72 |
|
| 73 |
if ($class && is_a($name, $class, true)) { |
| 74 |
return $name::instance($value); |
| 75 |
} |
| 76 |
|
| 77 |
return $value; |
| 78 |
} |
| 79 |
|
| 80 |
public function call(mixed ...$arguments): mixed |
| 81 |
{ |
| 82 |
foreach ($arguments as $index => &$value) { |
| 83 |
if ($this->getPromotedClass($value)) { |
| 84 |
$value = $this->prepareParameter($value, $index); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return ($this->closure)(...$arguments); |
| 89 |
} |
| 90 |
|
| 91 |
private function getParameterType(string|int $index): ?ReflectionType |
| 92 |
{ |
| 93 |
$parameters = $this->getReflectionFunction()->getParameters(); |
| 94 |
|
| 95 |
if (\is_int($index)) { |
| 96 |
return ($parameters[$index] ?? null)?->getType(); |
| 97 |
} |
| 98 |
|
| 99 |
foreach ($parameters as $parameter) { |
| 100 |
if ($parameter->getName() === $index) { |
| 101 |
return $parameter->getType(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
private function getPromotedClass(mixed $value): ?string |
| 110 |
{ |
| 111 |
if ($value instanceof DateTimeInterface) { |
| 112 |
return CarbonInterface::class; |
| 113 |
} |
| 114 |
|
| 115 |
if ($value instanceof DateInterval) { |
| 116 |
return CarbonInterval::class; |
| 117 |
} |
| 118 |
|
| 119 |
if ($value instanceof DatePeriod) { |
| 120 |
return CarbonPeriod::class; |
| 121 |
} |
| 122 |
|
| 123 |
if ($value instanceof DateTimeZone) { |
| 124 |
return CarbonTimeZone::class; |
| 125 |
} |
| 126 |
|
| 127 |
return null; |
| 128 |
} |
| 129 |
} |