Skip to content

Commit

Permalink
feat(Collection): add JsonToHtml
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Feb 10, 2025
1 parent 8991cfb commit 5171ef1
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Collection/JsonToHtml.php
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;
}

}
39 changes: 39 additions & 0 deletions src/Collection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,42 @@ $strictTypeArray->stringNull('d'); // null
$strictTypeArray->int('b'); // 1
$strictTypeArray->float('b'); // 1.0
```

# JsonToHtml

is abstract class what convert setup from server for javascript by json. You must extend the class and define property yourself. And load it by JSON.parse().

```php
use h4kuna\DataType\Collection\JsonToHtml;

class MyJsonConfig extends JsonToHtml {
public int $foo = 1;

public function __construct() {
parent::__construct('my-config');
}
}


$myJsonConfig = new MyJsonConfig();
$myJsonConfig->foo = 10;

(string) $myJsonConfig;
// <script type="text/json" id="my-config">{"foo":10}</script>
```

Typescript

```typescript
export function loadConfig<T>(id: string): T {
const c = document.getElementById(id)

if (c === null) {
throw new Error(`Could not find element with id '${id}'`)
}

return JSON.parse(c.textContent || c.innerHTML) as T
}

console.log(loadConfig('my-config'));
```
38 changes: 38 additions & 0 deletions tests/src/Unit/Collection/JsonToHtmlTest.php
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();

0 comments on commit 5171ef1

Please sign in to comment.