Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Sakovich committed Jul 22, 2019
1 parent 212bb66 commit 1b00097
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/SharedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

namespace Coderello\SharedData;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Renderable;
use JsonSerializable;

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

private $jsNamespace = 'sharedData';

public function __construct(array $config = [])
{
$this->hydrateConfig($config);
}

private function hydrateConfig(array $config)

This comment has been minimized.

Copy link
@klimov-paul

klimov-paul Jul 22, 2019

Contributor

Extracting such method makes a little sense: it will not be re-used anywhere besides the constructor, which contained this functionality before.

{
if (isset($config['js_namespace'])) {
$this->setJsNamespace($config['js_namespace']);
Expand All @@ -28,10 +35,14 @@ public function put($key, $value = null)
foreach ($key as $nestedKey => $nestedValue) {
$this->put($nestedKey, $nestedValue);
}
} elseif ($key instanceof JsonSerializable) {
$this->put($key->jsonSerialize());
} elseif ($key instanceof Arrayable) {
$this->put($key->toArray());
} elseif (is_object($key)) {
$this->put(get_object_vars($key));
} else {
throw new InvalidArgumentException('Unsupported data key type: '.gettype($key));
throw new InvalidArgumentException('Key type ['.gettype($key).'] is not supported.');
}

return $this;
Expand Down
24 changes: 24 additions & 0 deletions tests/SharedDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Coderello\SharedData\Tests;

use Coderello\SharedData\SharedData;
use Illuminate\Contracts\Support\Arrayable;
use JsonSerializable;

class SharedDataTest extends AbstractTestCase
{
Expand Down Expand Up @@ -39,6 +41,28 @@ public function testSetupData()
$this->sharedData->put($object);
$this->assertSame('object-scalar', $this->sharedData->get('objectScalar'));
$this->assertSame(['nested' => 'object-scalar'], $this->sharedData->get('objectArray'));

$arrayable = new class implements Arrayable {
public function toArray()
{
return [
'arrayable-key' => 'arrayable-value',
];
}
};
$this->sharedData->put($arrayable);
$this->assertSame('arrayable-value', $this->sharedData->get('arrayable-key'));

$jsonSerializable = new class implements JsonSerializable {
public function jsonSerialize()
{
return [
'json-serializable-key' => 'json-serializable-value',
];
}
};
$this->sharedData->put($jsonSerializable);
$this->assertSame('json-serializable-value', $this->sharedData->get('json-serializable-key'));
}

/**
Expand Down

0 comments on commit 1b00097

Please sign in to comment.