BodyParser   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 22
c 1
b 0
f 0
dl 0
loc 78
rs 10

4 Methods

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