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
|
12 |
|
public function __construct(CsvFormatInterface $csvFormat) |
46
|
|
|
{ |
47
|
12 |
|
$this->csvFormat = $csvFormat; |
48
|
|
|
|
49
|
12 |
|
$this->escapeChars = [ |
50
|
12 |
|
$this->csvFormat->getEscapeCharacter(), // escape escape first so that it doesn't re-escape later on |
51
|
12 |
|
$this->csvFormat->getDelimiter(), |
52
|
12 |
|
"\n", |
53
|
12 |
|
"\r", |
54
|
12 |
|
"\t", |
55
|
|
|
]; |
56
|
12 |
View Code Duplication |
if ($this->csvFormat->hasQuotes() && !$this->csvFormat->isDoubleQuote()) { |
|
|
|
|
57
|
9 |
|
$this->escapeChars[] = $this->csvFormat->getQuoteCharacter(); |
58
|
9 |
|
} |
59
|
|
|
|
60
|
12 |
|
$this->escapeChars = array_unique($this->escapeChars); |
61
|
|
|
|
62
|
12 |
|
$this->replaceChars = array_map(function ($char) { |
63
|
12 |
|
return $this->csvFormat->getEscapeCharacter() . $char; |
64
|
12 |
|
}, $this->escapeChars); |
65
|
|
|
|
66
|
12 |
View Code Duplication |
if ($this->csvFormat->hasQuotes() && $this->csvFormat->isDoubleQuote()) { |
|
|
|
|
67
|
1 |
|
$this->escapeChars[] = '"'; |
68
|
1 |
|
$this->replaceChars[] = '""'; |
69
|
1 |
|
} |
70
|
|
|
|
71
|
12 |
|
$this->addProcessor(new DateTimeProcessor()); |
72
|
12 |
|
$this->addProcessor(new BoolProcessor()); |
73
|
12 |
|
$this->addProcessor(new ObjectToStringProcessor()); |
74
|
12 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array $data |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
9 |
|
public function format(array $data) |
82
|
|
|
{ |
83
|
9 |
|
$data = $this->process($data); |
84
|
|
|
|
85
|
9 |
|
foreach ($data as &$element) { |
86
|
9 |
|
if (is_null($element)) { |
87
|
4 |
|
$element = $this->csvFormat->getNullOutput(); |
88
|
4 |
|
} else { |
89
|
9 |
|
$element = $this->csvFormat->getQuoteCharacter() . $this->escape($element) . $this->csvFormat->getQuoteCharacter(); |
90
|
|
|
} |
91
|
9 |
|
} |
92
|
|
|
|
93
|
9 |
|
return implode($this->csvFormat->getDelimiter(), $data); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $string |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
9 |
|
protected function escape($string) |
102
|
|
|
{ |
103
|
9 |
|
return str_replace($this->escapeChars, $this->replaceChars, $string); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return an initial block if required |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
4 |
|
public function getInitialBlock() |
112
|
|
|
{ |
113
|
4 |
|
return ''; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get a separator between each row |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
4 |
|
public function getRowSeparator() |
122
|
|
|
{ |
123
|
4 |
|
return $this->csvFormat->getLineTerminator(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return a closing block if required |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
4 |
|
public function getClosingBlock() |
132
|
|
|
{ |
133
|
4 |
|
return ''; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.