• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

valkyrjaio / valkyrja / 15659546660

15 Jun 2025 04:39AM UTC coverage: 47.202% (-0.4%) from 47.589%
15659546660

push

github

MelechMizrachi
Update Config.

5331 of 11294 relevant lines covered (47.2%)

16.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Valkyrja/Http/Routing/Middleware/ResponseStructMiddleware.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <melechmizrachi@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Valkyrja\Http\Routing\Middleware;
15

16
use Valkyrja\Http\Message\Request\Contract\ServerRequest;
17
use Valkyrja\Http\Message\Response\Contract\JsonResponse;
18
use Valkyrja\Http\Message\Response\Contract\Response;
19
use Valkyrja\Http\Middleware\Contract\RouteDispatchedMiddleware;
20
use Valkyrja\Http\Middleware\Handler\Contract\RouteDispatchedHandler;
21
use Valkyrja\Http\Routing\Model\Contract\Route;
22
use Valkyrja\Http\Struct\Response\Contract\ResponseStruct;
23

24
/**
25
 * Class StructRouteDispatchedMiddleware.
26
 *
27
 * @author Melech Mizrachi
28
 */
29
class ResponseStructMiddleware implements RouteDispatchedMiddleware
30
{
31
    /**
32
     * @inheritDoc
33
     */
34
    public function routeDispatched(ServerRequest $request, Response $response, Route $route, RouteDispatchedHandler $handler): Response
35
    {
36
        $responseStruct = $route->getResponseStruct();
×
37

38
        if ($responseStruct !== null && $response instanceof JsonResponse) {
×
39
            $response = $this->updateJsonWithResponseStruct($response, $responseStruct);
×
40
        }
41

42
        return $handler->routeDispatched($request, $response, $route);
×
43
    }
44

45
    /**
46
     * Update the Json in a response with a given response struct.
47
     *
48
     * @param JsonResponse                 $response       The json response
49
     * @param class-string<ResponseStruct> $responseStruct The response struct
50
     *
51
     * @return JsonResponse
52
     */
53
    protected function updateJsonWithResponseStruct(JsonResponse $response, string $responseStruct): JsonResponse
54
    {
55
        $data = $response->getBodyAsJson();
×
56

57
        /** @psalm-suppress MixedArgumentTypeCoercion */
58
        return $response->withJsonAsBody($responseStruct::getStructuredData($data));
×
59
    }
60
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc