Skip to content

[12.x] Ability to perform higher order static calls on collection items #55880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

daniser
Copy link
Contributor

@daniser daniser commented May 28, 2025

This PR adds ability to perform higher order static calls on collection items (class names).

Example usage scenario:

interface Handler
{
    public static function satisfies($condition): bool;
}

class HandlerFactory
{
    protected Collection $handlers;

    /**
     * @param  list<class-string<Handler>>  $handlers
     */
    public function construct(array $handlers) // ❤️giveConfig
    {
        $this->handlers = collect($handlers);
    }

    public function make($condition): Handler
    {
        $handler = $this->handlers->first->satisfies($condition) ?? FallbackHandler::class;

        return new $handler;
    }
}

@taylorotwell taylorotwell merged commit 3d084e4 into laravel:12.x May 28, 2025
59 checks passed
@daniser daniser deleted the feature/higher-order-collection-static-call branch May 28, 2025 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants