master

laravel/framework

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

ComponentSlot.php

TLDR

This file defines the ComponentSlot class, which implements the Htmlable and Stringable interfaces. It represents a slot used in Blade component views.

Classes

ComponentSlot

The ComponentSlot class represents a slot used in Blade component views. It has the following properties:

  • $attributes: The slot attribute bag, an instance of Illuminate\View\ComponentAttributeBag.
  • $contents: The slot contents as a string.

The class provides the following methods:

  • __construct(string $contents = '', array $attributes = []): Constructor method that initializes the slot instance with the given contents and attributes.
  • withAttributes(array $attributes): ComponentSlot: Sets the extra attributes that the slot should make available and returns the slot instance.
  • toHtml(): string: Returns the HTML string representation of the slot.
  • isEmpty(): bool: Checks if the slot is empty.
  • isNotEmpty(): bool: Checks if the slot is not empty.
  • __toString(): string: Returns the string representation of the slot.
<?php

namespace Illuminate\View;

use Illuminate\Contracts\Support\Htmlable;
use Stringable;

class ComponentSlot implements Htmlable, Stringable
{
    /**
     * The slot attribute bag.
     *
     * @var \Illuminate\View\ComponentAttributeBag
     */
    public $attributes;

    /**
     * The slot contents.
     *
     * @var string
     */
    protected $contents;

    /**
     * Create a new slot instance.
     *
     * @param  string  $contents
     * @param  array  $attributes
     * @return void
     */
    public function __construct($contents = '', $attributes = [])
    {
        $this->contents = $contents;

        $this->withAttributes($attributes);
    }

    /**
     * Set the extra attributes that the slot should make available.
     *
     * @param  array  $attributes
     * @return $this
     */
    public function withAttributes(array $attributes)
    {
        $this->attributes = new ComponentAttributeBag($attributes);

        return $this;
    }

    /**
     * Get the slot's HTML string.
     *
     * @return string
     */
    public function toHtml()
    {
        return $this->contents;
    }

    /**
     * Determine if the slot is empty.
     *
     * @return bool
     */
    public function isEmpty()
    {
        return $this->contents === '';
    }

    /**
     * Determine if the slot is not empty.
     *
     * @return bool
     */
    public function isNotEmpty()
    {
        return ! $this->isEmpty();
    }

    /**
     * Get the slot's HTML string.
     *
     * @return string
     */
    public function __toString()
    {
        return $this->toHtml();
    }
}