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

YamlHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
ccs 0
cts 2
cp 0
cc 1
eloc 3
nc 1
nop 2
crap 2
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 hidev\components\Interpolator;
14
use Symfony\Component\Yaml\Yaml;
15
use yii\helpers\ArrayHelper;
16
17
/**
18
 * Handler for YAML files.
19
 */
20
class YamlHandler extends TypeHandler
21
{
22
    protected $interpolator;
23
24
    public function __construct(Interpolator $interpolator, $options = [])
25
    {
26
        parent::__construct($options);
27
        $this->interpolator = $interpolator;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 2
    public function renderType($data)
34
    {
35 2
        /// XXX TODO fix getItems crutch
36
        return Yaml::dump(ArrayHelper::toArray(method_exists($data, 'getItems') ? $data->getItems() : $data), 4);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function parse($yaml)
43
    {
44
        $data = (array) Yaml::parse($yaml);
45
        $this->interpolator->interpolate($data);
46
47
        return $data;
48
    }
49
}
50