|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Csv-Machine package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dan McAdams <[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
|
|
|
|
|
12
|
|
|
namespace RoadBunch\Csv; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use Psr\Log\NullLogger; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Csv |
|
19
|
|
|
* |
|
20
|
|
|
* @author Dan McAdams |
|
21
|
|
|
* @package RoadBunch\Csv |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class Csv implements CsvInterface |
|
24
|
|
|
{ |
|
25
|
|
|
protected $delimiter = Delimiter::DELIMITER_COMMA; |
|
26
|
|
|
protected $enclosure = Enclosure::ENCLOSURE_DOUBLE_QUOTE; |
|
27
|
|
|
protected $newline = Newline::NEWLINE_LF; |
|
28
|
|
|
protected $escape = Escape::ESCAPE_CHAR; |
|
29
|
|
|
|
|
30
|
|
|
/** @var LoggerInterface */ |
|
31
|
|
|
protected $logger; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Csv constructor. |
|
35
|
|
|
* @param LoggerInterface|null $logger |
|
36
|
|
|
*/ |
|
37
|
13 |
|
public function __construct(LoggerInterface $logger = null) |
|
38
|
|
|
{ |
|
39
|
13 |
|
$this->logger = null !== $logger ? $logger : new NullLogger(); |
|
40
|
13 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $delimiter |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function setDelimiter(string $delimiter) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->delimiter = $delimiter; |
|
48
|
1 |
|
$this->logger->info('Delimiter character set ' . json_encode($delimiter)); |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $enclosure |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function setEnclosure(string $enclosure) |
|
55
|
|
|
{ |
|
56
|
1 |
|
$this->enclosure = $enclosure; |
|
57
|
1 |
|
$this->logger->info('Enclosure character set ' . json_encode($enclosure)); |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $newline |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function setNewline(string $newline) |
|
64
|
|
|
{ |
|
65
|
2 |
|
$this->newline = $newline; |
|
66
|
2 |
|
$this->logger->info('Newline character set ' . json_encode($newline)); |
|
67
|
2 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $escape |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function setEscape(string $escape) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$this->escape = $escape; |
|
75
|
1 |
|
$this->logger->info('Escape character set ' . json_encode($escape)); |
|
76
|
1 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|