master

laravel/framework

Last updated at: 29/12/2023 09:22

HigherOrderBuilderProxy.php

TLDR

This file defines a class called HigherOrderBuilderProxy in the Illuminate\Database\Eloquent namespace. This class is used to proxy scope calls onto the query builder.

Classes

HigherOrderBuilderProxy

This class is responsible for proxying scope calls onto the query builder. It has the following properties:

  • $builder: The collection being operated on, of type \Illuminate\Database\Eloquent\Builder.
  • $method: The method being proxied, of type string.

It has the following methods:

  • __construct(Builder $builder, string $method): This constructor method initializes the $builder and $method properties.
  • __call(string $method, array $parameters): This method proxies a scope call onto the query builder. It calls the specified method on the value, passing the parameters, and returns the result.
<?php

namespace Illuminate\Database\Eloquent;

/**
 * @mixin \Illuminate\Database\Eloquent\Builder
 */
class HigherOrderBuilderProxy
{
    /**
     * The collection being operated on.
     *
     * @var \Illuminate\Database\Eloquent\Builder
     */
    protected $builder;

    /**
     * The method being proxied.
     *
     * @var string
     */
    protected $method;

    /**
     * Create a new proxy instance.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
     * @param  string  $method
     * @return void
     */
    public function __construct(Builder $builder, $method)
    {
        $this->method = $method;
        $this->builder = $builder;
    }

    /**
     * Proxy a scope call onto the query builder.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->builder->{$this->method}(function ($value) use ($method, $parameters) {
            return $value->{$method}(...$parameters);
        });
    }
}