Completed
Push — master ( 93ef1f...d80a95 )
by Christophe
13:13
created

ResolverTrait::resolveValue()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 36
rs 6.7273
cc 7
eloc 20
nc 4
nop 2
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
    private function resolveValue(ContainerInterface $container, $value)
30
    {
31
        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
        if (!is_string($value)) {
40
            return $value;
41
        }
42
43
        $escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($container, $value) {
44
            // skip %%
45
            if (!isset($match[1])) {
46
                return '%%';
47
            }
48
49
            $resolved = $container->getParameter($match[1]);
50
            if (is_string($resolved) || is_numeric($resolved)) {
51
                return (string) $resolved;
52
            }
53
54
            throw new \RuntimeException(sprintf(
55
                    'The container parameter "%s" must be a string or numeric, but it is of type %s.',
56
                    $match[1],
57
                    $value,
58
                    gettype($resolved)
59
                )
60
            );
61
        }, $value);
62
63
        return str_replace('%%', '%', $escapedValue);
64
    }
65
}
66