Interpolator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 39
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A interpolate() 0 4 1
A getConfig() 0 5 1
A get() 0 8 3
A interpolateArray() 0 10 4
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\base;
12
13
use Yii;
14
use yii\helpers\ArrayHelper;
15
16
/**
17
 * Interpolates array recursively.
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class Interpolator
21
{
22
    public $data;
23
24
    public function interpolate(&$data)
25
    {
26
        $this->data = &$data;
27
        $this->interpolateArray($data);
28
    }
29
30
    private function interpolateArray(&$data)
31
    {
32
        if (is_array($data)) {
33
            foreach ($data as &$item) {
34
                $this->interpolateArray($item);
35
            }
36
        } elseif (is_string($data)) {
37
            $data = preg_replace_callback('/\\$(\\w+)\\[\'(.+?)\'\\]/', function ($matches) {
38
                return $this->get($matches[1], $matches[2]);
39
            }, $data);
40
        }
41
    }
42
43
    public function get($scope, $name)
44
    {
45
        if ($scope === 'params') {
46
            return $this->data['params'][$name];
47
        } elseif ($scope === '_ENV') {
48
            return $_ENV[$name];
49
        } else {
50
            return "\$${scope}['$name']";
51
        }
52
    }
53
54
    public function getConfig($name)
55
    {
56
        list($goal, $subname) = explode('.', $name, 2);
57
58
        return ArrayHelper::getValue(Yii::$app->get('config')->getGoal($goal), $subname);
59
    }
60
}
61