master

laravel/framework

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

AnonymousComponent.php

TLDR

The AnonymousComponent class in the Illuminate\View namespace extends the Component class. It represents an anonymous component and provides methods to render the component and retrieve the data for the associated view.

Classes

AnonymousComponent

The AnonymousComponent class extends the Component class and represents an anonymous component. It has the following properties:

  • $view: the component view.
  • $data: the component data.

The class provides the following methods:

  • __construct($view, $data): The constructor method initializes the $view and $data properties.
  • render(): This method returns the view associated with the component.
  • data(): This method returns the data that should be supplied to the view.
<?php

namespace Illuminate\View;

class AnonymousComponent extends Component
{
    /**
     * The component view.
     *
     * @var string
     */
    protected $view;

    /**
     * The component data.
     *
     * @var array
     */
    protected $data = [];

    /**
     * Create a new anonymous component instance.
     *
     * @param  string  $view
     * @param  array  $data
     * @return void
     */
    public function __construct($view, $data)
    {
        $this->view = $view;
        $this->data = $data;
    }

    /**
     * Get the view / view contents that represent the component.
     *
     * @return string
     */
    public function render()
    {
        return $this->view;
    }

    /**
     * Get the data that should be supplied to the view.
     *
     * @return array
     */
    public function data()
    {
        $this->attributes = $this->attributes ?: $this->newAttributeBag();

        return array_merge(
            ($this->data['attributes'] ?? null)?->getAttributes() ?: [],
            $this->attributes->getAttributes(),
            $this->data,
            ['attributes' => $this->attributes]
        );
    }
}