Completed
Push — master ( 8a5a61...9f7ab1 )
by Andrii
12:42
created

Interpolator::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace hidev\components;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
8
class Interpolator
9
{
10
    public function interpolate(&$data)
11
    {
12
        if (is_array($data)) {
13
            foreach ($data as &$item) {
14
                $this->interpolate($item);
15
            }
16
        } elseif (is_string($data)) {
17
            $data = preg_replace_callback('/\{{ (.*?) }}/', function ($matches) {
18
                return $this->get($matches[1]);
19
            }, $data);
20
        }
21
    }
22
23
    public function get($name)
24
    {
25
        list($scope, $subname) = explode('.', $name, 2);
26
27
        if ($scope === 'params') {
28
            return Yii::$app->params[$subname];
29
        } elseif ($scope === 'config') {
30
            return $this->getConfig($subname);
31
        } else {
32
            return $this->getConfig($name);
33
        }
34
    }
35
36
    public function getConfig($name)
37
    {
38
        list($goal, $subname) = explode('.', $name, 2);
39
40
        return ArrayHelper::getValue(Yii::$app->get('config')->getGoal($goal), $subname);
41
    }
42
}
43