You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -21,7 +21,7 @@ You may install the Bakame Url package with Composer (recommended) or manually.
21
21
```json
22
22
{
23
23
"require": {
24
-
"bakame/csv": "2.*"
24
+
"bakame/csv": "3.*"
25
25
}
26
26
}
27
27
```
@@ -40,45 +40,196 @@ The easiest way to get started is to add `'/path/to/Bakame/Csv/src'` to your PSR
40
40
Usage
41
41
-------
42
42
43
-
The wrapper serves 2 main functions loading and saving data in CSV formats. To do that, the wrapper always returns a `SplFileObject` instance with flags set to `SplFileObject::READ_CSV` so that you can use it to further manipulate your data.
44
-
Of course, since a `SplFileObject` is returned you are free to modify the flags to satisfy your application needs.
43
+
### The Codec Class.
44
+
45
+
The `Codec` class enable loading and saving data in CSV formats. The class returns a `Reader` class to use to further manipulate your data.
//returns a SplFileObject object you can use to iterate throught your CSV data
60
+
Depending on your CSV source you may choose `Codec::loadFile` method or `Codec::loadString` to enable reading your CSV. Whatever source you chose both methods will return a `Bakame\Csv\Reader` object to help you manipulate your data.
The `Reader` facilitates manipulating CSV data that are stored in a `SplFileObject` object.
97
+
To instantiate the class you must provide at leat a `SplFileObject` object like below:
98
+
99
+
```php
100
+
101
+
use Bakame\Csv\Reader;
102
+
103
+
$file = new \SpliFileObject('/path/to/your/csv/file.csv');
104
+
$reader = new Reader($file);
105
+
$reader->setDelimeter(',');
106
+
$reader->setEnclosure('"');
107
+
$reader->setEscape('\\');
108
+
109
+
```
110
+
You can optionally set CSV delimiter, enclosure and/or escape characters.
111
+
112
+
The `Bakame\Csv\Reader` object let you access the `SplFileObject` used to instantiate it when using the method `Reader::getFile`. This method comes handy if, for instance, you want to download your data. But it also exposes several fetching methods to help you easily extract you CSV data:
113
+
114
+
#### `Reader::fetchAll`
115
+
116
+
This methods returns a sequentials array of all CSV rows.
117
+
118
+
```php
119
+
$data = $reader->fetchAll();
120
+
// will return something like this :
121
+
//
122
+
// [
123
+
// ['john', 'doe', 'john.doe@example.com'],
124
+
// ['jane', 'doe', 'jane.doe@example.com'],
125
+
// ...
126
+
// ]
127
+
//
128
+
```
129
+
This method can take an optional callable variable to further manipulate each row before being returned. This callable expected an array as its sole argument.
130
+
131
+
```php
132
+
$data = $reader->fetchAll(function ($value) {
133
+
return array_map('strtoupper', $value);
134
+
});
135
+
// will return something like this :
136
+
//
137
+
// [
138
+
// ['JOHN', 'DOE', 'JOHN.DOE@EXAMPLE.COM'],
139
+
// ['JANE', 'DOE', 'JANE.DOE@EXAMPLE.COM'],
140
+
// ...
141
+
// ]
142
+
//
143
+
```
144
+
145
+
#### `Reader::fetchAssoc`
146
+
147
+
This method returns a sequentials array of all CSV rows. the rows are associative arrays where the key are given to the method using a array.
This method can take an optional callable variable to further manipulate each row before being returned. This callable expected an array as its sole argument.
161
+
162
+
```php
163
+
$data = $reader->fetchAssoc(['firstname', 'lastname', 'email'], function ($value) {
This method can take an optional callable variable to further manipulate each value before being returned. This callable expected an array as its sole argument.
0 commit comments