1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2015-05-16 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\Csv\Reader; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class EasyCsvReaderAdapter |
11
|
|
|
* @package Net\Bazzline\Component\Csv\Reader |
12
|
|
|
*/ |
13
|
|
|
class EasyCsvReaderAdapter |
14
|
|
|
{ |
15
|
|
|
/** @var ReaderInterface */ |
16
|
|
|
private $reader; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $path |
20
|
|
|
* @param string $mode - not in use |
21
|
|
|
* @param bool $headersInFirstRow |
22
|
|
|
* @param ReaderInterface $reader - optional |
23
|
|
|
*/ |
24
|
|
|
public function __construct($path, $mode = 'r+', $headersInFirstRow = true, ReaderInterface $reader = null) |
25
|
|
|
{ |
26
|
|
|
if (is_null($reader)) { |
27
|
|
|
$factory = new ReaderFactory(); |
28
|
|
|
$this->reader = $factory->create(); |
29
|
|
|
} else { |
30
|
|
|
$this->reader = $reader; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->reader->setDelimiter(','); |
34
|
|
|
$this->reader->setEnclosure('"'); |
35
|
|
|
$this->reader->setPath($path); |
36
|
|
|
|
37
|
|
|
if ($headersInFirstRow) { |
38
|
|
|
$this->reader->enableHasHeadline(); |
39
|
|
|
} else { |
40
|
|
|
$this->reader->disableHasHeadline(); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $delimiter |
46
|
|
|
*/ |
47
|
|
|
public function setDelimiter($delimiter) |
48
|
|
|
{ |
49
|
|
|
$this->reader->setDelimiter($delimiter); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $enclosure |
54
|
|
|
*/ |
55
|
|
|
public function setEnclosure($enclosure) |
56
|
|
|
{ |
57
|
|
|
$this->reader->setEnclosure($enclosure); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array|false |
62
|
|
|
*/ |
63
|
|
|
public function getHeaders() |
64
|
|
|
{ |
65
|
|
|
$headline = $this->reader->readHeadline(); |
66
|
|
|
|
67
|
|
|
return $headline; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array|bool|string |
72
|
|
|
*/ |
73
|
|
|
public function getRow() |
74
|
|
|
{ |
75
|
|
|
$this->reader->disableAddHeadlineToOutput(); |
76
|
|
|
|
77
|
|
|
return $this->reader->readOne(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function getAll() |
84
|
|
|
{ |
85
|
|
|
$this->reader->enableAddHeadlineToOutput(); |
86
|
|
|
|
87
|
|
|
return $this->reader->readAll(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return int|null |
92
|
|
|
*/ |
93
|
|
|
public function getLineNumber() |
94
|
|
|
{ |
95
|
|
|
return $this->reader->key(); |
96
|
|
|
} |
97
|
|
|
} |