Completed
Push — develop ( 3c1b70...55bb1b )
by Tom
03:03
created

EnvHelper::exportVariable()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.6186
c 0
b 0
f 0
cc 7
nc 7
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Config\Env;
4
5
/**
6
 * Class EnvHelper
7
 * @package N98\Magento\Command\Config\Env
8
 */
9
class EnvHelper
10
{
11
    /**
12
     * @param $var
13
     * @param string $indent
14
     * @return string|null
15
     */
16
    public static function exportVariable($var, $indent='')
17
    {
18
        switch (gettype($var)) {
19
            case 'string':
20
                return '"' . addcslashes($var, "\\\$\"\r\n\t\v\f") . '"';
21
22
            case 'array':
23
                $indexed = array_keys($var) === range(0, count($var) - 1);
24
                $r = [];
25
                foreach ($var as $key => $value) {
26
                    $r[] = $indent . '    '
27
                        . ($indexed ? '' : self::exportVariable($key) . ' => ')
28
                        . self::exportVariable($value, $indent . '    ');
29
                }
30
                return "[\n" . implode(",\n", $r) . "\n" . $indent . "]";
31
32
            case 'boolean':
33
                return $var ? 'TRUE' : 'FALSE';
34
35
            default:
36
                return var_export($var, true);
37
        }
38
    }
39
}
40