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