This repository was archived by the owner on Jan 5, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 9 files changed +81
-7
lines changed Expand file tree Collapse file tree 9 files changed +81
-7
lines changed Original file line number Diff line number Diff line change 22
33All notable changes to ` api-client ` will be documented in this file
44
5+ ## 3.3.0 - 2021-05-21
6+
7+ - Added ` chunk() ` method in ` JsonApiFetcher `
8+ - Added ` getDataCollection ` in ` PaginatedData `
9+ - Added definition of ` getDataCollection ` in ` FetchedData `
10+ - Make method ` parseElement ` public in ` StraightKeyParser `
11+ - Added definition of ` praseElement ` in ` ResponseParser ` interface
12+
13+
514## 3.2.1 - 2021-05-04
615
716- Bug fixed in ` StraightKeyParser `
Original file line number Diff line number Diff line change 22
33namespace Grixu \ApiClient \Contracts ;
44
5+ use Illuminate \Support \Collection ;
6+
57interface FetchedData
68{
79 public function getData (): array ;
10+ public function getDataCollection (): Collection ;
811 public function isMoreToLoad (): bool ;
912}
Original file line number Diff line number Diff line change 33namespace Grixu \ApiClient \Contracts ;
44
55use Illuminate \Support \Collection ;
6+ use Spatie \DataTransferObject \DataTransferObject ;
67
78interface ResponseParser
89{
910 public function parse (Collection $ inputData ): Collection ;
11+ public function parseElement (array $ input ): DataTransferObject ;
1012}
Original file line number Diff line number Diff line change 55use Grixu \ApiClient \Contracts \FetchedData ;
66use Grixu \ApiClient \Exceptions \DamagedResponse ;
77use Illuminate \Http \Client \Response ;
8+ use Illuminate \Support \Collection ;
89
910class PaginatedData implements FetchedData
1011{
@@ -35,7 +36,7 @@ protected function validateResponse(Response $response): void
3536
3637 protected function validateResponseData (array $ responseData ): void
3738 {
38- if (!isset ($ responseData ['data ' ]) || empty ( $ responseData [ ' data ' ]) ) {
39+ if (!isset ($ responseData ['data ' ])) {
3940 throw new DamagedResponse ();
4041 }
4142
@@ -49,6 +50,11 @@ public function getData(): array
4950 return $ this ->data ;
5051 }
5152
53+ public function getDataCollection (): Collection
54+ {
55+ return collect ($ this ->data );
56+ }
57+
5258 public function getCurrentPage (): int
5359 {
5460 return $ this ->currentPage ;
Original file line number Diff line number Diff line change 77use Illuminate \Support \Str ;
88use ReflectionClass ;
99use ReflectionProperty ;
10+ use Spatie \DataTransferObject \DataTransferObject ;
1011
1112class StraightKeyParser implements ResponseParser
1213{
@@ -27,31 +28,30 @@ public function parse(Collection $inputCollection): Collection
2728 continue ;
2829 }
2930
30- $ preparedData = $ this ->parseElement ($ inputData );
31- $ dto = new $ this ->dtoClass ($ preparedData );
31+ $ dto = $ this ->parseElement ($ inputData );
3232
3333 $ parsed ->push ($ dto );
3434 }
3535
3636 return $ parsed ;
3737 }
3838
39- protected function parseElement (array $ input ): array
39+ public function parseElement (array $ input ): DataTransferObject
4040 {
4141 $ data = [];
4242
4343 foreach ($ this ->fields as $ field ) {
4444 $ dtoFieldName = $ field ->getName ();
4545 $ arrayFieldName = Str::snake ($ dtoFieldName );
4646
47- if ($ dtoFieldName === 'relationships ' && isset ($ input [$ arrayFieldName ])) {
47+ if ($ dtoFieldName === 'relations ' && isset ($ input [$ arrayFieldName ])) {
4848 $ data [$ dtoFieldName ] = $ this ->parseRelationship ($ input [$ arrayFieldName ]);
4949 } else {
5050 $ data [$ dtoFieldName ] = $ input [$ arrayFieldName ] ?? null ;
5151 }
5252 }
5353
54- return $ data ;
54+ return ( new $ this -> dtoClass ( $ data)) ;
5555 }
5656
5757 protected function parseRelationship (array $ inputRelationships ): array
Original file line number Diff line number Diff line change @@ -46,4 +46,23 @@ public function fetch(?Closure $before = null)
4646 $ this ->fetch ($ before );
4747 }
4848 }
49+
50+ public function chunk (Closure $ loop )
51+ {
52+ $ dataFetcher = new DataFetcher (
53+ $ this ->urlComposer ->get (),
54+ $ this ->config ->getResponseDataClass (),
55+ $ this ->token
56+ );
57+
58+ $ dataFetcher ->fetch ();
59+ $ results = $ dataFetcher ->get ();
60+
61+ $ loop ($ results ->getDataCollection ());
62+
63+ if ($ results ->isMoreToLoad () && $ this ->loadAll ) {
64+ $ this ->urlComposer ->nextPage ();
65+ $ this ->chunk ($ loop );
66+ }
67+ }
4968}
Original file line number Diff line number Diff line change @@ -78,7 +78,6 @@ public function it_replaces_datetime_to_carbon()
7878 );
7979
8080
81-
8281 $ returnedData = $ this ->basicAssertions ($ inputData );
8382
8483 $ this ->assertEquals ($ date ->timestamp , $ returnedData ->first ()->date ->timestamp );
@@ -133,4 +132,25 @@ public function it_replaces_enums()
133132
134133 $ this ->basicAssertions ($ inputData );
135134 }
135+
136+ /** @test */
137+ public function it_parsing_collection_of_arrays_with_relations_data_to_collection_of_dtos ()
138+ {
139+ $ inputData = collect (
140+ [
141+ [
142+ 'first ' => 'first entry ' ,
143+ 'second ' => 'second entry ' ,
144+ 'third ' => 'third entry ' ,
145+ 'relations ' => [
146+ [
147+ 'test ' => 'another_thing '
148+ ]
149+ ]
150+ ]
151+ ]
152+ );
153+
154+ $ this ->basicAssertions ($ inputData );
155+ }
136156}
Original file line number Diff line number Diff line change @@ -19,4 +19,6 @@ class ExampleDto extends DataTransferObject
1919 public FakeEnum |null $ enum ;
2020
2121 public int |null $ id ;
22+
23+ public array |null $ relations ;
2224}
Original file line number Diff line number Diff line change 99use Grixu \ApiClient \Tests \Helpers \HttpMocksTrait ;
1010use Grixu \ApiClient \UrlCompose ;
1111use Illuminate \Support \Facades \Cache ;
12+ use Mockery \Mock ;
1213use Orchestra \Testbench \TestCase ;
1314
1415class JsonApiFetcherTest extends TestCase
@@ -118,4 +119,16 @@ public function it_give_access_to_underlying_url_compose()
118119
119120 $ this ->assertEquals (UrlCompose::class, $ returnedValue ::class);
120121 }
122+
123+ /** @test */
124+ public function it_chunk_load ()
125+ {
126+ Cache::flush ();
127+ $ this ->mockHttpMultiplePagesDataResponseSequence ();
128+ $ this ->makeObj (true );
129+
130+ $ closure = \Mockery::mock (fn () => true );
131+ $ this ->obj ->chunk (fn () => $ closure ());
132+ $ closure ->shouldHaveBeenCalled ();
133+ }
121134}
You can’t perform that action at this time.
0 commit comments