From bbf47d5507c0ff018763170988284eeca6021fe8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 5 Nov 2021 09:37:10 -0500 Subject: [PATCH] rename class --- .../Support/{JsString.php => Js.php} | 78 +++++++++---------- tests/Support/SupportJsStringTest.php | 24 +++--- 2 files changed, 50 insertions(+), 52 deletions(-) rename src/Illuminate/Support/{JsString.php => Js.php} (82%) diff --git a/src/Illuminate/Support/JsString.php b/src/Illuminate/Support/Js.php similarity index 82% rename from src/Illuminate/Support/JsString.php rename to src/Illuminate/Support/Js.php index b57727607173..6d6de3440d71 100644 --- a/src/Illuminate/Support/JsString.php +++ b/src/Illuminate/Support/Js.php @@ -7,39 +7,24 @@ use Illuminate\Contracts\Support\Jsonable; use JsonSerializable; -class JsString implements Htmlable +class Js implements Htmlable { /** - * Flags that must always be used when encoding to JSON for JsString. - * - * @var int - */ - protected const REQUIRED_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_THROW_ON_ERROR; - - /** - * The javascript string. + * The JavaScript string. * * @var string */ protected $js; /** - * Create a new JsString from data. - * - * @param mixed $data - * @param int $flags - * @param int $depth - * @return static + * Flags that should be used when encoding to JSON. * - * @throws \JsonException + * @var int */ - public static function from($data, $flags = 0, $depth = 512) - { - return new static($data, $flags, $depth); - } + protected const REQUIRED_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_THROW_ON_ERROR; /** - * Create a new JsString. + * Create a new class instance. * * @param mixed $data * @param int|null $flags @@ -54,27 +39,22 @@ public function __construct($data, $flags = 0, $depth = 512) } /** - * Get string representation of data for use in HTML. + * Create a new JavaScript string from the given data. * - * @return string - */ - public function toHtml() - { - return $this->js; - } - - /** - * Get string representation of data for use in HTML. + * @param mixed $data + * @param int $flags + * @param int $depth + * @return static * - * @return string + * @throws \JsonException */ - public function __toString() + public static function from($data, $flags = 0, $depth = 512) { - return $this->toHtml(); + return new static($data, $flags, $depth); } /** - * Convert data to a JavaScript expression. + * Convert the given data to a JavaScript expression. * * @param mixed $data * @param int $flags @@ -99,7 +79,7 @@ protected function convertDataToJavaScriptExpression($data, $flags = 0, $depth = } /** - * Encode data as JSON. + * Encode the given data as JSON. * * @param mixed $data * @param int $flags @@ -122,7 +102,7 @@ protected function jsonEncode($data, $flags = 0, $depth = 512) } /** - * Convert JSON to a JavaScript expression. + * Convert the given JSON to a JavaScript expression. * * @param string $json * @param int $flags @@ -137,11 +117,29 @@ protected function convertJsonToJavaScriptExpression($json, $flags = 0) } if (Str::startsWith($json, ['"', '{', '['])) { - $json = json_encode($json, $flags | static::REQUIRED_FLAGS); - - return "JSON.parse('".substr($json, 1, -1)."')"; + 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(); + } } diff --git a/tests/Support/SupportJsStringTest.php b/tests/Support/SupportJsStringTest.php index 6f63ab69cd0e..dfce3c458b62 100644 --- a/tests/Support/SupportJsStringTest.php +++ b/tests/Support/SupportJsStringTest.php @@ -4,7 +4,7 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; -use Illuminate\Support\JsString; +use Illuminate\Support\Js; use JsonSerializable; use PHPUnit\Framework\TestCase; @@ -12,13 +12,13 @@ class SupportJsStringTest extends TestCase { public function testScalars() { - $this->assertEquals('false', (string) JsString::from(false)); - $this->assertEquals('true', (string) JsString::from(true)); - $this->assertEquals('1', (string) JsString::from(1)); - $this->assertEquals('1.1', (string) JsString::from(1.1)); + $this->assertEquals('false', (string) Js::from(false)); + $this->assertEquals('true', (string) Js::from(true)); + $this->assertEquals('1', (string) Js::from(1)); + $this->assertEquals('1.1', (string) Js::from(1.1)); $this->assertEquals( "'\\u003Cdiv class=\\u0022foo\\u0022\\u003E\\u0027quoted html\\u0027\\u003C\\/div\\u003E'", - (string) JsString::from('
\'quoted html\'
') + (string) Js::from('
\'quoted html\'
') ); } @@ -26,12 +26,12 @@ public function testArrays() { $this->assertEquals( "JSON.parse('[\\u0022hello\\u0022,\\u0022world\\u0022]')", - (string) JsString::from(['hello', 'world']) + (string) Js::from(['hello', 'world']) ); $this->assertEquals( "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')", - (string) JsString::from(['foo' => 'hello', 'bar' => 'world']) + (string) Js::from(['foo' => 'hello', 'bar' => 'world']) ); } @@ -39,7 +39,7 @@ public function testObjects() { $this->assertEquals( "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')", - (string) JsString::from((object) ['foo' => 'hello', 'bar' => 'world']) + (string) Js::from((object) ['foo' => 'hello', 'bar' => 'world']) ); } @@ -66,7 +66,7 @@ public function toArray() $this->assertEquals( "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')", - (string) JsString::from($data) + (string) Js::from($data) ); } @@ -98,7 +98,7 @@ public function toArray() $this->assertEquals( "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')", - (string) JsString::from($data) + (string) Js::from($data) ); } @@ -118,7 +118,7 @@ public function toArray() $this->assertEquals( "JSON.parse('{\\u0022foo\\u0022:\\u0022hello\\u0022,\\u0022bar\\u0022:\\u0022world\\u0022}')", - (string) JsString::from($data) + (string) Js::from($data) ); } }