Total Complexity | 8 |
Total Lines | 45 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
14 | class SeparatedElements implements IFileFormat |
||
15 | { |
||
16 | use TNl; |
||
17 | |||
18 | /** @var string */ |
||
19 | protected $delimitElements = '|'; |
||
20 | /** @var string */ |
||
21 | protected $delimitLines = PHP_EOL; |
||
22 | |||
23 | 1 | public function setDelimiters(string $elements = '|', string $lines = PHP_EOL): self |
|
24 | { |
||
25 | 1 | $this->delimitElements = $elements; |
|
26 | 1 | $this->delimitLines = $lines; |
|
27 | 1 | return $this; |
|
28 | } |
||
29 | |||
30 | 5 | public function unpack(string $content): array |
|
31 | { |
||
32 | 5 | $lines = explode($this->delimitLines, $content); |
|
33 | 5 | $records = []; |
|
34 | 5 | if (false !== $lines) { |
|
35 | 5 | foreach ($lines as &$line) { |
|
36 | 5 | if (empty($line)) { |
|
37 | 5 | continue; |
|
38 | } |
||
39 | |||
40 | 5 | $items = explode($this->delimitElements, strval($line)); |
|
41 | 5 | if (false !== $items) { |
|
42 | 5 | $records[] = array_map([$this, 'unescapeNl'], $items); |
|
43 | } |
||
44 | } |
||
45 | } |
||
46 | 5 | return $records; |
|
47 | } |
||
48 | |||
49 | 6 | public function pack(array $records): string |
|
59 | } |
||
60 | } |
||
61 |