Completed
Push — export_override ( d05c98...cc40f7 )
by
unknown
04:34 queued 02:32
created

Data::toYaml()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 3
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Models;
4
5
use Exception;
6
use craft\base\Model;
7
use Symfony\Component\Yaml\Yaml;
8
9
/**
10
 * Schematic Data Model.
11
 *
12
 * Encapsulates data that has been exported via schematic.
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015-2018, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @see      http://www.nerds.company
19
 */
20
class Data extends Model
21
{
22
    /**
23
     * Parse a yaml file.
24
     *
25
     * @param string $yaml
26
     * @param string $overrideYaml
27
     *
28
     * @return array
29
     */
30 6
    public static function fromYaml($yaml, $overrideYaml): array
31
    {
32 6
        $data = Yaml::parse($yaml);
33 6
        if (!empty($overrideYaml)) {
34 6
            $overrideYaml = static::replaceEnvVariables($overrideYaml);
35 5
            $overrideData = Yaml::parse($overrideYaml);
36 5
            if (null != $overrideData) {
37 5
                $data = array_replace_recursive($data, $overrideData);
38
            }
39
        }
40
41 5
        return $data;
42
    }
43
44
    /**
45
     * Replace placeholders with enviroment variables.
46
     *
47
     * Placeholders start with % and end with %. This will be replaced by the
48
     * environment variable with the name SCHEMATIC_{PLACEHOLDER}. If the
49
     * environment variable is not set an exception will be thrown.
50
     *
51
     * @param string $yaml
52
     *
53
     * @return string
54
     *
55
     * @throws Exception
56
     */
57 6
    public static function replaceEnvVariables($yaml): string
58
    {
59 6
        $matches = null;
60 6
        preg_match_all('/%\w+%/', $yaml, $matches);
61 6
        $originalValues = $matches[0];
62 6
        $replaceValues = [];
63 6
        foreach ($originalValues as $match) {
64 6
            $envVariable = strtoupper(substr($match, 1, -1));
65 6
            $envVariable = 'SCHEMATIC_'.$envVariable;
66 6
            $envValue = getenv($envVariable);
67 6
            if (!$envValue) {
68 1
                throw new Exception("Schematic environment variable not set: {$envVariable}");
69
            }
70 5
            $replaceValues[] = $envValue;
71
        }
72
73 5
        return str_replace($originalValues, $replaceValues, $yaml);
74
    }
75
76
    /**
77
     * Convert array to yaml.
78
     *
79
     * @param array  $data
80
     * @param string $overrideYaml
81
     *
82
     * @return string
83
     */
84 3
    public static function toYaml(array $data, $overrideYaml = ''): string
85
    {
86 3
        if (!empty($overrideYaml)) {
87 1
            $overrideData = Yaml::parse($overrideYaml);
88 1
            if (null != $overrideData) {
89 1
                $data = array_replace_recursive($data, $overrideData);
90
            }
91
        }
92
93 3
        return Yaml::dump($data, 12, 2);
94
    }
95
}
96