Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Support;
4
 
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Contracts\Support\Htmlable;
7
use Illuminate\Contracts\Support\Jsonable;
8
use JsonSerializable;
9
 
10
class Js implements Htmlable
11
{
12
    /**
13
     * The JavaScript string.
14
     *
15
     * @var string
16
     */
17
    protected $js;
18
 
19
    /**
20
     * Flags that should be used when encoding to JSON.
21
     *
22
     * @var int
23
     */
24
    protected const REQUIRED_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_THROW_ON_ERROR;
25
 
26
    /**
27
     * Create a new class instance.
28
     *
29
     * @param  mixed  $data
30
     * @param  int|null  $flags
31
     * @param  int  $depth
32
     * @return void
33
     *
34
     * @throws \JsonException
35
     */
36
    public function __construct($data, $flags = 0, $depth = 512)
37
    {
38
        $this->js = $this->convertDataToJavaScriptExpression($data, $flags, $depth);
39
    }
40
 
41
    /**
42
     * Create a new JavaScript string from the given data.
43
     *
44
     * @param  mixed  $data
45
     * @param  int  $flags
46
     * @param  int  $depth
47
     * @return static
48
     *
49
     * @throws \JsonException
50
     */
51
    public static function from($data, $flags = 0, $depth = 512)
52
    {
53
        return new static($data, $flags, $depth);
54
    }
55
 
56
    /**
57
     * Convert the given data to a JavaScript expression.
58
     *
59
     * @param  mixed  $data
60
     * @param  int  $flags
61
     * @param  int  $depth
62
     * @return string
63
     *
64
     * @throws \JsonException
65
     */
66
    protected function convertDataToJavaScriptExpression($data, $flags = 0, $depth = 512)
67
    {
68
        if ($data instanceof self) {
69
            return $data->toHtml();
70
        }
71
 
72
        $json = $this->jsonEncode($data, $flags, $depth);
73
 
74
        if (is_string($data)) {
75
            return "'".substr($json, 1, -1)."'";
76
        }
77
 
78
        return $this->convertJsonToJavaScriptExpression($json, $flags);
79
    }
80
 
81
    /**
82
     * Encode the given data as JSON.
83
     *
84
     * @param  mixed  $data
85
     * @param  int  $flags
86
     * @param  int  $depth
87
     * @return string
88
     *
89
     * @throws \JsonException
90
     */
91
    protected function jsonEncode($data, $flags = 0, $depth = 512)
92
    {
93
        if ($data instanceof Jsonable) {
94
            return $data->toJson($flags | static::REQUIRED_FLAGS);
95
        }
96
 
97
        if ($data instanceof Arrayable && ! ($data instanceof JsonSerializable)) {
98
            $data = $data->toArray();
99
        }
100
 
101
        return json_encode($data, $flags | static::REQUIRED_FLAGS, $depth);
102
    }
103
 
104
    /**
105
     * Convert the given JSON to a JavaScript expression.
106
     *
107
     * @param  string  $json
108
     * @param  int  $flags
109
     * @return string
110
     *
111
     * @throws \JsonException
112
     */
113
    protected function convertJsonToJavaScriptExpression($json, $flags = 0)
114
    {
115
        if ($json === '[]' || $json === '{}') {
116
            return $json;
117
        }
118
 
119
        if (Str::startsWith($json, ['"', '{', '['])) {
120
            return "JSON.parse('".substr(json_encode($json, $flags | static::REQUIRED_FLAGS), 1, -1)."')";
121
        }
122
 
123
        return $json;
124
    }
125
 
126
    /**
127
     * Get the string representation of the data for use in HTML.
128
     *
129
     * @return string
130
     */
131
    public function toHtml()
132
    {
133
        return $this->js;
134
    }
135
 
136
    /**
137
     * Get the string representation of the data for use in HTML.
138
     *
139
     * @return string
140
     */
141
    public function __toString()
142
    {
143
        return $this->toHtml();
144
    }
145
}