forked from Daniel-KM/Omeka-plugin-CsvImportPlus
/
File.php
128 lines (121 loc) · 3.74 KB
/
File.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* CsvImport_File class - represents a csv file
*
* @copyright Copyright 2007-2012 Roy Rosenzweig Center for History and New Media
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
* @package CsvImport
*/
class CsvImport_File implements IteratorAggregate
{
private $_filePath;
private $_columnNames = array();
private $_columnExamples = array();
private $_columnDelimiter;
private $_enclosure;
private $_parseErrors = array();
private $_rowIterator;
/**
* @param string $filePath Absolute path to the file.
* @param string|null $columnDelimiter Optional Column delimiter for the CSV file.
* @param string|null $enclosure Optional Enclosure for the CSV file.
*/
public function __construct($filePath, $columnDelimiter = null, $enclosure = null)
{
$this->_filePath = $filePath;
if ($columnDelimiter) {
$this->_columnDelimiter = $columnDelimiter;
}
if ($enclosure) {
$this->_enclosure = $enclosure;
}
// The function fgetcsv() doesn't allow empty enclosure.
elseif ($enclosure === '') {
$this->_enclosure = chr(0);
}
}
/**
* Absolute path to the file.
*
* @return string
*/
public function getFilePath()
{
return $this->_filePath;
}
/**
* Get an array of headers for the column names.
*
* @return array The array of headers for the column names
*/
public function getColumnNames()
{
if (!$this->_columnNames) {
throw new LogicException("CSV file must be validated before "
. "retrieving the list of columns.");
}
return $this->_columnNames;
}
/**
* Get an array of example data for the columns.
*
* @return array Examples have the same order as the column names.
*/
public function getColumnExamples()
{
if (!$this->_columnExamples) {
throw new LogicException("CSV file must be validated before "
. "retrieving the list of column examples.");
}
return $this->_columnExamples;
}
/**
* Get an iterator for the rows in the CSV file.
*
* @return CsvImport_RowIterator
*/
public function getIterator()
{
if (!$this->_rowIterator) {
$this->_rowIterator = new CsvImport_RowIterator(
$this->getFilePath(), $this->_columnDelimiter, $this->_enclosure);
}
return $this->_rowIterator;
}
/**
* Parse metadata. Currently retrieves the column names and an "example"
* row, i.e. the first row after the header.
*
* @return boolean
*/
public function parse()
{
if ($this->_columnNames || $this->_columnExamples) {
throw new RuntimeException('Cannot be parsed twice.');
}
$rowIterator = $this->getIterator();
try {
$this->_columnNames = $rowIterator->getColumnNames();
$this->_columnExamples = $rowIterator->current();
} catch (CsvImport_DuplicateColumnException $e) {
$this->_parseErrors[] = $e->getMessage()
. ' ' . __('Please ensure that all column names are unique.');
return false;
} catch (CsvImport_MissingColumnException $e) {
$this->_parseErrors[] = $e->getMessage()
. ' ' . __('Please ensure that the CSV file is formatted correctly'
. ' and contains the expected number of columns for each row.');
return false;
}
return true;
}
/**
* Get the error string.
*
* @return string
*/
public function getErrorString()
{
return join(' ', $this->_parseErrors);
}
}