PHP 8.3.31
Preview: Callback.php Size: 3.05 KB
/home/getspomw/royalsquad.us/vendor/nesbot/carbon/src/Carbon/Callback.php

<?php

declare(strict_types=1);

/**
 * This file is part of the Carbon package.
 *
 * (c) Brian Nesbitt <brian@nesbot.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Carbon;

use Closure;
use DateInterval;
use DatePeriod;
use DateTime;
use DateTimeInterface;
use DateTimeZone;
use ReflectionFunction;
use ReflectionNamedType;
use ReflectionType;

final class Callback
{
    private ?ReflectionFunction $function;

    private function __construct(private readonly Closure $closure)
    {
    }

    public static function fromClosure(Closure $closure): self
    {
        return new self($closure);
    }

    public static function parameter(mixed $closure, mixed $value, string|int $index = 0): mixed
    {
        if ($closure instanceof Closure) {
            return self::fromClosure($closure)->prepareParameter($value, $index);
        }

        return $value;
    }

    public function getReflectionFunction(): ReflectionFunction
    {
        return $this->function ??= new ReflectionFunction($this->closure);
    }

    public function prepareParameter(mixed $value, string|int $index = 0): mixed
    {
        $type = $this->getParameterType($index);

        if (!($type instanceof ReflectionNamedType)) {
            return $value;
        }

        $name = $type->getName();

        if ($name === CarbonInterface::class) {
            $name = $value instanceof DateTime ? Carbon::class : CarbonImmutable::class;
        }

        if (!class_exists($name) || is_a($value, $name)) {
            return $value;
        }

        $class = $this->getPromotedClass($value);

        if ($class && is_a($name, $class, true)) {
            return $name::instance($value);
        }

        return $value;
    }

    public function call(mixed ...$arguments): mixed
    {
        foreach ($arguments as $index => &$value) {
            if ($this->getPromotedClass($value)) {
                $value = $this->prepareParameter($value, $index);
            }
        }

        return ($this->closure)(...$arguments);
    }

    private function getParameterType(string|int $index): ?ReflectionType
    {
        $parameters = $this->getReflectionFunction()->getParameters();

        if (\is_int($index)) {
            return ($parameters[$index] ?? null)?->getType();
        }

        foreach ($parameters as $parameter) {
            if ($parameter->getName() === $index) {
                return $parameter->getType();
            }
        }

        return null;
    }

    /** @return class-string|null */
    private function getPromotedClass(mixed $value): ?string
    {
        if ($value instanceof DateTimeInterface) {
            return CarbonInterface::class;
        }

        if ($value instanceof DateInterval) {
            return CarbonInterval::class;
        }

        if ($value instanceof DatePeriod) {
            return CarbonPeriod::class;
        }

        if ($value instanceof DateTimeZone) {
            return CarbonTimeZone::class;
        }

        return null;
    }
}

Directory Contents

Dirs: 8 × Files: 21

Name Size Perms Modified Actions
Cli DIR
- drwxrwxrwx 2025-09-17 06:53:16
Edit Download
- drwxrwxrwx 2026-04-21 08:55:54
Edit Download
Lang DIR
- drwxrwxrwx 2025-09-17 06:53:16
Edit Download
Laravel DIR
- drwxrwxrwx 2026-04-22 18:10:15
Edit Download
List DIR
- drwxrwxrwx 2025-09-17 06:53:16
Edit Download
- drwxrwxrwx 2025-09-17 06:53:16
Edit Download
PHPStan DIR
- drwxrwxrwx 2026-04-21 16:07:18
Edit Download
Traits DIR
- drwxrwxrwx 2025-09-17 06:53:17
Edit Download
11.10 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
3.05 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
156.66 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
443 B lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
157.70 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
306.58 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
116.53 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
84.15 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
856 B lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
8.91 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
10.81 KB lrw-r--r-- 2026-04-26 10:16:59
Edit Download
44.47 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
24.28 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
6.42 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
2.49 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
833 B lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
2.35 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
577 B lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
3.32 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
2.10 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download
5.09 KB lrw-rw-rw- 2025-09-17 06:53:13
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).