Skip to content

Commit 7693d2c

Browse files
author
Antoine Aflalo
committed
feat(Collection): Improves Collections
How to build them and define them, most of the time it's about iterating in the same array to build the wanted response.
1 parent a8e63d6 commit 7693d2c

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

src/Response/Api/Collection/CollectionResponse.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,25 @@ public function __construct(CollectionMetaData $metaData, array $data)
4646
*
4747
* @return IResponse[]
4848
*/
49-
abstract public static function dataToResponses(array $data): array;
49+
protected static function dataToResponses(array $data): array
50+
{
51+
$class = static::responseClass();
52+
53+
return array_map(
54+
function (array $resData) use ($class) {
55+
return new $class($resData);
56+
},
57+
$data
58+
);
59+
}
60+
61+
62+
/**
63+
* Which class to use for building underlying responses
64+
*
65+
* @return string
66+
*/
67+
abstract protected static function responseClass(): string;
5068

5169
/**
5270
* Meta data of the collection (pagination mostly)

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use ZEROSPAM\Framework\SDK\Response\Api\Collection\CollectionMetaData;
1212
use ZEROSPAM\Framework\SDK\Response\Api\Collection\CollectionResponse;
13-
use ZEROSPAM\Framework\SDK\Response\Api\IResponse;
1413
use ZEROSPAM\Framework\SDK\Test\Base\Data\TestResponse;
1514

1615
class CollectionTestResponse extends CollectionResponse
@@ -25,21 +24,13 @@ public function __construct(array $data)
2524
parent::__construct(new CollectionMetaData($data['meta']), $data['response']);
2625
}
2726

28-
2927
/**
30-
* Transform the basic data (string[]) into a response array (IResponse[])
31-
*
32-
* @param array $data
28+
* Which class to use for building underlying responses
3329
*
34-
* @return IResponse[]
30+
* @return string
3531
*/
36-
public static function dataToResponses(array $data): array
32+
protected static function responseClass(): string
3733
{
38-
return array_map(
39-
function (array $respData) {
40-
return new TestResponse($respData);
41-
},
42-
$data
43-
);
34+
return TestResponse::class;
4435
}
4536
}

0 commit comments

Comments
 (0)