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 2
2
3
3
All notable changes to ` api-client ` will be documented in this file
4
4
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
+
5
14
## 3.2.1 - 2021-05-04
6
15
7
16
- Bug fixed in ` StraightKeyParser `
Original file line number Diff line number Diff line change 2
2
3
3
namespace Grixu \ApiClient \Contracts ;
4
4
5
+ use Illuminate \Support \Collection ;
6
+
5
7
interface FetchedData
6
8
{
7
9
public function getData (): array ;
10
+ public function getDataCollection (): Collection ;
8
11
public function isMoreToLoad (): bool ;
9
12
}
Original file line number Diff line number Diff line change 3
3
namespace Grixu \ApiClient \Contracts ;
4
4
5
5
use Illuminate \Support \Collection ;
6
+ use Spatie \DataTransferObject \DataTransferObject ;
6
7
7
8
interface ResponseParser
8
9
{
9
10
public function parse (Collection $ inputData ): Collection ;
11
+ public function parseElement (array $ input ): DataTransferObject ;
10
12
}
Original file line number Diff line number Diff line change 5
5
use Grixu \ApiClient \Contracts \FetchedData ;
6
6
use Grixu \ApiClient \Exceptions \DamagedResponse ;
7
7
use Illuminate \Http \Client \Response ;
8
+ use Illuminate \Support \Collection ;
8
9
9
10
class PaginatedData implements FetchedData
10
11
{
@@ -35,7 +36,7 @@ protected function validateResponse(Response $response): void
35
36
36
37
protected function validateResponseData (array $ responseData ): void
37
38
{
38
- if (!isset ($ responseData ['data ' ]) || empty ( $ responseData [ ' data ' ]) ) {
39
+ if (!isset ($ responseData ['data ' ])) {
39
40
throw new DamagedResponse ();
40
41
}
41
42
@@ -49,6 +50,11 @@ public function getData(): array
49
50
return $ this ->data ;
50
51
}
51
52
53
+ public function getDataCollection (): Collection
54
+ {
55
+ return collect ($ this ->data );
56
+ }
57
+
52
58
public function getCurrentPage (): int
53
59
{
54
60
return $ this ->currentPage ;
Original file line number Diff line number Diff line change 7
7
use Illuminate \Support \Str ;
8
8
use ReflectionClass ;
9
9
use ReflectionProperty ;
10
+ use Spatie \DataTransferObject \DataTransferObject ;
10
11
11
12
class StraightKeyParser implements ResponseParser
12
13
{
@@ -27,31 +28,30 @@ public function parse(Collection $inputCollection): Collection
27
28
continue ;
28
29
}
29
30
30
- $ preparedData = $ this ->parseElement ($ inputData );
31
- $ dto = new $ this ->dtoClass ($ preparedData );
31
+ $ dto = $ this ->parseElement ($ inputData );
32
32
33
33
$ parsed ->push ($ dto );
34
34
}
35
35
36
36
return $ parsed ;
37
37
}
38
38
39
- protected function parseElement (array $ input ): array
39
+ public function parseElement (array $ input ): DataTransferObject
40
40
{
41
41
$ data = [];
42
42
43
43
foreach ($ this ->fields as $ field ) {
44
44
$ dtoFieldName = $ field ->getName ();
45
45
$ arrayFieldName = Str::snake ($ dtoFieldName );
46
46
47
- if ($ dtoFieldName === 'relationships ' && isset ($ input [$ arrayFieldName ])) {
47
+ if ($ dtoFieldName === 'relations ' && isset ($ input [$ arrayFieldName ])) {
48
48
$ data [$ dtoFieldName ] = $ this ->parseRelationship ($ input [$ arrayFieldName ]);
49
49
} else {
50
50
$ data [$ dtoFieldName ] = $ input [$ arrayFieldName ] ?? null ;
51
51
}
52
52
}
53
53
54
- return $ data ;
54
+ return ( new $ this -> dtoClass ( $ data)) ;
55
55
}
56
56
57
57
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)
46
46
$ this ->fetch ($ before );
47
47
}
48
48
}
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
+ }
49
68
}
Original file line number Diff line number Diff line change @@ -78,7 +78,6 @@ public function it_replaces_datetime_to_carbon()
78
78
);
79
79
80
80
81
-
82
81
$ returnedData = $ this ->basicAssertions ($ inputData );
83
82
84
83
$ this ->assertEquals ($ date ->timestamp , $ returnedData ->first ()->date ->timestamp );
@@ -133,4 +132,25 @@ public function it_replaces_enums()
133
132
134
133
$ this ->basicAssertions ($ inputData );
135
134
}
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
+ }
136
156
}
Original file line number Diff line number Diff line change @@ -19,4 +19,6 @@ class ExampleDto extends DataTransferObject
19
19
public FakeEnum |null $ enum ;
20
20
21
21
public int |null $ id ;
22
+
23
+ public array |null $ relations ;
22
24
}
Original file line number Diff line number Diff line change 9
9
use Grixu \ApiClient \Tests \Helpers \HttpMocksTrait ;
10
10
use Grixu \ApiClient \UrlCompose ;
11
11
use Illuminate \Support \Facades \Cache ;
12
+ use Mockery \Mock ;
12
13
use Orchestra \Testbench \TestCase ;
13
14
14
15
class JsonApiFetcherTest extends TestCase
@@ -118,4 +119,16 @@ public function it_give_access_to_underlying_url_compose()
118
119
119
120
$ this ->assertEquals (UrlCompose::class, $ returnedValue ::class);
120
121
}
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
+ }
121
134
}
You can’t perform that action at this time.
0 commit comments