1 | <?php |
||
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) |
||
48 | |||
49 | /** |
||
50 | * Parses provided message body |
||
51 | * |
||
52 | * @param StreamInterface $body |
||
53 | * @return mixed |
||
54 | */ |
||
55 | public function parse(StreamInterface $body) |
||
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) |
||
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) |
||
104 | } |