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

slimphp / Slim / 15373867066

01 Jun 2025 10:16AM UTC coverage: 98.808% (+0.1%) from 98.663%
15373867066

push

github

web-flow
Add v5

124 of 128 new or added lines in 9 files covered. (96.88%)

995 of 1007 relevant lines covered (98.81%)

37.31 hits per line

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

94.44
/Slim/Middleware/XmlBodyParserMiddleware.php
1
<?php
2

3
/**
4
 * Slim Framework (https://slimframework.com)
5
 *
6
 * @license https://github.com/slimphp/Slim/blob/5.x/LICENSE.md (MIT License)
7
 */
8

9
declare(strict_types=1);
10

11
namespace Slim\Middleware;
12

13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Psr\Http\Server\MiddlewareInterface;
16
use Psr\Http\Server\RequestHandlerInterface;
17
use Slim\Exception\HttpBadRequestException;
18

19
final class XmlBodyParserMiddleware implements MiddlewareInterface
20
{
21
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
22
    {
23
        $method = $request->getMethod();
4✔
24
        $contentType = $request->getHeaderLine('Content-Type');
4✔
25

26
        if (!in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE'], true)) {
4✔
NEW
27
            return $handler->handle($request);
×
28
        }
29

30
        if ($this->isXmlMediaType($contentType)) {
4✔
31
            $backup = libxml_use_internal_errors(true);
3✔
32
            $body = (string)$request->getBody();
3✔
33
            $xml = simplexml_load_string($body);
3✔
34

35
            libxml_clear_errors();
3✔
36
            libxml_use_internal_errors($backup);
3✔
37

38
            if ($xml === false) {
3✔
39
                throw new HttpBadRequestException($request, 'Invalid XML body');
1✔
40
            }
41

42
            $request = $request->withParsedBody($xml);
2✔
43
        }
44

45
        return $handler->handle($request);
3✔
46
    }
47

48
    private function isXmlMediaType(string $contentType): bool
49
    {
50
        $contentType = strtolower(trim(explode(';', $contentType)[0]));
4✔
51

52
        return $contentType === 'application/xml'
4✔
53
            || $contentType === 'text/xml'
4✔
54
            || str_ends_with($contentType, '+xml');
4✔
55
    }
56
}
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