Completed
Push — master ( b1054d...eba5f2 )
by Christian
07:28 queued 11s
created

ResolverTrait::resolveValue()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.9295

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 11
cts 15
cp 0.7332
rs 8.5066
c 0
b 0
f 0
cc 7
nc 4
nop 2
crap 7.9295
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\RestBundle\Util;
13
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
16
/**
17
 * @author Ener-Getick <[email protected]>
18
 *
19
 * @internal do not use this trait or its functions in your code
20
 */
21
trait ResolverTrait
22
{
23
    /**
24
     * @param ContainerInterface $container
25
     * @param mixed              $value
26
     *
27
     * @return mixed
28
     */
29 13
    private function resolveValue(ContainerInterface $container, $value)
30
    {
31 13
        if (is_array($value)) {
32
            foreach ($value as $key => $val) {
33
                $value[$key] = $this->resolveValue($container, $val);
34
            }
35
36
            return $value;
37
        }
38
39 13
        if (!is_string($value)) {
40 7
            return $value;
41
        }
42
43
        $escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($container, $value) {
44
            // skip %%
45 6
            if (!isset($match[1])) {
46 6
                return '%%';
47
            }
48
49 6
            $resolved = $container->getParameter($match[1]);
50 6
            if (is_string($resolved) || is_numeric($resolved)) {
51 6
                return (string) $resolved;
52
            }
53
54
            throw new \RuntimeException(sprintf('The container parameter "%s" must be a string or numeric, but it is of type %s.', $match[1], gettype($resolved)));
55 12
        }, $value);
56
57 12
        return str_replace('%%', '%', $escapedValue);
58
    }
59
}
60