FileParser::pack()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 18
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 24
ccs 19
cts 19
cp 1
crap 2
rs 9.6666
1
<?php
2
3
namespace kalanis\kw_menu\MetaSource;
4
5
6
use kalanis\kw_menu\Interfaces;
7
use kalanis\kw_menu\Menu\Entry;
8
use kalanis\kw_menu\Menu\Menu;
9
use kalanis\kw_menu\Traits\TEscape;
10
11
12
/**
13
 * Class FileParser
14
 * @package kalanis\kw_menu\MetaSource
15
 * Process metadata as file on volume
16
 */
17
class FileParser implements Interfaces\IMetaFileParser
18
{
19
    use TEscape;
20
21
    protected Menu $menu;
22
    protected Entry $entry;
23
24 33
    public function __construct()
25
    {
26 33
        $this->menu = new Menu();
27 33
        $this->entry = new Entry();
28 33
    }
29
30
    /**
31
     * @param string $content
32
     * @return Menu
33
     */
34 12
    public function unpack(string $content): Menu
35
    {
36 12
        $menu = clone $this->menu;
37 12
        if (!empty($content) && (false !== mb_strpos($content, "\r\n"))) {
38 11
            $lines = explode("\r\n", $content);
39 11
            $this->loadHeader($menu, reset($lines));
40 11
            $this->loadItems($menu, array_slice($lines, 1));
41
        }
42 12
        return $menu;
43
    }
44
45 11
    protected function loadHeader(Menu $menu, string $line): void
46
    {
47 11
        if (false === mb_strpos($line, Interfaces\IMenu::SEPARATOR)) {
48 1
            return;
49
        }
50 10
        $headData = explode(Interfaces\IMenu::SEPARATOR, $line);
51 10
        $menu->setData(
52 10
            $this->restoreNl(strval($headData[0])),
53 10
            $this->restoreNl(strval($headData[2])),
54 10
            $this->restoreNl(strval($headData[3])),
55 10
            intval($headData[1])
56
        );
57 10
    }
58
59
    /**
60
     * @param Menu $menu
61
     * @param string[] $lines
62
     */
63 11
    protected function loadItems(Menu $menu, array $lines): void
64
    {
65 11
        foreach ($lines as $line) {
66 11
            if (empty($line)) {
67 11
                continue;
68
            }
69 10
            if (in_array($line[0], ['#', ';'])) {
70 10
                continue;
71
            }
72 10
            $data = explode(Interfaces\IMenu::SEPARATOR, $line);
73 10
            if (2 > count($data)) {
74 10
                continue;
75
            }
76 10
            $item = clone $this->entry;
77 10
            $item->setData(
78 10
                $this->restoreNl(strval($data[0])),
79 10
                $this->restoreNl(strval($data[2])),
80 10
                $this->restoreNl(strval($data[3])),
81 10
                intval($data[1]),
82 10
                boolval(intval($data[4]))
83
            );
84 10
            $menu->addItem($item);
85
        }
86 11
    }
87
88 10
    public function pack(Menu $menu): string
89
    {
90 10
        $content = [];
91 10
        $content[] = implode(Interfaces\IMenu::SEPARATOR, [ // header
92 10
            $this->escapeNl($menu->getFile()),
93 10
            $menu->getDisplayCount(),
94 10
            $this->escapeNl($menu->getName()),
95 10
            $this->escapeNl($menu->getTitle()),
96 10
            '',
97
        ]);
98 10
        $content[] = '#';
99 10
        foreach ($menu->getEntries() as $item) { // save all! Limit only on render
100 6
            $content[] = implode(Interfaces\IMenu::SEPARATOR, [
101 6
                $this->escapeNl($item->getId()),
102 6
                strval($item->getPosition()),
103 6
                $this->escapeNl($item->getName()),
104 6
                $this->escapeNl($item->getDesc()),
105 6
                strval(intval($item->canGoSub())),
106 6
                '',
107
            ]);
108
        }
109 10
        $content[] = '';
110
111 10
        return implode("\r\n", $content);
112
    }
113
}
114