Completed
Pull Request — master (#3)
by Harry
07:29
created

CsvFormatter::__construct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 30
Code Lines 20

Duplication

Lines 7
Ratio 23.33 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 30
rs 8.439
cc 5
eloc 20
nc 4
nop 1
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Format\Formatter;
15
16
use Graze\DataFile\Format\CsvFormatInterface;
17
use Graze\DataFile\Format\Processor\BoolProcessor;
18
use Graze\DataFile\Format\Processor\DateTimeProcessor;
19
use Graze\DataFile\Format\Processor\ObjectToStringProcessor;
20
use Graze\DataFile\Format\Processor\RowProcessor;
21
22
class CsvFormatter implements FormatterInterface
23
{
24
    use RowProcessor;
25
    use InvokeFormatter;
26
27
    /**
28
     * @var CsvFormatInterface
29
     */
30
    private $csvFormat;
31
32
    /**
33
     * @var string[]
34
     */
35
    private $escapeChars;
36
37
    /**
38
     * @var string[]
39
     */
40
    private $replaceChars;
41
42
    /**
43
     * @param CsvFormatInterface $csvFormat
44
     */
45
    public function __construct(CsvFormatInterface $csvFormat)
46
    {
47
        $this->csvFormat = $csvFormat;
48
49
        $this->escapeChars = [
50
            $this->csvFormat->getEscapeCharacter(), // escape escape first so that it doesn't re-escape later on
51
            $this->csvFormat->getDelimiter(),
52
            "\n",
53
            "\r",
54
            "\t",
55
        ];
56 View Code Duplication
        if ($this->csvFormat->hasQuotes() && !$this->csvFormat->isDoubleQuote()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
            $this->escapeChars[] = $this->csvFormat->getQuoteCharacter();
58
        }
59
60
        $this->escapeChars = array_unique($this->escapeChars);
61
62
        $this->replaceChars = array_map(function ($char) {
63
            return $this->csvFormat->getEscapeCharacter() . $char;
64
        }, $this->escapeChars);
65
66 View Code Duplication
        if ($this->csvFormat->hasQuotes() && $this->csvFormat->isDoubleQuote()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
            $this->escapeChars[] = '"';
68
            $this->replaceChars[] = '""';
69
        }
70
71
        $this->addProcessor(new DateTimeProcessor());
72
        $this->addProcessor(new BoolProcessor());
73
        $this->addProcessor(new ObjectToStringProcessor());
74
    }
75
76
    /**
77
     * @param array $data
78
     *
79
     * @return string
80
     */
81
    public function format(array $data)
82
    {
83
        $data = $this->process($data);
84
85
        foreach ($data as &$element) {
86
            if (is_null($element)) {
87
                $element = $this->csvFormat->getNullOutput();
88
            } else {
89
                $element = $this->csvFormat->getQuoteCharacter() . $this->escape($element) . $this->csvFormat->getQuoteCharacter();
90
            }
91
        }
92
93
        return implode($this->csvFormat->getDelimiter(), $data);
94
    }
95
96
    /**
97
     * @param string $string
98
     *
99
     * @return string
100
     */
101
    protected function escape($string)
102
    {
103
        return str_replace($this->escapeChars, $this->replaceChars, $string);
104
    }
105
106
    /**
107
     * Return an initial block if required
108
     *
109
     * @return string
110
     */
111
    public function getInitialBlock()
112
    {
113
        return '';
114
    }
115
116
    /**
117
     * Get a separator between each row
118
     *
119
     * @return string
120
     */
121
    public function getRowSeparator()
122
    {
123
        return $this->csvFormat->getLineTerminator();
124
    }
125
126
    /**
127
     * Return a closing block if required
128
     *
129
     * @return string
130
     */
131
    public function getClosingBlock()
132
    {
133
        return '';
134
    }
135
}
136