1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sylius\Bundle\ResourceBundle\Controller; |
15
|
|
|
|
16
|
|
|
use Sylius\Component\Resource\Metadata\MetadataInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class RequestConfigurationFactory implements RequestConfigurationFactoryInterface |
23
|
|
|
{ |
24
|
|
|
const API_VERSION_HEADER = 'Accept'; |
25
|
|
|
const API_GROUPS_HEADER = 'Accept'; |
26
|
|
|
|
27
|
|
|
const API_VERSION_REGEXP = '/(v|version)=(?P<version>[0-9\.]+)/i'; |
28
|
|
|
const API_GROUPS_REGEXP = '/(g|groups)=(?P<groups>[a-z,_\s]+)/i'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ParametersParserInterface |
32
|
|
|
*/ |
33
|
|
|
private $parametersParser; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $configurationClass; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $defaultParameters; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ParametersParserInterface $parametersParser |
47
|
|
|
* @param string $configurationClass |
48
|
|
|
* @param array $defaultParameters |
49
|
|
|
*/ |
50
|
|
|
public function __construct(ParametersParserInterface $parametersParser, $configurationClass, array $defaultParameters = []) |
51
|
|
|
{ |
52
|
|
|
$this->parametersParser = $parametersParser; |
53
|
|
|
$this->configurationClass = $configurationClass; |
54
|
|
|
$this->defaultParameters = $defaultParameters; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function create(MetadataInterface $metadata, Request $request) |
61
|
|
|
{ |
62
|
|
|
$parameters = array_merge($this->defaultParameters, $this->parseApiParameters($request)); |
63
|
|
|
$parameters = $this->parametersParser->parseRequestValues($parameters, $request); |
64
|
|
|
|
65
|
|
|
return new $this->configurationClass($metadata, $request, new Parameters($parameters)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Request $request |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
* |
73
|
|
|
* @throws \InvalidArgumentException |
74
|
|
|
*/ |
75
|
|
|
private function parseApiParameters(Request $request) |
76
|
|
|
{ |
77
|
|
|
$parameters = []; |
78
|
|
|
|
79
|
|
|
$apiVersionHeaders = $request->headers->get(self::API_VERSION_HEADER, null, false); |
80
|
|
|
foreach ($apiVersionHeaders as $apiVersionHeader) { |
|
|
|
|
81
|
|
|
if (preg_match(self::API_VERSION_REGEXP, $apiVersionHeader, $matches)) { |
82
|
|
|
$parameters['serialization_version'] = $matches['version']; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$apiGroupsHeaders = $request->headers->get(self::API_GROUPS_HEADER, null, false); |
87
|
|
|
foreach ($apiGroupsHeaders as $apiGroupsHeader) { |
|
|
|
|
88
|
|
|
if (preg_match(self::API_GROUPS_REGEXP, $apiGroupsHeader, $matches)) { |
89
|
|
|
$parameters['serialization_groups'] = array_map('trim', explode(',', $matches['groups'])); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return array_merge($request->attributes->get('_sylius', []), $parameters); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.