66use Hippiemedia \Format ;
77use Hippiemedia \Resource ;
88use Hippiemedia \Link ;
9+ use DocteurKlein \JsonChunks \Encode ;
910
1011final class Hal implements Format
1112{
@@ -15,6 +16,11 @@ public function accepts(): string
1516 }
1617
1718 public function __invoke (Resource $ resource ): iterable
19+ {
20+ return Encode::from ($ this ->normalize ($ resource ));
21+ }
22+
23+ private function normalize (Resource $ resource ): iterable
1824 {
1925 $ linksByRel = array_merge (
2026 f \group ($ resource ->links , f \invoker ('rel ' )),
@@ -39,7 +45,7 @@ public function __invoke(Resource $resource): iterable
3945 }),
4046 '_embedded ' => array_merge (f \map ($ operationsByRel , function ($ operations ) {
4147 return f \map (array_values ($ operations ), function ($ operation ) {
42- return $ this (new Resource ($ operation ->url , [
48+ return $ this -> normalize (new Resource ($ operation ->url , [
4349 '_templates ' => [
4450 'default ' => [
4551 'title ' => $ operation ->title ,
@@ -62,8 +68,8 @@ public function __invoke(Resource $resource): iterable
6268 ], array_merge ([new Link (['self ' ], $ operation ->url , $ operation ->templated )], $ operation ->links ), []));
6369 });
6470 }), f \map ($ resource ->embedded , function ($ resources ) {
65- return f \map ( $ resources , $ this );
66- }, $ this ))
71+ return array_map ([ $ this , ' normalize ' ], $ resources );
72+ }))
6773 ]);
6874 }
6975}
0 commit comments