Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

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