Completed
Push — master ( 49324f...8a5a61 )
by Andrii
12:44
created

Interpolator::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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