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

Interpolator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 8
c 2
b 0
f 2
lcom 0
cbo 2
dl 0
loc 35
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A interpolate() 0 12 4
A get() 0 12 3
A getConfig() 0 6 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