Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit f3f0814

Browse files
authored
Dev/3.3 (#3)
* Added chunk() functionality in JsonApiFetcher * Added `getDataCollection` method & definition * Added `chunk` method & definition * Updated JsonApiFetcher to inject Collection to Closure in chunk() Collection is making on the fly, from data key from PaginatedData DTO (which contain full API response) * Updated CHANGELOG.md
1 parent 702c0b3 commit f3f0814

File tree

9 files changed

+81
-7
lines changed

9 files changed

+81
-7
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
All 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`

src/Contracts/FetchedData.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Grixu\ApiClient\Contracts;
44

5+
use Illuminate\Support\Collection;
6+
57
interface FetchedData
68
{
79
public function getData(): array;
10+
public function getDataCollection(): Collection;
811
public function isMoreToLoad(): bool;
912
}

src/Contracts/ResponseParser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Grixu\ApiClient\Contracts;
44

55
use Illuminate\Support\Collection;
6+
use Spatie\DataTransferObject\DataTransferObject;
67

78
interface ResponseParser
89
{
910
public function parse(Collection $inputData): Collection;
11+
public function parseElement(array $input): DataTransferObject;
1012
}

src/Data/PaginatedData.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Grixu\ApiClient\Contracts\FetchedData;
66
use Grixu\ApiClient\Exceptions\DamagedResponse;
77
use Illuminate\Http\Client\Response;
8+
use Illuminate\Support\Collection;
89

910
class 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;

src/Data/StraightKeyParser.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Str;
88
use ReflectionClass;
99
use ReflectionProperty;
10+
use Spatie\DataTransferObject\DataTransferObject;
1011

1112
class 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

src/JsonApiFetcher.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

tests/Data/StraightKeyParserTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

tests/Helpers/ExampleDto.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

tests/JsonApiFetcherTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Grixu\ApiClient\Tests\Helpers\HttpMocksTrait;
1010
use Grixu\ApiClient\UrlCompose;
1111
use Illuminate\Support\Facades\Cache;
12+
use Mockery\Mock;
1213
use Orchestra\Testbench\TestCase;
1314

1415
class 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
}

0 commit comments

Comments
 (0)