-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\DataType\Collection; | ||
|
||
use Nette\Utils\Html; | ||
use Nette\Utils\Json; | ||
use Stringable; | ||
|
||
abstract class JsonToHtml implements Stringable | ||
{ | ||
|
||
public function __construct(private string $_id) | ||
{ | ||
} | ||
|
||
|
||
public function __toString(): string | ||
{ | ||
return (string) $this->render(); | ||
} | ||
|
||
|
||
public function render(): ?Html | ||
{ | ||
$config = get_object_vars($this); | ||
unset($config['_id']); | ||
|
||
if ($config === []) { | ||
return null; | ||
} | ||
$el = Html::el('script', Json::encode($config)); | ||
$el->type = 'text/json'; | ||
$el->id = $this->_id; | ||
|
||
return $el; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\DataType\Tests\Unit\Collection; | ||
|
||
use h4kuna\DataType\Collection\JsonToHtml; | ||
use Tester\Assert; | ||
use Tester\TestCase; | ||
|
||
require __DIR__ . '/../../../bootstrap.php'; | ||
|
||
final class JsonToHtmlTest extends TestCase | ||
{ | ||
public function testMain(): void | ||
{ | ||
$config = self::createJsonToHtml(); | ||
Assert::same('<script type="text/json" id="test-config">{"foo":"","bar":0,"baz":0.0,"is":false}</script>', (string) $config); | ||
|
||
$config->bar = 1; | ||
$config->foo = 'lorem ipsum'; | ||
Assert::same('<script type="text/json" id="test-config">{"foo":"lorem ipsum","bar":1,"baz":0.0,"is":false}</script>', (string) $config); | ||
} | ||
|
||
|
||
private static function createJsonToHtml(): JsonToHtml | ||
{ | ||
return new class('test-config') extends JsonToHtml { | ||
public string $foo = ''; | ||
|
||
public int $bar = 0; | ||
|
||
public float $baz = 0.0; | ||
|
||
public bool $is = false; | ||
}; | ||
} | ||
} | ||
|
||
(new JsonToHtmlTest())->run(); |