1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gielfeldt\Iterators; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Provide fielded rows of an array from a csv file. |
7
|
|
|
* |
8
|
|
|
* Input: |
9
|
|
|
* [ |
10
|
|
|
* ['column1 header', 'column2 header', 'column3 header'], |
11
|
|
|
* ['value 1', 'value 2', 'value 3'], |
12
|
|
|
* ['value 4', 'value 5', 'value 6'], |
13
|
|
|
* ] |
14
|
|
|
* |
15
|
|
|
* Output: |
16
|
|
|
* [ |
17
|
|
|
* ['column1 header' => 'value 1', 'column2 header' => 'value 2', 'column3 header' => 'value 3'], |
18
|
|
|
* ['column1 header' => 'value 4', 'column2 header' => 'value 5', 'column3 header' => 'value 6'], |
19
|
|
|
* ] |
20
|
|
|
*/ |
21
|
|
|
class CsvFileObject extends \SplFileObject implements \JsonSerializable, \Countable |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The fields of a row. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $fields; |
29
|
|
|
|
30
|
|
|
public $rows; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* SplFileObject constructor. |
34
|
|
|
* |
35
|
|
|
* Setup SplFileObject in CSV read-mode. |
36
|
|
|
*/ |
37
|
11 |
|
public function __construct($filename, $open_mode = "r", $use_include_path = false, $context = null) |
38
|
|
|
{ |
39
|
11 |
|
parent::__construct($filename, $open_mode, $use_include_path, $context); |
40
|
11 |
|
$this->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE); |
41
|
11 |
|
$this->processHeader(); |
42
|
11 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get determined fields for this csv file. |
46
|
|
|
*/ |
47
|
2 |
|
public function getFields() |
48
|
|
|
{ |
49
|
2 |
|
return $this->fields; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Fetch fields from the current row. |
54
|
|
|
* |
55
|
|
|
* This method should only be called when file is rewound. |
56
|
|
|
*/ |
57
|
11 |
|
protected function processHeader() |
58
|
|
|
{ |
59
|
11 |
|
$this->fields = array_values(parent::current()); |
|
|
|
|
60
|
11 |
|
parent::next(); |
|
|
|
|
61
|
11 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Rewind file and fetch fields from first row. |
65
|
|
|
*/ |
66
|
8 |
|
public function rewind() |
67
|
|
|
{ |
68
|
8 |
|
parent::rewind(); |
69
|
8 |
|
$this->processHeader(); |
70
|
8 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Map row into fields. |
74
|
|
|
*/ |
75
|
7 |
|
public function current() |
76
|
|
|
{ |
77
|
7 |
|
$row = parent::current(); |
78
|
7 |
|
$fieldedRow = []; |
79
|
7 |
|
foreach ($this->fields as $idx => $field) { |
80
|
7 |
|
$fieldedRow[$field] = $row[$idx] ?? null; |
81
|
|
|
} |
82
|
7 |
|
return $fieldedRow; |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
public function valid() |
86
|
|
|
{ |
87
|
8 |
|
$valid = parent::valid(); |
88
|
8 |
|
if (!$valid && !isset($this->rows) && !is_null($this->key())) { |
89
|
8 |
|
$this->rows = $this->key(); |
90
|
|
|
} |
91
|
8 |
|
return $valid; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Subtract 1 row from index, so that we don't include the header in our count. |
96
|
|
|
*/ |
97
|
9 |
|
public function key() |
98
|
|
|
{ |
99
|
9 |
|
return parent::key() - 1; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Add 1 to skip the header when seeking. |
104
|
|
|
*/ |
105
|
4 |
|
public function seek($position) |
106
|
|
|
{ |
107
|
4 |
|
if ($position < 0) { |
108
|
1 |
|
throw new \LogicException("Can't seek file " . $this->getFilename() . " to negative line $position"); |
109
|
|
|
} |
110
|
4 |
|
return parent::seek($position + 1); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Implements jsonSerialize. |
115
|
|
|
*/ |
116
|
3 |
|
public function jsonSerialize() |
117
|
|
|
{ |
118
|
3 |
|
return iterator_to_array($this); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Implements Countable. |
123
|
|
|
*/ |
124
|
3 |
|
public function count() |
125
|
|
|
{ |
126
|
3 |
|
if (!isset($this->rows)) { |
127
|
3 |
|
$this->seek($this->getSize()); |
128
|
3 |
|
$this->rows = $this->key(); |
129
|
|
|
} |
130
|
3 |
|
return $this->rows; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.