master

laravel/framework

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

Js.php

TLDR

This file defines a class called Js in the Illuminate\Support namespace. The Js class implements the Htmlable and Stringable interfaces. It provides methods for converting data to a JavaScript expression, encoding data as JSON, and converting JSON to a JavaScript expression.

Methods

__construct

The __construct method is the constructor of the Js class. It initializes the $js property by converting the given data to a JavaScript expression using the convertDataToJavaScriptExpression method.

from

The from method is a static method that creates a new instance of the Js class from the given data. It uses the __construct method to initialize the instance.

convertDataToJavaScriptExpression

The convertDataToJavaScriptExpression method converts the given data to a JavaScript expression. It checks if the data is an instance of the Js class or a BackedEnum. If it is, it calls the toHtml method or uses the value property of the BackedEnum. Then, it encodes the data as JSON using the encode method. If the data is a string, it removes the quotes from the encoded JSON. Finally, it calls the convertJsonToJavaScriptExpression method to convert the JSON to a JavaScript expression.

encode

The encode method encodes the given data as JSON. If the data is an instance of Jsonable, it calls the toJson method. If the data is an instance of Arrayable and not JsonSerializable, it converts it to an array. Then, it calls the json_encode function with the required flags to encode the data as JSON.

convertJsonToJavaScriptExpression

The convertJsonToJavaScriptExpression method converts the given JSON to a JavaScript expression. If the JSON is an empty array or object, it returns the JSON as is. Otherwise, it checks if the JSON starts with a quote, curly brace, or square bracket. It then removes the surrounding quotes from the JSON and calls JSON.parse on it.

toHtml

The toHtml method returns the JavaScript expression as a string.

__toString

The __toString method returns the JavaScript expression as a string. It is an alias for the toHtml method.

Classes

No classes found in the file.

<?php

namespace Illuminate\Support;

use BackedEnum;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
use Stringable;

class Js implements Htmlable, Stringable
{
    /**
     * The JavaScript string.
     *
     * @var string
     */
    protected $js;

    /**
     * Flags that should be used when encoding to JSON.
     *
     * @var int
     */
    protected const REQUIRED_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_THROW_ON_ERROR;

    /**
     * Create a new class instance.
     *
     * @param  mixed  $data
     * @param  int|null  $flags
     * @param  int  $depth
     * @return void
     *
     * @throws \JsonException
     */
    public function __construct($data, $flags = 0, $depth = 512)
    {
        $this->js = $this->convertDataToJavaScriptExpression($data, $flags, $depth);
    }

    /**
     * Create a new JavaScript string from the given data.
     *
     * @param  mixed  $data
     * @param  int  $flags
     * @param  int  $depth
     * @return static
     *
     * @throws \JsonException
     */
    public static function from($data, $flags = 0, $depth = 512)
    {
        return new static($data, $flags, $depth);
    }

    /**
     * Convert the given data to a JavaScript expression.
     *
     * @param  mixed  $data
     * @param  int  $flags
     * @param  int  $depth
     * @return string
     *
     * @throws \JsonException
     */
    protected function convertDataToJavaScriptExpression($data, $flags = 0, $depth = 512)
    {
        if ($data instanceof self) {
            return $data->toHtml();
        }

        if ($data instanceof BackedEnum) {
            $data = $data->value;
        }

        $json = static::encode($data, $flags, $depth);

        if (is_string($data)) {
            return "'".substr($json, 1, -1)."'";
        }

        return $this->convertJsonToJavaScriptExpression($json, $flags);
    }

    /**
     * Encode the given data as JSON.
     *
     * @param  mixed  $data
     * @param  int  $flags
     * @param  int  $depth
     * @return string
     *
     * @throws \JsonException
     */
    public static function encode($data, $flags = 0, $depth = 512)
    {
        if ($data instanceof Jsonable) {
            return $data->toJson($flags | static::REQUIRED_FLAGS);
        }

        if ($data instanceof Arrayable && ! ($data instanceof JsonSerializable)) {
            $data = $data->toArray();
        }

        return json_encode($data, $flags | static::REQUIRED_FLAGS, $depth);
    }

    /**
     * Convert the given JSON to a JavaScript expression.
     *
     * @param  string  $json
     * @param  int  $flags
     * @return string
     *
     * @throws \JsonException
     */
    protected function convertJsonToJavaScriptExpression($json, $flags = 0)
    {
        if ($json === '[]' || $json === '{}') {
            return $json;
        }

        if (Str::startsWith($json, ['"', '{', '['])) {
            return "JSON.parse('".substr(json_encode($json, $flags | static::REQUIRED_FLAGS), 1, -1)."')";
        }

        return $json;
    }

    /**
     * Get the string representation of the data for use in HTML.
     *
     * @return string
     */
    public function toHtml()
    {
        return $this->js;
    }

    /**
     * Get the string representation of the data for use in HTML.
     *
     * @return string
     */
    public function __toString()
    {
        return $this->toHtml();
    }
}