Test Setup Failed
Branch 0.x (13f0ad)
by Pavel
13:06 queued 01:31
created

TxtDecoder::decode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AppBundle\Decoder;
17
18
use Symfony\Component\Serializer\Encoder\DecoderInterface;
19
20
/**
21
 * Converts a plain text string into array of lines by specified delimiter
22
 */
23
class TxtDecoder implements DecoderInterface
24
{
25
    /**
26
     * Format supported by decoder
27
     *
28
     * @const string
29
     */
30
    private const FORMAT = 'txt';
31
32
    /**
33
     * Line delimiter
34
     *
35
     * @var string
36
     */
37
    private $lineDelimiter;
38
39
    /**
40
     * TxtDecoder constructor.
41
     *
42
     * @param string $lineDelimiter Line delimiter
43
     */
44
    public function __construct(string $lineDelimiter = "\n")
45
    {
46
        $this->lineDelimiter = $lineDelimiter;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function decode($data, $format, array $context = [])
53
    {
54
        if (empty($data)) {
55
            return [];
56
        }
57
58
        $array = explode($this->lineDelimiter, $data);
59
60
        return $array;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function supportsDecoding($format)
67
    {
68
        return self::FORMAT === $format;
69
    }
70
}
71