Completed
Push — master ( 5f97d8...49324f )
by Andrii
11:55
created

YamlHandler::interpolateParams()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 20
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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\handlers;
12
13
use Symfony\Component\Yaml\Yaml;
14
use Yii;
15
use yii\helpers\ArrayHelper;
16
17
/**
18
 * Handler for YAML files.
19
 */
20
class YamlHandler extends TypeHandler
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function renderType($data)
26
    {
27
        /// XXX TODO fix getItems crutch
28
        return Yaml::dump(ArrayHelper::toArray(method_exists($data, 'getItems') ? $data->getItems() : $data), 4);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33 2
     */
34
    public function parse($yaml)
35 2
    {
36
        $data = (array) Yaml::parse($yaml);
37
        $this->interpolateParams($data);
38
39
        return $data;
40
    }
41
42
    public function interpolateParams(&$data)
43
    {
44
        if (is_array($data)) {
45
            foreach ($data as &$item) {
46
                $this->interpolateParams($item);
47
            }
48
        } elseif (is_string($data)) {
49
            $data = preg_replace_callback('/\$params\[\'(.*?)\'\]/', function ($matches) {
50
                return Yii::$app->params[$matches[1]];
51
            }, $data);
52
        }
53
    }
54
}
55