Skip to content

Commit 811d520

Browse files
authored
Update yiisoft/data-response and refactor (#265)
1 parent 5c333dc commit 811d520

14 files changed

Lines changed: 81 additions & 107 deletions

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
- Bug #256: Fix incorrect .env files used in Docker Compose for production (@aa-chernyh)
77
- Enh #258: Set locale `C.UTF-8` in `Dockerfile` (@vjik)
88
- Bug #260: Fix psalm cache directory in configuration file (@vjik)
9-
- Enh #260: Update composer dependencies (@vjik)
9+
- Enh #260, #265: Update composer dependencies and refactor to replace use of deprecated classes (@vjik)
10+
- Chg #265: Refactor `PresenterInterface` and implementations for preparing data only (@vjik)
1011

1112
## 1.1.0 December 22, 2025
1213

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"yiisoft/aliases": "^3.1.1",
4343
"yiisoft/config": "^1.6.2",
4444
"yiisoft/data": "^1.0.1",
45-
"yiisoft/data-response": "^2.1.2",
45+
"yiisoft/data-response": "^2.2",
4646
"yiisoft/definitions": "^3.4.1",
4747
"yiisoft/di": "^1.4.1",
4848
"yiisoft/error-handler": "^4.3.2",
@@ -54,7 +54,7 @@
5454
"yiisoft/log-target-file": "^3.1",
5555
"yiisoft/middleware-dispatcher": "^5.4",
5656
"yiisoft/request-body-parser": "^1.2.1",
57-
"yiisoft/request-provider": "^1.2",
57+
"yiisoft/request-provider": "^1.3",
5858
"yiisoft/router": "^4.0.2",
5959
"yiisoft/router-fastroute": "^4.0.3",
6060
"yiisoft/validator": "^2.5.1",
@@ -65,19 +65,19 @@
6565
},
6666
"require-dev": {
6767
"codeception/c3": "^2.9",
68-
"codeception/codeception": "^5.3.4",
69-
"codeception/lib-innerbrowser": "^4.0.8",
68+
"codeception/codeception": "^5.3.5",
69+
"codeception/lib-innerbrowser": "^4.1.0",
7070
"codeception/module-asserts": "^3.3.0",
7171
"codeception/module-cli": "^2.0.1",
7272
"codeception/module-db": "^3.2.2",
7373
"codeception/module-phpbrowser": "^3.0.2",
7474
"codeception/module-rest": "^3.4.3",
75-
"friendsofphp/php-cs-fixer": "^3.93.0",
76-
"phpunit/phpunit": "^11.5.49",
77-
"rector/rector": "^2.3.4",
75+
"friendsofphp/php-cs-fixer": "^3.94.2",
76+
"phpunit/phpunit": "^11.5.55",
77+
"rector/rector": "^2.3.8",
7878
"roave/infection-static-analysis-plugin": "^1.40",
7979
"shipmonk/composer-dependency-analyser": "^1.8.4",
80-
"vimeo/psalm": "^6.14.3"
80+
"vimeo/psalm": "^6.15.1"
8181
},
8282
"autoload": {
8383
"psr-4": {

config/web/di/application.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
use App\Api\Shared\ExceptionResponderFactory;
66
use App\Api\Shared\NotFoundMiddleware;
7-
use Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter;
8-
use Yiisoft\DataResponse\Formatter\XmlDataResponseFormatter;
9-
use Yiisoft\DataResponse\Middleware\ContentNegotiator;
10-
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsJson;
7+
use Yiisoft\DataResponse\Formatter\JsonFormatter;
8+
use Yiisoft\DataResponse\Formatter\XmlFormatter;
9+
use Yiisoft\DataResponse\Middleware\ContentNegotiatorDataResponseMiddleware;
1110
use Yiisoft\Definitions\DynamicReference;
1211
use Yiisoft\Definitions\Reference;
1312
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
@@ -29,11 +28,13 @@
2928
'class' => MiddlewareDispatcher::class,
3029
'withMiddlewares()' => [
3130
[
32-
FormatDataResponseAsJson::class,
33-
static fn() => new ContentNegotiator([
34-
'application/xml' => new XmlDataResponseFormatter(),
35-
'application/json' => new JsonDataResponseFormatter(),
36-
]),
31+
static fn() => new ContentNegotiatorDataResponseMiddleware(
32+
formatters: [
33+
'application/xml' => new XmlFormatter(),
34+
'application/json' => new JsonFormatter(),
35+
],
36+
fallback: new JsonFormatter(),
37+
),
3738
ErrorCatcher::class,
3839
static fn(ExceptionResponderFactory $factory) => $factory->create(),
3940
RequestBodyParser::class,

src/Api/Shared/Presenter/AsIsPresenter.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44

55
namespace App\Api\Shared\Presenter;
66

7-
use Yiisoft\DataResponse\DataResponse;
8-
97
/**
108
* @implements PresenterInterface<mixed>
119
*/
1210
final readonly class AsIsPresenter implements PresenterInterface
1311
{
14-
public function present(mixed $value, DataResponse $response): DataResponse
12+
public function present(mixed $value): mixed
1513
{
16-
return $response->withData($value);
14+
return $value;
1715
}
1816
}

src/Api/Shared/Presenter/CollectionPresenter.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace App\Api\Shared\Presenter;
66

7-
use Yiisoft\DataResponse\DataResponse;
8-
97
/**
108
* @implements PresenterInterface<iterable>
119
*/
@@ -15,13 +13,12 @@ public function __construct(
1513
private PresenterInterface $itemPresenter = new AsIsPresenter(),
1614
) {}
1715

18-
public function present(mixed $value, DataResponse $response): DataResponse
16+
public function present(mixed $value): array
1917
{
2018
$result = [];
2119
foreach ($value as $item) {
22-
$response = $this->itemPresenter->present($item, $response);
23-
$result[] = $response->getData();
20+
$result[] = $this->itemPresenter->present($item);
2421
}
25-
return $response->withData($result);
22+
return $result;
2623
}
2724
}

src/Api/Shared/Presenter/FailPresenter.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace App\Api\Shared\Presenter;
66

7-
use Yiisoft\DataResponse\DataResponse;
8-
use Yiisoft\Http\Status;
9-
107
/**
118
* @implements PresenterInterface<mixed>
129
*/
@@ -15,13 +12,11 @@
1512
public function __construct(
1613
private string $message = 'Unknown error.',
1714
private int|null $code = null,
18-
private int $httpCode = Status::BAD_REQUEST,
1915
private PresenterInterface $presenter = new AsIsPresenter(),
2016
) {}
2117

22-
public function present(mixed $value, DataResponse $response): DataResponse
18+
public function present(mixed $value): mixed
2319
{
24-
$response = $this->presenter->present($value, $response);
2520
$result = [
2621
'status' => 'failed',
2722
'error_message' => $this->message,
@@ -30,8 +25,8 @@ public function present(mixed $value, DataResponse $response): DataResponse
3025
$result['error_code'] = $this->code;
3126
}
3227
if ($value !== null) {
33-
$result['error_data'] = $response->getData();
28+
$result['error_data'] = $this->presenter->present($value);
3429
}
35-
return $response->withData($result)->withStatus($this->httpCode);
30+
return $result;
3631
}
3732
}

src/Api/Shared/Presenter/OffsetPaginatorPresenter.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace App\Api\Shared\Presenter;
66

77
use Yiisoft\Data\Paginator\OffsetPaginator;
8-
use Yiisoft\DataResponse\DataResponse;
98

109
/**
1110
* @implements PresenterInterface<OffsetPaginator>
@@ -20,14 +19,13 @@ public function __construct(
2019
$this->collectionPresenter = new CollectionPresenter($itemPresenter);
2120
}
2221

23-
public function present(mixed $value, DataResponse $response): DataResponse
22+
public function present(mixed $value): array
2423
{
25-
$collectionResponse = $this->collectionPresenter->present($value->read(), $response);
26-
return $collectionResponse->withData([
27-
'items' => $collectionResponse->getData(),
24+
return [
25+
'items' => $this->collectionPresenter->present($value->read()),
2826
'pageSize' => $value->getPageSize(),
2927
'currentPage' => $value->getCurrentPage(),
3028
'totalPages' => $value->getTotalPages(),
31-
]);
29+
];
3230
}
3331
}

src/Api/Shared/Presenter/PresenterInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace App\Api\Shared\Presenter;
66

7-
use Yiisoft\DataResponse\DataResponse;
8-
97
/**
108
* @template T
119
*/
@@ -14,5 +12,5 @@ interface PresenterInterface
1412
/**
1513
* @param T $value
1614
*/
17-
public function present(mixed $value, DataResponse $response): DataResponse;
15+
public function present(mixed $value): mixed;
1816
}

src/Api/Shared/Presenter/SuccessPresenter.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace App\Api\Shared\Presenter;
66

7-
use Yiisoft\DataResponse\DataResponse;
8-
use Yiisoft\Http\Status;
9-
107
/**
118
* @implements PresenterInterface<mixed>
129
*/
@@ -16,14 +13,11 @@ public function __construct(
1613
private PresenterInterface $presenter = new AsIsPresenter(),
1714
) {}
1815

19-
public function present(mixed $value, DataResponse $response): DataResponse
16+
public function present(mixed $value): array
2017
{
21-
$response = $this->presenter->present($value, $response);
22-
return $response
23-
->withData([
24-
'status' => 'success',
25-
'data' => $response->getData(),
26-
])
27-
->withStatus(Status::OK);
18+
return [
19+
'status' => 'success',
20+
'data' => $this->presenter->present($value),
21+
];
2822
}
2923
}

src/Api/Shared/Presenter/ValidationResultPresenter.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44

55
namespace App\Api\Shared\Presenter;
66

7-
use Yiisoft\DataResponse\DataResponse;
87
use Yiisoft\Validator\Result;
98

109
/**
1110
* @implements PresenterInterface<Result>
1211
*/
1312
final readonly class ValidationResultPresenter implements PresenterInterface
1413
{
15-
public function present(mixed $value, DataResponse $response): DataResponse
14+
public function present(mixed $value): array
1615
{
17-
return $response->withData(
18-
$value->getErrorMessagesIndexedByPath(),
19-
);
16+
return $value->getErrorMessagesIndexedByPath();
2017
}
2118
}

0 commit comments

Comments
 (0)