|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Request; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
6
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
7
|
|
|
|
|
8
|
|
|
trait UploadParserTrait |
|
9
|
|
|
{ |
|
10
|
|
|
private function handleUploadedFiles(array $parameters, array $files) |
|
11
|
|
|
{ |
|
12
|
|
|
$payload = $this->normalized($parameters); |
|
13
|
|
|
if ($this->isUploadPayload($payload)) { |
|
14
|
|
|
return $this->bindUploadedFiles($payload['operations'], $payload['map'], $files); |
|
15
|
|
|
} else { |
|
16
|
|
|
return $parameters; |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
private function bindUploadedFiles(array $operations, array $map, array $files) |
|
21
|
|
|
{ |
|
22
|
|
|
$accessor = PropertyAccess::createPropertyAccessorBuilder() |
|
23
|
|
|
->enableExceptionOnInvalidIndex() |
|
24
|
|
|
->getPropertyAccessor(); |
|
25
|
|
|
|
|
26
|
|
|
foreach ($map as $fileName => $locations) { |
|
27
|
|
|
foreach ($locations as $location) { |
|
28
|
|
|
$fileKey = sprintf('[%s]', $fileName); |
|
29
|
|
|
if (!$accessor->isReadable($files, $fileKey)) { |
|
30
|
|
|
throw new BadRequestHttpException(sprintf('File %s is missing in the request.', json_encode($fileName))); |
|
31
|
|
|
} |
|
32
|
|
|
$file = $accessor->getValue($files, $fileKey); |
|
33
|
|
|
$locationKey = $this->locationToPropertyAccessPath($location); |
|
34
|
|
|
if (!$accessor->isReadable($operations, $locationKey)) { |
|
35
|
|
|
throw new BadRequestHttpException(sprintf('Map entry %s could not be localized in operations.', json_encode($location))); |
|
36
|
|
|
} |
|
37
|
|
|
$accessor->setValue($operations, $locationKey, $file); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $operations; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function isUploadPayload(array $payload) |
|
45
|
|
|
{ |
|
46
|
|
|
if (isset($payload['operations']) && isset($payload['map']) && is_array($payload['operations']) && is_array($payload['map'])) { |
|
47
|
|
|
$payloadKeys = array_keys($payload); |
|
48
|
|
|
// the specs says that operations must be place before map |
|
49
|
|
|
$operationsPosition = array_search('operations', $payloadKeys); |
|
50
|
|
|
$mapPosition = array_search('map', $payloadKeys); |
|
51
|
|
|
|
|
52
|
|
|
return $operationsPosition < $mapPosition; |
|
53
|
|
|
} else { |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function locationToPropertyAccessPath($location) |
|
59
|
|
|
{ |
|
60
|
|
|
return array_reduce( |
|
61
|
|
|
explode('.', $location), |
|
62
|
|
|
function ($carry, $item) { |
|
63
|
|
|
return sprintf('%s[%s]', $carry, $item); |
|
64
|
|
|
} |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function normalized(array $parsedBody) |
|
69
|
|
|
{ |
|
70
|
|
|
foreach (['operations', 'map'] as $key) { |
|
71
|
|
|
if (isset($parsedBody[$key]) && is_string($parsedBody[$key])) { |
|
72
|
|
|
$parsedBody[$key] = json_decode($parsedBody[$key], true); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $parsedBody; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|