master

laravel/framework

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

HtmlString.php

TLDR

The HtmlString class is a part of the Illuminate\Support namespace and implements the Htmlable and Stringable contracts. It represents an HTML string and provides methods for retrieving the HTML string and checking if it is empty or not.

Classes

HtmlString

The HtmlString class represents an HTML string. It has the following methods:

  • __construct($html = ''): Creates a new HTML string instance. The $html parameter is optional and can be used to set the initial HTML string.
  • toHtml(): Returns the HTML string.
  • isEmpty(): Determines if the HTML string is empty. Returns true if the string is empty, false otherwise.
  • isNotEmpty(): Determines if the HTML string is not empty. Returns true if the string is not empty, false otherwise.
  • __toString(): Returns the HTML string. This method is automatically called when the object is used as a string.
<?php

namespace Illuminate\Support;

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

class HtmlString implements Htmlable, Stringable
{
    /**
     * The HTML string.
     *
     * @var string
     */
    protected $html;

    /**
     * Create a new HTML string instance.
     *
     * @param  string  $html
     * @return void
     */
    public function __construct($html = '')
    {
        $this->html = $html;
    }

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

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

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

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