Completed
Push — master ( 989494...62f72e )
by Kamil
17s
created

OptionsParser::parseOptions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 1
nop 3
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\Grid\Parser;
13
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
18
19
/**
20
 * @author Grzegorz Sadowski <[email protected]>
21
 */
22
final class OptionsParser implements OptionsParserInterface
23
{
24
    /**
25
     * @var ContainerInterface
26
     */
27
    private $container;
28
29
    /**
30
     * @var ExpressionLanguage
31
     */
32
    private $expression;
33
34
    /**
35
     * @var PropertyAccessorInterface
36
     */
37
    private $propertyAccessor;
38
39
    /**
40
     * @param ContainerInterface $container
41
     * @param ExpressionLanguage $expression
42
     * @param PropertyAccessorInterface $propertyAccessor
43
     */
44
    public function __construct(
45
        ContainerInterface $container,
46
        ExpressionLanguage $expression,
47
        PropertyAccessorInterface $propertyAccessor
48
    ) {
49
        $this->container = $container;
50
        $this->expression = $expression;
51
        $this->propertyAccessor = $propertyAccessor;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function parseOptions(array $parameters, Request $request, $data = null)
58
    {
59
        return array_map(function ($parameter) use ($request, $data) {
60
            if (is_array($parameter)) {
61
                return $this->parseOptions($parameter, $request, $data);
62
            }
63
64
            return $this->parseOption($parameter, $request, $data);
65
        }, $parameters);
66
    }
67
68
    /**
69
     * @param mixed $parameter
70
     * @param Request $request
71
     * @param mixed $data
72
     *
73
     * @return mixed
74
     */
75
    private function parseOption($parameter, Request $request, $data)
76
    {
77
        if (0 === strpos($parameter, '$')) {
78
            return $request->get(substr($parameter, 1));
79
        }
80
81
        if (0 === strpos($parameter, 'expr:')) {
82
            return $this->parseOptionExpression(substr($parameter, 5), $request);
83
        }
84
85
        if (0 === strpos($parameter, 'resource.')) {
86
            return $this->parseOptionResourceField(substr($parameter, 9), $data);
87
        }
88
89
        return $parameter;
90
    }
91
92
    /**
93
     * @param string $expression
94
     * @param Request $request
95
     *
96
     * @return string
97
     */
98
    private function parseOptionExpression($expression, Request $request)
99
    {
100
        $expression = preg_replace_callback('/\$(\w+)/', function ($matches) use ($request) {
101
            $variable = $request->get($matches[1]);
102
103
            return is_string($variable) ? sprintf('"%s"', $variable) : $variable;
104
        }, $expression);
105
106
        return $this->expression->evaluate($expression, ['container' => $this->container]);
107
    }
108
109
    /**
110
     * @param string $value
111
     * @param mixed $data
112
     *
113
     * @return string
114
     */
115
    private function parseOptionResourceField($value, $data)
116
    {
117
        return $this->propertyAccessor->getValue($data, $value);
118
    }
119
}
120