|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the StfalconApiBundle. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Stfalcon LLC <stfalcon.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace StfalconStudio\ApiBundle\Request; |
|
14
|
|
|
|
|
15
|
|
|
use StfalconStudio\ApiBundle\DTO\DtoInterface; |
|
16
|
|
|
use StfalconStudio\ApiBundle\Exception\InvalidArgumentException; |
|
17
|
|
|
use StfalconStudio\ApiBundle\Service\AnnotationProcessor\DtoAnnotationProcessor; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
19
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
|
20
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* DtoExtractor. |
|
24
|
|
|
*/ |
|
25
|
|
|
class DtoExtractor |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var DtoAnnotationProcessor */ |
|
28
|
|
|
private $dtoAnnotationProcessor; |
|
29
|
|
|
|
|
30
|
|
|
/** @var SerializerInterface */ |
|
31
|
|
|
private $serializer; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param DtoAnnotationProcessor $dtoAnnotationProcessor |
|
35
|
|
|
* @param SerializerInterface $serializer |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(DtoAnnotationProcessor $dtoAnnotationProcessor, SerializerInterface $serializer) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->dtoAnnotationProcessor = $dtoAnnotationProcessor; |
|
40
|
|
|
$this->serializer = $serializer; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Request $request |
|
45
|
|
|
* @param string $controllerClassName |
|
46
|
|
|
* @param object|null $objectToPopulate |
|
47
|
|
|
* |
|
48
|
|
|
* @return DtoInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getDtoFromRequestForControllerClass(Request $request, string $controllerClassName, object $objectToPopulate = null): DtoInterface |
|
51
|
|
|
{ |
|
52
|
|
|
$dtoClassName = $this->dtoAnnotationProcessor->processAnnotationForClass($controllerClassName); |
|
53
|
|
|
|
|
54
|
|
|
return $this->getDtoFromRequestForDtoClass($request, $dtoClassName, $objectToPopulate); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param Request $request |
|
59
|
|
|
* @param string $dtoClassName |
|
60
|
|
|
* @param object|null $objectToPopulate |
|
61
|
|
|
* |
|
62
|
|
|
* @throws InvalidArgumentException |
|
63
|
|
|
* |
|
64
|
|
|
* @return DtoInterface |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getDtoFromRequestForDtoClass(Request $request, string $dtoClassName, object $objectToPopulate = null): DtoInterface |
|
67
|
|
|
{ |
|
68
|
|
|
$context = []; |
|
69
|
|
|
if (null !== $objectToPopulate) { |
|
70
|
|
|
$context = [AbstractNormalizer::OBJECT_TO_POPULATE => $objectToPopulate]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$object = $this->serializer->deserialize($request->getContent(), $dtoClassName, 'json', $context); |
|
74
|
|
|
|
|
75
|
|
|
if (!$object instanceof DtoInterface) { |
|
76
|
|
|
throw new InvalidArgumentException(\sprintf('DtoExtractor supports only classes which implement %s', DtoInterface::class)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $object; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|