Skip to content

Commit bbf47d5

Browse files
committed
rename class
1 parent 4965ce3 commit bbf47d5

File tree

2 files changed

+50
-52
lines changed

2 files changed

+50
-52
lines changed

src/Illuminate/Support/JsString.php renamed to src/Illuminate/Support/Js.php

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,24 @@
77
use Illuminate\Contracts\Support\Jsonable;
88
use JsonSerializable;
99

10-
class JsString implements Htmlable
10+
class Js implements Htmlable
1111
{
1212
/**
13-
* Flags that must always be used when encoding to JSON for JsString.
14-
*
15-
* @var int
16-
*/
17-
protected const REQUIRED_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_THROW_ON_ERROR;
18-
19-
/**
20-
* The javascript string.
13+
* The JavaScript string.
2114
*
2215
* @var string
2316
*/
2417
protected $js;
2518

2619
/**
27-
* Create a new JsString from data.
28-
*
29-
* @param mixed $data
30-
* @param int $flags
31-
* @param int $depth
32-
* @return static
20+
* Flags that should be used when encoding to JSON.
3321
*
34-
* @throws \JsonException
22+
* @var int
3523
*/
36-
public static function from($data, $flags = 0, $depth = 512)
37-
{
38-
return new static($data, $flags, $depth);
39-
}
24+
protected const REQUIRED_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_THROW_ON_ERROR;
4025

4126
/**
42-
* Create a new JsString.
27+
* Create a new class instance.
4328
*
4429
* @param mixed $data
4530
* @param int|null $flags
@@ -54,27 +39,22 @@ public function __construct($data, $flags = 0, $depth = 512)
5439
}
5540

5641
/**
57-
* Get string representation of data for use in HTML.
42+
* Create a new JavaScript string from the given data.
5843
*
59-
* @return string
60-
*/
61-
public function toHtml()
62-
{
63-
return $this->js;
64-
}
65-
66-
/**
67-
* Get string representation of data for use in HTML.
44+
* @param mixed $data
45+
* @param int $flags
46+
* @param int $depth
47+
* @return static
6848
*
69-
* @return string
49+
* @throws \JsonException
7050
*/
71-
public function __toString()
51+
public static function from($data, $flags = 0, $depth = 512)
7252
{
73-
return $this->toHtml();
53+
return new static($data, $flags, $depth);
7454
}
7555

7656
/**
77-
* Convert data to a JavaScript expression.
57+
* Convert the given data to a JavaScript expression.
7858
*
7959
* @param mixed $data
8060
* @param int $flags
@@ -99,7 +79,7 @@ protected function convertDataToJavaScriptExpression($data, $flags = 0, $depth =
9979
}
10080

10181
/**
102-
* Encode data as JSON.
82+
* Encode the given data as JSON.
10383
*
10484
* @param mixed $data
10585
* @param int $flags
@@ -122,7 +102,7 @@ protected function jsonEncode($data, $flags = 0, $depth = 512)
122102
}
123103

124104
/**
125-
* Convert JSON to a JavaScript expression.
105+
* Convert the given JSON to a JavaScript expression.
126106
*
127107
* @param string $json
128108
* @param int $flags
@@ -137,11 +117,29 @@ protected function convertJsonToJavaScriptExpression($json, $flags = 0)
137117
}
138118

139119
if (Str::startsWith($json, ['"', '{', '['])) {
140-
$json = json_encode($json, $flags | static::REQUIRED_FLAGS);
141-
142-
return "JSON.parse('".substr($json, 1, -1)."')";
120+
return "JSON.parse('".substr(json_encode($json, $flags | static::REQUIRED_FLAGS), 1, -1)."')";
143121
}
144122

145123
return $json;
146124
}
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+
}
147145
}

tests/Support/SupportJsStringTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,42 @@
44

55
use Illuminate\Contracts\Support\Arrayable;
66
use Illuminate\Contracts\Support\Jsonable;
7-
use Illuminate\Support\JsString;
7+
use Illuminate\Support\Js;
88
use JsonSerializable;
99
use PHPUnit\Framework\TestCase;
1010

1111
class SupportJsStringTest extends TestCase
1212
{
1313
public function testScalars()
1414
{
15-
$this->assertEquals('false', (string) JsString::from(false));
16-
$this->assertEquals('true', (string) JsString::from(true));
17-
$this->assertEquals('1', (string) JsString::from(1));
18-
$this->assertEquals('1.1', (string) JsString::from(1.1));
15+
$this->assertEquals('false', (string) Js::from(false));
16+
$this->assertEquals('true', (string) Js::from(true));
17+
$this->assertEquals('1', (string) Js::from(1));
18+
$this->assertEquals('1.1', (string) Js::from(1.1));
1919
$this->assertEquals(
2020
"'\\u003Cdiv class=\\u0022foo\\u0022\\u003E\\u0027quoted html\\u0027\\u003C\\/div\\u003E'",
21-
(string) JsString::from('<div class="foo">\'quoted html\'</div>')
21+
(string) Js::from('<div class="foo">\'quoted html\'</div>')
2222
);
2323
}
2424

2525
public function testArrays()
2626
{
2727
$this->assertEquals(
2828
"JSON.parse('[\\u0022hello\\u0022,\\u0022world\\u0022]')",
29-
(string) JsString::from(['hello', 'world'])
29+
(string) Js::from(['hello', 'world'])
3030
);
3131

3232
$this->assertEquals(
3333
"JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
34-
(string) JsString::from(['foo' => 'hello', 'bar' => 'world'])
34+
(string) Js::from(['foo' => 'hello', 'bar' => 'world'])
3535
);
3636
}
3737

3838
public function testObjects()
3939
{
4040
$this->assertEquals(
4141
"JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
42-
(string) JsString::from((object) ['foo' => 'hello', 'bar' => 'world'])
42+
(string) Js::from((object) ['foo' => 'hello', 'bar' => 'world'])
4343
);
4444
}
4545

@@ -66,7 +66,7 @@ public function toArray()
6666

6767
$this->assertEquals(
6868
"JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
69-
(string) JsString::from($data)
69+
(string) Js::from($data)
7070
);
7171
}
7272

@@ -98,7 +98,7 @@ public function toArray()
9898

9999
$this->assertEquals(
100100
"JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
101-
(string) JsString::from($data)
101+
(string) Js::from($data)
102102
);
103103
}
104104

@@ -118,7 +118,7 @@ public function toArray()
118118

119119
$this->assertEquals(
120120
"JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')",
121-
(string) JsString::from($data)
121+
(string) Js::from($data)
122122
);
123123
}
124124
}

0 commit comments

Comments
 (0)