22
33namespace Coderello \SharedData ;
44
5+ use Illuminate \Contracts \Support \Arrayable ;
56use Illuminate \Support \Arr ;
67use InvalidArgumentException ;
78use Illuminate \Contracts \Support \Jsonable ;
89use Illuminate \Contracts \Support \Renderable ;
10+ use JsonSerializable ;
911
1012class SharedData implements Renderable, Jsonable
1113{
12- protected $ data = [];
14+ private $ data = [];
1315
1416 private $ jsNamespace = 'sharedData ' ;
1517
1618 public function __construct (array $ config = [])
19+ {
20+ $ this ->hydrateConfig ($ config );
21+ }
22+
23+ private function hydrateConfig (array $ config )
1724 {
1825 if (isset ($ config ['js_namespace ' ])) {
1926 $ this ->setJsNamespace ($ config ['js_namespace ' ]);
@@ -28,10 +35,14 @@ public function put($key, $value = null)
2835 foreach ($ key as $ nestedKey => $ nestedValue ) {
2936 $ this ->put ($ nestedKey , $ nestedValue );
3037 }
38+ } elseif ($ key instanceof JsonSerializable) {
39+ $ this ->put ($ key ->jsonSerialize ());
40+ } elseif ($ key instanceof Arrayable) {
41+ $ this ->put ($ key ->toArray ());
3142 } elseif (is_object ($ key )) {
3243 $ this ->put (get_object_vars ($ key ));
3344 } else {
34- throw new InvalidArgumentException ('Unsupported data key type: ' .gettype ($ key ));
45+ throw new InvalidArgumentException ('Key type [ ' .gettype ($ key ). ' ] is not supported. ' );
3546 }
3647
3748 return $ this ;
0 commit comments