Completed
Push — master ( bca24d...1b0d8b )
by John
03:18
created

RequestParameterAssembler::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions 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
namespace KleijnWeb\PhpApi\Descriptions\Request;
9
10
use KleijnWeb\PhpApi\Descriptions\Description\Operation;
11
use KleijnWeb\PhpApi\Descriptions\Description\Parameter;
12
use Psr\Http\Message\ServerRequestInterface;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class RequestParameterAssembler
18
{
19
    /**
20
     * @var ParameterCoercer
21
     */
22
    private $parameterCoercer;
23
24
    /**
25
     * ApiRequest constructor.
26
     *
27
     * @param ParameterCoercer $parameterCoercer
28
     */
29
    public function __construct(ParameterCoercer $parameterCoercer = null)
30
    {
31
        $this->parameterCoercer = $parameterCoercer ?: new ParameterCoercer();
32
    }
33
34
    /**
35
     * @param ServerRequestInterface $httpRequest
36
     * @param Operation              $operation
37
     *
38
     * @return \stdClass
39
     */
40
    public function getRequestParameters(ServerRequestInterface $httpRequest, Operation $operation): \stdClass
41
    {
42
        $indexed    = array_combine(
43
            explode('/', trim($operation->getPath(), '/')),
44
            explode('/', trim($httpRequest->getUri()->getPath(), '/'))
45
        );
46
        $pathParams = (object)[];
47
48
        foreach ($indexed as $key => $value) {
49
            if (0 == strpos('{', $key)) {
50
                $pathParams->{trim($key, '{}')} = $value;
51
            }
52
        }
53
54
        $parameters     = (object)[];
55
        $queryParams    = (object)$httpRequest->getQueryParams();
56
        $headers        = $httpRequest->getHeaders();
57
        $headerParamMap = array_combine(array_map(function ($key) {
58
            return $this->getHeaderParameterName($key);
59
        }, array_keys($headers)), array_keys($headers));
60
61
        foreach ($operation->getParameters() as $parameter) {
62
            $paramName = $parameter->getName();
63
64
            switch ($parameter->getIn()) {
65 View Code Duplication
                case Parameter::IN_QUERY:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
                    if (isset($queryParams->$paramName)) {
67
                        $parameters->$paramName = $this->parameterCoercer->coerce($parameter, $queryParams->$paramName);
68
                    }
69
                    break;
70
                case Parameter::IN_BODY:
71
                    $parameters->$paramName = $httpRequest->getParsedBody();
72
                    break;
73 View Code Duplication
                case Parameter::IN_PATH:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
                    if (isset($pathParams->$paramName)) {
75
                        $parameters->$paramName = $this->parameterCoercer->coerce($parameter, $pathParams->$paramName);
76
                    }
77
                    break;
78
                case Parameter::IN_HEADER:
79
                    if (isset($headers[$paramName])) {
80
                        $value = $headers[$paramName];
81
                    } elseif (isset($headerParamMap[$paramName])) {
82
                        $value = $headers[$headerParamMap[$paramName]];
83
                    } else {
84
                        break;
85
                    }
86
                    $parameters->$paramName = $this->parameterCoercer->coerce($parameter, $value);
87
                    break;
88
            }
89
        }
90
91
        return $parameters;
92
    }
93
94
    private function getHeaderParameterName($headerName)
95
    {
96
        $replacements = [
97
            function ($matches) {
98
                return strtolower($matches[2]);
99
            },
100
            function ($matches) {
101
                return strtoupper($matches[2]);
102
            },
103
            function ($matches) {
104
                return strtoupper($matches[1]);
105
            },
106
        ];
107
108
        foreach (['/^(X-)?(.*)/i', '/(\-)([\S]{1})/', '/(^[\S]{1})/',] as $index => $pattern) {
109
            $headerName = preg_replace_callback($pattern, $replacements[$index], $headerName);
110
        }
111
112
        return lcfirst($headerName);
113
    }
114
}
115