Skip to content

Commit 2a80f05

Browse files
author
Antoine Aflalo
committed
feat(Collection): Make collection response usable
1 parent ab0c21a commit 2a80f05

5 files changed

Lines changed: 213 additions & 19 deletions

File tree

src/Response/Api/Collection/CollectionMetaData.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@
1313
/**
1414
* Class CollectionPagination
1515
*
16-
* @property-read int $total
17-
* @property-read int $count
18-
* @property-read int $perPage
19-
* @property-read int $currentPage
20-
* @property-read int $totalPages
2116
* @package ProvulusSDK\Client\Response\Collection
2217
*/
2318
class CollectionMetaData
2419
{
2520

26-
private $metaDat = [];
21+
private $metaData = [];
2722

2823
/**
2924
* CollectionPagination constructor.
@@ -32,15 +27,15 @@ class CollectionMetaData
3227
*/
3328
public function __construct(array $pagination)
3429
{
35-
$this->metaDat = $pagination;
30+
$this->metaData = $pagination;
3631
}
3732

3833

3934
function __isset($name)
4035
{
4136
$name = Str::snake($name);
4237

43-
return isset($this->metaDat[$name]);
38+
return isset($this->metaData[$name]);
4439
}
4540

4641

@@ -49,7 +44,7 @@ function __get($name)
4944
if (isset($this->{$name})) {
5045
$name = Str::snake($name);
5146

52-
return $this->metaDat[$name];
47+
return $this->metaData[$name];
5348
}
5449

5550
return null;

src/Response/Api/Collection/CollectionResponse.php

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace ZEROSPAM\Framework\SDK\Response\Api\Collection;
1010

11-
use Traversable;
1211
use ZEROSPAM\Framework\SDK\Response\Api\BaseResponse;
1312
use ZEROSPAM\Framework\SDK\Response\Api\IResponse;
1413

@@ -20,7 +19,7 @@
2019
*
2120
* @package ZEROSPAM\Framework\SDK\Response\Api\Collection
2221
*/
23-
abstract class CollectionResponse extends BaseResponse implements \IteratorAggregate
22+
abstract class CollectionResponse extends BaseResponse implements \IteratorAggregate, \ArrayAccess
2423
{
2524

2625
/**
@@ -63,12 +62,86 @@ public function getMetaData(): CollectionMetaData
6362
* Retrieve an external iterator
6463
*
6564
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
66-
* @return Traversable An instance of an object implementing <b>Iterator</b> or
67-
* <b>Traversable</b>
6865
* @since 5.0.0
66+
* @return \ArrayIterator
6967
*/
7068
public function getIterator()
7169
{
7270
return new \ArrayIterator($this->data);
7371
}
72+
73+
74+
/**
75+
* Whether a offset exists
76+
*
77+
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
78+
*
79+
* @param mixed $offset <p>
80+
* An offset to check for.
81+
* </p>
82+
*
83+
* @return boolean true on success or false on failure.
84+
* </p>
85+
* <p>
86+
* The return value will be casted to boolean if non-boolean was returned.
87+
* @since 5.0.0
88+
*/
89+
public function offsetExists($offset)
90+
{
91+
return $this->getIterator()->offsetExists($offset);
92+
}
93+
94+
/**
95+
* Offset to retrieve
96+
*
97+
* @link http://php.net/manual/en/arrayaccess.offsetget.php
98+
*
99+
* @param mixed $offset <p>
100+
* The offset to retrieve.
101+
* </p>
102+
*
103+
* @return mixed Can return all value types.
104+
* @since 5.0.0
105+
*/
106+
public function offsetGet($offset)
107+
{
108+
return $this->getIterator()->offsetGet($offset);
109+
}
110+
111+
/**
112+
* Offset to set
113+
*
114+
* @link http://php.net/manual/en/arrayaccess.offsetset.php
115+
*
116+
* @param mixed $offset <p>
117+
* The offset to assign the value to.
118+
* </p>
119+
* @param mixed $value <p>
120+
* The value to set.
121+
* </p>
122+
*
123+
* @return void
124+
* @since 5.0.0
125+
*/
126+
public function offsetSet($offset, $value)
127+
{
128+
throw new \InvalidArgumentException("Can't modify collection");
129+
}
130+
131+
/**
132+
* Offset to unset
133+
*
134+
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
135+
*
136+
* @param mixed $offset <p>
137+
* The offset to unset.
138+
* </p>
139+
*
140+
* @return void
141+
* @since 5.0.0
142+
*/
143+
public function offsetUnset($offset)
144+
{
145+
throw new \InvalidArgumentException("Can't modify collection");
146+
}
74147
}

tests/src/Base/Data/Collection/CollectionTestRequest.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,49 @@
88

99
namespace ZEROSPAM\Framework\SDK\Test\Base\Data\Collection;
1010

11+
use ZEROSPAM\Framework\SDK\Request\Api\BaseRequest;
12+
use ZEROSPAM\Framework\SDK\Request\Type\RequestType;
13+
use ZEROSPAM\Framework\SDK\Response\Api\IResponse;
1114

12-
class CollectionTestRequest
15+
/**
16+
* Class CollectionTestRequest
17+
*
18+
* @method CollectionTestResponse getResponse()
19+
* @package ZEROSPAM\Framework\SDK\Test\Base\Data\Collection
20+
*/
21+
class CollectionTestRequest extends BaseRequest
1322
{
1423

15-
}
24+
25+
/**
26+
* The url of the route.
27+
*
28+
* @return string
29+
*/
30+
public function routeUrl(): string
31+
{
32+
return 'collection';
33+
}
34+
35+
/**
36+
* Type of request.
37+
*
38+
* @return RequestType
39+
*/
40+
public function httpType(): RequestType
41+
{
42+
return RequestType::HTTP_GET();
43+
}
44+
45+
/**
46+
* Process the data that is in the response.
47+
*
48+
* @param array $jsonResponse
49+
*
50+
* @return \ZEROSPAM\Framework\SDK\Response\Api\IResponse
51+
*/
52+
public function processResponse(array $jsonResponse): IResponse
53+
{
54+
return new CollectionTestResponse($jsonResponse);
55+
}
56+
}

tests/src/Base/Data/Collection/CollectionTestResponse.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,38 @@
88

99
namespace ZEROSPAM\Framework\SDK\Test\Base\Data\Collection;
1010

11+
use ZEROSPAM\Framework\SDK\Response\Api\Collection\CollectionMetaData;
12+
use ZEROSPAM\Framework\SDK\Response\Api\Collection\CollectionResponse;
13+
use ZEROSPAM\Framework\SDK\Response\Api\IResponse;
14+
use ZEROSPAM\Framework\SDK\Test\Base\Data\TestResponse;
1115

12-
class CollectionTestResponse
16+
class CollectionTestResponse extends CollectionResponse
1317
{
18+
/**
19+
* CollectionTestResponse constructor.
20+
*
21+
* @param array $data
22+
*/
23+
public function __construct(array $data)
24+
{
25+
parent::__construct(new CollectionMetaData($data['meta']), $data['response']);
26+
}
1427

15-
}
28+
29+
/**
30+
* Transform the basic data (string[]) into a response array (IResponse[])
31+
*
32+
* @param array $data
33+
*
34+
* @return IResponse[]
35+
*/
36+
public static function dataToResponses(array $data): array
37+
{
38+
return array_map(
39+
function (array $respData) {
40+
return new TestResponse($respData);
41+
},
42+
$data
43+
);
44+
}
45+
}

tests/src/Tests/Request/CollectionRequestTest.php

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,63 @@
88

99
namespace ZEROSPAM\Framework\SDK\Test\Tests\Request;
1010

11+
use ZEROSPAM\Framework\SDK\Test\Base\Data\Collection\CollectionTestRequest;
12+
use ZEROSPAM\Framework\SDK\Test\Base\Data\TestResponse;
13+
use ZEROSPAM\Framework\SDK\Test\Base\TestCase;
1114

12-
class CollectionRequestTest
15+
class CollectionRequestTest extends TestCase
1316
{
1417

15-
}
18+
public function testCollectionResponse()
19+
{
20+
$json
21+
= <<<JSON
22+
{
23+
"response": [
24+
{
25+
"author": "Koma",
26+
"featured": false,
27+
"id": "chatwork",
28+
"name": "chatwork",
29+
"version": "1.0.1",
30+
"icons": {
31+
"png": "https://cdn.franzinfra.com/recipes/dist/chatwork/src/icon.png",
32+
"svg": "https://cdn.franzinfra.com/recipes/dist/chatwork/src/icon.svg"
33+
}
34+
},
35+
{
36+
"author": "Stefan Malzner <stefan@adlk.io>",
37+
"featured": false,
38+
"id": "ciscospark",
39+
"name": "Cisco Spark",
40+
"version": "1.0.0",
41+
"icons": {
42+
"png": "https://cdn.franzinfra.com/recipes/dist/ciscospark/src/icon.png",
43+
"svg": "https://cdn.franzinfra.com/recipes/dist/ciscospark/src/icon.svg"
44+
}
45+
}
46+
],
47+
"meta": {
48+
"per_page": 2,
49+
"page": 0,
50+
"pages": 1,
51+
"count": 2
52+
}
53+
}
54+
JSON;
55+
56+
$client = $this->preSuccess($json);
57+
58+
$request = new CollectionTestRequest();
59+
$client->getOAuthTestClient()->processRequest($request);
60+
61+
$response = $request->getResponse();
62+
$this->assertCount(2, $response);
63+
$this->assertInstanceOf(TestResponse::class, $response[0]);
64+
$this->assertInstanceOf(TestResponse::class, $response[1]);
65+
$this->assertEquals("Stefan Malzner <stefan@adlk.io>", $response[1]->author);
66+
67+
$this->assertEquals(2, $response->getMetaData()->count);
68+
$this->assertEquals(2, $response->getMetaData()->perPage);
69+
}
70+
}

0 commit comments

Comments
 (0)