Skip to content

Commit

Permalink
rename class
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 5, 2021
1 parent 4965ce3 commit bbf47d5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();
}
}
24 changes: 12 additions & 12 deletions tests/Support/SupportJsStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,42 @@

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\JsString;
use Illuminate\Support\Js;
use JsonSerializable;
use PHPUnit\Framework\TestCase;

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('<div class="foo">\'quoted html\'</div>')
(string) Js::from('<div class="foo">\'quoted html\'</div>')
);
}

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'])
);
}

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'])
);
}

Expand All @@ -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)
);
}

Expand Down Expand Up @@ -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)
);
}

Expand All @@ -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)
);
}
}

0 comments on commit bbf47d5

Please sign in to comment.