BodyParser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 76
rs 10
c 1
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parse() 0 3 1
A addParser() 0 12 2
A createParserWith() 0 11 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of slick/http
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Slick\Http\Message\Server;
13
14
use Psr\Http\Message\StreamInterface;
15
use Slick\Http\Message\Exception\InvalidArgumentException;
16
use Slick\Http\Message\Server\BodyParser\JsonParser;
17
use Slick\Http\Message\Server\BodyParser\NullParser;
18
use Slick\Http\Message\Server\BodyParser\UrlEncodedParser;
19
use Slick\Http\Message\Server\BodyParser\XmlParser;
20
21
/**
22
 * BodyParser
23
 *
24
 * @package Slick\Http\Message\Server
25
*/
26
class BodyParser
27
{
28
    /**
29
     * @var string
30
     */
31
    private $contentType;
32
33
    /**
34
     * @var array<string, array<string>>
35
     */
36
    private static array $parsers = [
37
        JsonParser::class => ['+json', 'application/json'],
38
        XmlParser::class  => ['+xml', 'text/xml'],
39
        UrlEncodedParser::class => ['urlencoded']
40
    ];
41
42
    /**
43
     * Creates a Body Parser for the provided header
44
     * @param string $contentType
45
     */
46
    public function __construct(string $contentType)
47
    {
48
        $this->contentType = $contentType;
49
    }
50
51
    /**
52
     * Parses provided the message body
53
     *
54
     * @param StreamInterface $body
55
     * @return mixed
56
     */
57
    public function parse(StreamInterface $body)
58
    {
59
        return $this->createParserWith($body)->parse();
60
    }
61
62
    /**
63
     * Creates the parser based on the parsers map with provided stream
64
     *
65
     * @param StreamInterface $body
66
     *
67
     * @return BodyParserInterface
68
     */
69
    private function createParserWith(StreamInterface $body)
70
    {
71
        $class = NullParser::class;
72
        foreach (self::$parsers as $parser => $contentTypes) {
73
            foreach ($contentTypes as $contentType) {
74
                if (stripos($this->contentType, $contentType) !== false) {
75
                    $class = $parser;
76
                }
77
            }
78
        }
79
        return new $class($body);
80
    }
81
82
    /**
83
     * Adds a body parser to parsers map
84
     *
85
     * @param string $className
86
     * @param array<string>  $contentTypes
87
     *
88
     * @throws InvalidArgumentException if provided class does not implement BodyParserInterface
89
     */
90
    public static function addParser(string $className, array $contentTypes): void
91
    {
92
        if (! is_subclass_of($className, BodyParserInterface::class)) {
93
            throw new InvalidArgumentException(
94
                "Parser objects MUST implement the BodyParserInterface interface."
95
            );
96
        }
97
98
        $existing = self::$parsers[$className] ?? [];
99
        array_unshift(
100
            self::$parsers[$className],
101
            array_merge($existing, $contentTypes)
102
        );
103
    }
104
}
105