Skip to content

Commit 212bb66

Browse files
author
Ilya Sakovich
authored
Merge pull request #6 from klimov-paul/refactor-config
Refactor `SharedData` configuration
2 parents 0255eec + c87ea02 commit 212bb66

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ install:
1919
- travis_retry composer install --prefer-dist --no-interaction
2020

2121
script:
22-
- phpunit $PHPUNIT_FLAGS
22+
- vendor/bin/phpunit $PHPUNIT_FLAGS

config/shared-data.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
/**
3+
* @see https://github.com/coderello/laravel-shared-data
4+
*/
25

36
return [
47

@@ -7,7 +10,7 @@
710
*
811
* By default the namespace is equal to 'sharedData'.
912
*
10-
* It means that the shared data will be accessible from window.sharedData
13+
* It means that the shared data will be accessible from `window.sharedData`
1114
*/
1215
'js_namespace' => 'sharedData',
1316

src/SharedData.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
namespace Coderello\SharedData;
44

5+
use Illuminate\Support\Arr;
6+
use InvalidArgumentException;
57
use Illuminate\Contracts\Support\Jsonable;
68
use Illuminate\Contracts\Support\Renderable;
7-
use Illuminate\Support\Arr;
8-
use LogicException;
99

1010
class SharedData implements Renderable, Jsonable
1111
{
1212
protected $data = [];
1313

14-
protected $config = [];
14+
private $jsNamespace = 'sharedData';
1515

16-
public function __construct(array $config)
16+
public function __construct(array $config = [])
1717
{
18-
$this->config = $config;
18+
if (isset($config['js_namespace'])) {
19+
$this->setJsNamespace($config['js_namespace']);
20+
}
1921
}
2022

2123
public function put($key, $value = null)
@@ -29,7 +31,7 @@ public function put($key, $value = null)
2931
} elseif (is_object($key)) {
3032
$this->put(get_object_vars($key));
3133
} else {
32-
throw new \InvalidArgumentException();
34+
throw new InvalidArgumentException('Unsupported data key type: '.gettype($key));
3335
}
3436

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

47-
protected function getNamespace()
49+
public function getJsNamespace(): string
4850
{
49-
if (! ($namespace = Arr::get($this->config, 'js_namespace'))) {
50-
throw new LogicException('JavaScript namespace should be specified in the config file.');
51-
}
51+
return $this->jsNamespace;
52+
}
53+
54+
public function setJsNamespace(string $jsNamespace): self
55+
{
56+
$this->jsNamespace = $jsNamespace;
5257

53-
return $namespace;
58+
return $this;
5459
}
5560

56-
public function toJson($options = 0)
61+
public function toJson($options = 0): string
5762
{
5863
return json_encode($this->get(), $options);
5964
}
6065

61-
public function render()
66+
public function render(): string
6267
{
63-
return '<script>window[\''.$this->getNamespace().'\'] = '.$this->toJson().';</script>';
68+
return '<script>window[\''.$this->getJsNamespace().'\'] = '.$this->toJson().';</script>';
6469
}
6570

66-
public function __toString()
71+
public function __toString(): string
6772
{
6873
return $this->render();
6974
}

tests/SharedDataTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,20 @@ public function testToString()
9797

9898
$this->assertSame($this->sharedData->render(), (string) $this->sharedData);
9999
}
100+
101+
/**
102+
* @depends testRender
103+
*/
104+
public function testStandaloneInstance()
105+
{
106+
$sharedData = (new SharedData())
107+
->setJsNamespace('testData');
108+
109+
$this->assertSame('testData', $sharedData->getJsNamespace());
110+
111+
$html = $sharedData->render();
112+
$expectedHtml = '<script>window[\'testData\'] = [];</script>';
113+
114+
$this->assertSame($expectedHtml, $html);
115+
}
100116
}

0 commit comments

Comments
 (0)