Passed
Push — master ( 6139d1...678a4c )
by Petr
13:22 queued 10:27
created

Ini   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 61
ccs 23
cts 28
cp 0.8214
rs 10
wmc 20

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pack() 0 7 2
A unpack() 0 11 3
C write_ini_string() 0 28 15
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Shared\FormatFiles;
4
5
6
use kalanis\kw_mapper\Interfaces\IFileFormat;
7
use kalanis\kw_mapper\MapperException;
8
9
10
/**
11
 * Class Ini
12
 * @package kalanis\kw_mapper\Storage\Shared\FormatFiles
13
 * Formats/unpack content into/from table created by separated elements in ini/inf file
14
 */
15
class Ini implements IFileFormat
16
{
17
    use TNl;
18
19 1
    public function unpack(string $content): array
20
    {
21 1
        $lines = @parse_ini_string($content, true);
22 1
        if (false === $lines) {
23 1
            throw new MapperException('Cannot parse INI input');
24
        }
25 1
        $records = [];
26 1
        foreach ($lines as $key => &$line) {
27 1
            $records[$key] = array_map([$this, 'unescapeNl'], $line);
28
        }
29 1
        return $records;
30
    }
31
32 1
    public function pack(array $records): string
33
    {
34 1
        $lines = [];
35 1
        foreach ($records as $key => &$record) {
36 1
            $lines[$key] = array_map([$this, 'escapeNl'], (array) $record);
37
        }
38 1
        return $this->write_ini_string($lines);
39
    }
40
41
    /**
42
     * Write an ini configuration file
43
     * @param array<string|int, string|int|float|array<string|int, string|int|array<string|int, string|int>>> $array
44
     * @return string
45
     * @link https://stackoverflow.com/questions/5695145/how-to-read-and-write-to-an-ini-file-with-php
46
     * @codeCoverageIgnore I have zero morale to check this one
47
     */
48 1
    protected function write_ini_string(array $array): string
49
    {
50
        // process array
51 1
        $data = array();
52 1
        foreach ($array as $key => $val) {
53 1
            if (is_array($val)) {
54 1
                $data[] = "[$key]";
55 1
                foreach ($val as $skey => $sval) {
56 1
                    if (is_array($sval)) {
57
                        foreach ($sval as $_skey => $_sval) {
58
                            if (is_numeric($_skey)) {
59
                                $data[] = $skey.'[] = '.(is_numeric($_sval) ? $_sval : (ctype_upper($_sval) ? $_sval : '"'.$_sval.'"'));
60
                            } else {
61
                                $data[] = $skey.'['.$_skey.'] = '.(is_numeric($_sval) ? $_sval : (ctype_upper($_sval) ? $_sval : '"'.$_sval.'"'));
62
                            }
63
                        }
64
                    } else {
65 1
                        $data[] = $skey.' = '.(is_numeric($sval) ? $sval : (ctype_upper($sval) ? $sval : '"'.$sval.'"'));
66
                    }
67
                }
68
            } else {
69
                $data[] = $key.' = '.(is_numeric($val) ? $val : (ctype_upper($val) ? $val : '"'.$val.'"'));
70
            }
71
            // empty line
72 1
            $data[] = null;
73
        }
74
75 1
        return implode(PHP_EOL, $data) . PHP_EOL;
76
    }
77
}
78