Skip to content

Commit

Permalink
Merge pull request #6 from klimov-paul/refactor-config
Browse files Browse the repository at this point in the history
Refactor `SharedData` configuration
  • Loading branch information
Ilya Sakovich committed Jul 16, 2019
2 parents 0255eec + c87ea02 commit 212bb66
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ install:
- travis_retry composer install --prefer-dist --no-interaction

script:
- phpunit $PHPUNIT_FLAGS
- vendor/bin/phpunit $PHPUNIT_FLAGS
5 changes: 4 additions & 1 deletion config/shared-data.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php
/**
* @see https://github.com/coderello/laravel-shared-data
*/

return [

Expand All @@ -7,7 +10,7 @@
*
* By default the namespace is equal to 'sharedData'.
*
* It means that the shared data will be accessible from window.sharedData
* It means that the shared data will be accessible from `window.sharedData`
*/
'js_namespace' => 'sharedData',

Expand Down
35 changes: 20 additions & 15 deletions src/SharedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

namespace Coderello\SharedData;

use Illuminate\Support\Arr;
use InvalidArgumentException;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Arr;
use LogicException;

class SharedData implements Renderable, Jsonable
{
protected $data = [];

protected $config = [];
private $jsNamespace = 'sharedData';

public function __construct(array $config)
public function __construct(array $config = [])
{
$this->config = $config;
if (isset($config['js_namespace'])) {
$this->setJsNamespace($config['js_namespace']);
}
}

public function put($key, $value = null)
Expand All @@ -29,7 +31,7 @@ public function put($key, $value = null)
} elseif (is_object($key)) {
$this->put(get_object_vars($key));
} else {
throw new \InvalidArgumentException();
throw new InvalidArgumentException('Unsupported data key type: '.gettype($key));
}

return $this;
Expand All @@ -44,26 +46,29 @@ public function get($key = null)
return Arr::get($this->data, $key);
}

protected function getNamespace()
public function getJsNamespace(): string
{
if (! ($namespace = Arr::get($this->config, 'js_namespace'))) {
throw new LogicException('JavaScript namespace should be specified in the config file.');
}
return $this->jsNamespace;
}

public function setJsNamespace(string $jsNamespace): self
{
$this->jsNamespace = $jsNamespace;

return $namespace;
return $this;
}

public function toJson($options = 0)
public function toJson($options = 0): string
{
return json_encode($this->get(), $options);
}

public function render()
public function render(): string
{
return '<script>window[\''.$this->getNamespace().'\'] = '.$this->toJson().';</script>';
return '<script>window[\''.$this->getJsNamespace().'\'] = '.$this->toJson().';</script>';
}

public function __toString()
public function __toString(): string
{
return $this->render();
}
Expand Down
16 changes: 16 additions & 0 deletions tests/SharedDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,20 @@ public function testToString()

$this->assertSame($this->sharedData->render(), (string) $this->sharedData);
}

/**
* @depends testRender
*/
public function testStandaloneInstance()
{
$sharedData = (new SharedData())
->setJsNamespace('testData');

$this->assertSame('testData', $sharedData->getJsNamespace());

$html = $sharedData->render();
$expectedHtml = '<script>window[\'testData\'] = [];</script>';

$this->assertSame($expectedHtml, $html);
}
}

0 comments on commit 212bb66

Please sign in to comment.