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