Text::unpack()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 5
rs 9.6111
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\DataEncoders;
4
5
6
use kalanis\UploadPerPartes\Uploader\Data;
7
8
9
/**
10
 * Class Text
11
 * @package kalanis\UploadPerPartes\ServerData\DataModifiers
12
 * Driver file - format plaintext
13
 */
14
class Text extends AEncoder
15
{
16
    const DATA_SEPARATOR = ':';
17
    const LINE_SEPARATOR = "\r\n";
18
19 1
    public function unpack(string $content): Data
20
    {
21 1
        $lines = explode(static::LINE_SEPARATOR, $content);
22 1
        $libData = new Data();
23 1
        if (false !== $lines) {
24 1
            foreach ($lines as $line) {
25 1
                if (0 < mb_strlen($line)) {
26 1
                    $data = explode(static::DATA_SEPARATOR, $line);
27 1
                    if (false !== $data) {
28 1
                        list($key, $value, $nothing) = $data;
29 1
                        $libData->{$key} = $value;
30
                    }
31
                }
32
            }
33
        }
34 1
        return $libData->clear();
35
    }
36
37 1
    public function pack(Data $data): string
38
    {
39 1
        $dataArray = (array) $data;
40 1
        $dataLines = [];
41 1
        foreach ($dataArray as $key => $value) {
42 1
            $dataLines[] = implode(static::DATA_SEPARATOR, [$key, $value, '']);
43
        }
44 1
        return implode(static::LINE_SEPARATOR, $dataLines);
45
    }
46
}
47