Completed
Push — dx/improve-parameters-parser ( 0a7ab9...dabe13 )
by Kamil
22:11
created

ParametersParser::parseRequestValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
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
namespace Sylius\Bundle\ResourceBundle\Controller;
13
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * @author Paweł Jędrzejewski <[email protected]>
20
 * @author Dosena Ishmael <[email protected]>
21
 */
22
final class ParametersParser implements ParametersParserInterface
23
{
24
    /**
25
     * @var ContainerInterface
26
     */
27
    private $container;
28
29
    /**
30
     * @var ExpressionLanguage
31
     */
32
    private $expression;
33
34
    /**
35
     * @param ContainerInterface $container
36
     * @param ExpressionLanguage $expression
37
     */
38
    public function __construct(ContainerInterface $container, ExpressionLanguage $expression)
39
    {
40
        $this->container = $container;
41
        $this->expression = $expression;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function parseRequestValues(array $parameters, Request $request)
48
    {
49
        return array_map(function ($parameter) use ($request) {
50
            if (is_array($parameter)) {
51
                return $this->parseRequestValues($parameter, $request);
52
            }
53
54
            return $this->parseRequestValue($parameter, $request);
55
        }, $parameters);
56
    }
57
58
    /**
59
     * @param mixed $parameter
60
     * @param Request $request
61
     *
62
     * @return mixed
63
     */
64
    private function parseRequestValue($parameter, Request $request)
65
    {
66
        if (0 === strpos($parameter, '$')) {
67
            return $request->get(substr($parameter, 1));
68
        }
69
70
        if (0 === strpos($parameter, 'expr:')) {
71
            return $this->parseRequestValueExpression(substr($parameter, 5), $request);
72
        }
73
74
        return $parameter;
75
    }
76
77
    /**
78
     * @param string $expression
79
     * @param Request $request
80
     *
81
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
82
     */
83
    private function parseRequestValueExpression($expression, Request $request)
84
    {
85
        $expression = preg_replace_callback('/(\$\w+)/', function ($matches) use ($request) {
86
            $variable = $request->get(substr($matches[1], 1));
87
88
            return is_string($variable) ? sprintf('"%s"', $variable) : $variable;
89
        }, $expression);
90
91
        return $this->expression->evaluate($expression, ['container' => $this->container]);
92
    }
93
}
94