Passed
Push — master ( 5b634b...5acd4d )
by Бабичев
05:11
created

IniWriter::encodeValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 6
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Bavix\SDK\FileLoader;
4
5
use Bavix\Exceptions\Runtime;
6
use Bavix\Helpers\File;
7
8
class IniWriter
9
{
10
11
    /**
12
     * @param string $filename
13
     * @param array  $config
14
     * @param string $header
15
     *
16
     * @return int
17
     */
18
    public function toFile($filename, array $config, $header = null)
19
    {
20
        $ini = $this->toString($config, $header);
0 ignored issues
show
Bug introduced by
It seems like $header defined by parameter $header on line 18 can also be of type string; however, Bavix\SDK\FileLoader\IniWriter::toString() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
21
        return File::put($filename, $ini);
22
    }
23
24
    /**
25
     * @param array $config
26
     * @param null  $header
27
     *
28
     * @return string
29
     */
30
    public function toString(array $config, $header = null)
31
    {
32
        $ini = !empty($header) ? $header . PHP_EOL : '';
33
34
        uasort($config, function ($first, $second) {
35
            if (is_array($first)) {
36
                return 1;
37
            }
38
39
            if (is_array($second))
40
            {
41
                return -1;
42
            }
43
44
            return 0;
45
        });
46
47
        $names = array_keys($config);
48
49
        foreach ($names as $name)
50
        {
51
            $section = $config[$name];
52
53
            if (!is_array($section))
54
            {
55
                $ini .= $name . ' = ' . $this->encodeValue($section) . PHP_EOL;
56
                continue;
57
            }
58
59
            if (empty($section))
60
            {
61
                continue;
62
            }
63
64
            if (!empty($ini))
65
            {
66
                $ini .= PHP_EOL;
67
            }
68
69
            $ini .= "[$name]" . PHP_EOL;
70
71
            foreach ($section as $option => $value)
72
            {
73
                if (is_numeric($option))
74
                {
75
                    $option = $name;
76
                    $value  = (array)$value;
77
                }
78
79
                if (is_array($value))
80
                {
81
                    foreach ($value as $key => $currentValue)
82
                    {
83
                        $ini .= $option . '[' . $key . '] = ' . $this->encodeValue($currentValue) . PHP_EOL;
84
                    }
85
                }
86
                else
87
                {
88
                    $ini .= $option . ' = ' . $this->encodeValue($value) . PHP_EOL;
89
                }
90
            }
91
92
            $ini .= "\n";
93
        }
94
95
        return $ini;
96
    }
97
98
    /**
99
     * @param $value
100
     *
101
     * @return int|string
102
     */
103
    protected function encodeValue($value)
104
    {
105
        if (is_bool($value))
106
        {
107
            return (int)$value;
108
        }
109
110
        if (is_string($value))
111
        {
112
            return '"' . $value . '"';
113
        }
114
115
        return $value;
116
    }
117
118
}
119