1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Request; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Document\DocumentRepository; |
12
|
|
|
use KleijnWeb\SwaggerBundle\Document\Specification\Operation; |
13
|
|
|
use KleijnWeb\SwaggerBundle\Exception\MalformedContentException; |
14
|
|
|
use KleijnWeb\SwaggerBundle\Exception\UnsupportedContentTypeException; |
15
|
|
|
use KleijnWeb\SwaggerBundle\Serialize\SerializationTypeResolver; |
16
|
|
|
use KleijnWeb\SwaggerBundle\Serialize\Serializer; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author John Kleijn <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class ContentDecoder |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Serializer |
26
|
|
|
*/ |
27
|
|
|
private $serializer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var SerializationTypeResolver |
31
|
|
|
*/ |
32
|
|
|
private $typeResolver; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var DocumentRepository |
36
|
|
|
*/ |
37
|
|
|
private $documentRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Serializer $serializer |
41
|
|
|
* @param DocumentRepository $documentRepository |
42
|
|
|
* @param SerializationTypeResolver $typeResolver |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
Serializer $serializer, |
46
|
|
|
DocumentRepository $documentRepository, |
47
|
|
|
SerializationTypeResolver $typeResolver = null |
48
|
|
|
) { |
49
|
|
|
$this->serializer = $serializer; |
50
|
|
|
$this->documentRepository = $documentRepository; |
51
|
|
|
$this->typeResolver = $typeResolver; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Request $request |
56
|
|
|
* @param Operation $operationObject |
57
|
|
|
* |
58
|
|
|
* @return mixed |
59
|
|
|
* @throws MalformedContentException |
60
|
|
|
* @throws UnsupportedContentTypeException |
61
|
|
|
*/ |
62
|
|
|
public function decodeContent(Request $request, Operation $operationObject) |
63
|
|
|
{ |
64
|
|
|
if ($content = $request->getContent()) { |
65
|
|
|
if (!$request->attributes->get('_definition')) { |
66
|
|
|
throw new \LogicException("Request does not contain reference to definition"); |
67
|
|
|
} |
68
|
|
|
$specification = $this->documentRepository->get($request->get('_definition')); |
69
|
|
|
$type = $this->typeResolver ? $this->typeResolver->resolveOperationBodyType($operationObject) : ''; |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
return $this->serializer->deserialize($content, $type, $specification); |
73
|
|
|
} catch (\Exception $e) { |
74
|
|
|
throw new MalformedContentException("Unable to decode payload", 400, $e); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|