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

Commit 1af312b

Browse files
author
Mateusz Gostański
committed
Changed response layout for PaginatedData
1 parent 95fb1ef commit 1af312b

File tree

3 files changed

+73
-73
lines changed

3 files changed

+73
-73
lines changed

src/Data/PaginatedData.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public function __construct(protected Response $response)
2020
$responseData = $response->json();
2121
$this->validateResponseData($responseData);
2222

23-
$this->data = $responseData['data']['data'];
24-
$this->currentPage = $responseData['data']['current_page'] ?? 1;
25-
$this->totalPages = $responseData['data']['last_page'] ?? 1;
26-
$this->perPage = $responseData['data']['per_page'] ?? count($this->data);
23+
$this->data = $responseData['data'];
24+
$this->currentPage = $responseData['meta']['current_page'] ?? 1;
25+
$this->totalPages = $responseData['meta']['last_page'] ?? 1;
26+
$this->perPage = $responseData['meta']['per_page'] ?? count($this->data);
2727
}
2828

2929
protected function validateResponse(Response $response): void
@@ -39,7 +39,7 @@ protected function validateResponseData(array $responseData): void
3939
throw new DamagedResponse();
4040
}
4141

42-
if (!isset($responseData['data']['data']) || empty($responseData['data']['data'])) {
42+
if (!isset($responseData['meta']) || empty($responseData['meta'])) {
4343
throw new DamagedResponse();
4444
}
4545
}

tests/Data/PaginatedDataTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Grixu\ApiClient\Tests;
3+
namespace Grixu\ApiClient\Tests\Data;
44

55
use Grixu\ApiClient\Data\PaginatedData;
66
use Grixu\ApiClient\Exceptions\DamagedResponse;
@@ -65,12 +65,12 @@ protected function createObjAndFakeWrongDataKeyName()
6565
[
6666
'*' => Http::response(
6767
[
68+
'data' => [
69+
'some',
70+
'data',
71+
'here'
72+
],
6873
'shit' => [
69-
'array' => [
70-
'some',
71-
'data',
72-
'here'
73-
],
7474
'current_page' => 1,
7575
'total' => 10,
7676
'per_page' => 3,
@@ -103,12 +103,12 @@ protected function createObjAndFakeWrongDataKeyInDataSection()
103103
[
104104
'*' => Http::response(
105105
[
106-
'data' => [
107-
'array' => [
108-
'some',
109-
'data',
110-
'here'
111-
],
106+
'shit' => [
107+
'some',
108+
'data',
109+
'here'
110+
],
111+
'meta' => [
112112
'current_page' => 1,
113113
'total' => 10,
114114
'per_page' => 3,

tests/Helpers/HttpMocksTrait.php

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ protected function mockHttpSuccessfulResponse(): void
1313
'*' => Http::response(
1414
[
1515
'data' => [
16-
'data' => [
17-
'some',
18-
'data',
19-
'here'
20-
],
16+
'some',
17+
'data',
18+
'here'
19+
],
20+
'meta' => [
2121
'current_page' => 1,
2222
'last_page' => 10,
2323
'per_page' => 3,
@@ -43,11 +43,11 @@ protected function mockHttpSinglePageDataResponseSequence(): void
4343
->push(
4444
[
4545
'data' => [
46-
'data' => [
47-
'some',
48-
'data',
49-
'here'
50-
],
46+
'some',
47+
'data',
48+
'here'
49+
],
50+
'meta' => [
5151
'current_page' => 1,
5252
'last_page' => 1,
5353
'per_page' => 3,
@@ -58,11 +58,11 @@ protected function mockHttpSinglePageDataResponseSequence(): void
5858
->push(
5959
[
6060
'data' => [
61-
'data' => [
62-
'some',
63-
'data',
64-
'here'
65-
],
61+
'some',
62+
'data',
63+
'here'
64+
],
65+
'meta' => [
6666
'current_page' => 1,
6767
'last_page' => 1,
6868
'per_page' => 3,
@@ -88,11 +88,11 @@ protected function mockHttpMultiplePagesDataResponseSequence(): void
8888
->push(
8989
[
9090
'data' => [
91-
'data' => [
92-
'some',
93-
'data',
94-
'here'
95-
],
91+
'some',
92+
'data',
93+
'here'
94+
],
95+
'meta' => [
9696
'current_page' => 1,
9797
'last_page' => 4,
9898
'per_page' => 3,
@@ -103,11 +103,11 @@ protected function mockHttpMultiplePagesDataResponseSequence(): void
103103
->push(
104104
[
105105
'data' => [
106-
'data' => [
107-
'some',
108-
'data',
109-
'here'
110-
],
106+
'some',
107+
'data',
108+
'here'
109+
],
110+
'meta' => [
111111
'current_page' => 2,
112112
'last_page' => 4,
113113
'per_page' => 3,
@@ -118,11 +118,11 @@ protected function mockHttpMultiplePagesDataResponseSequence(): void
118118
->push(
119119
[
120120
'data' => [
121-
'data' => [
122-
'some',
123-
'data',
124-
'here'
125-
],
121+
'some',
122+
'data',
123+
'here'
124+
],
125+
'meta' => [
126126
'current_page' => 3,
127127
'last_page' => 4,
128128
'per_page' => 3,
@@ -133,11 +133,11 @@ protected function mockHttpMultiplePagesDataResponseSequence(): void
133133
->push(
134134
[
135135
'data' => [
136-
'data' => [
137-
'some',
138-
'data',
139-
'here'
140-
],
136+
'some',
137+
'data',
138+
'here'
139+
],
140+
'meta' => [
141141
'current_page' => 4,
142142
'last_page' => 4,
143143
'per_page' => 3,
@@ -163,23 +163,23 @@ protected function mockHttpSinglePageRealDataResponseSequence(): void
163163
->push(
164164
[
165165
'data' => [
166-
'data' => [
167-
[
168-
'first' => 'First value',
169-
'second' => 'Second value',
170-
'third' => 'Third value'
171-
],
172-
[
173-
'first' => 'First value',
174-
'second' => 'Second value',
175-
'third' => 'Third value'
176-
],
177-
[
178-
'first' => 'First value',
179-
'second' => 'Second value',
180-
'third' => 'Third value'
181-
]
166+
[
167+
'first' => 'First value',
168+
'second' => 'Second value',
169+
'third' => 'Third value'
182170
],
171+
[
172+
'first' => 'First value',
173+
'second' => 'Second value',
174+
'third' => 'Third value'
175+
],
176+
[
177+
'first' => 'First value',
178+
'second' => 'Second value',
179+
'third' => 'Third value'
180+
]
181+
],
182+
'meta' => [
183183
'current_page' => 1,
184184
'last_page' => 1,
185185
'per_page' => 10,
@@ -211,11 +211,11 @@ protected function mockHttpSequenceWith401(): void
211211
->push(
212212
[
213213
'data' => [
214-
'data' => [
215-
'some',
216-
'data',
217-
'here'
218-
],
214+
'some',
215+
'data',
216+
'here'
217+
],
218+
'meta' => [
219219
'current_page' => 1,
220220
'last_page' => 10,
221221
'per_page' => 3,

0 commit comments

Comments
 (0)