1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* CSVelte: Slender, elegant CSV for PHP |
5
|
|
|
* Inspired by Python's CSV module and Frictionless Data and the W3C's CSV |
6
|
|
|
* standardization efforts, CSVelte was written in an effort to take all the |
7
|
|
|
* suck out of working with CSV. |
8
|
|
|
* |
9
|
|
|
* @version {version} |
10
|
|
|
* @copyright Copyright (c) 2016 Luke Visinoni <[email protected]> |
11
|
|
|
* @author Luke Visinoni <[email protected]> |
12
|
|
|
* @license https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT) |
13
|
|
|
*/ |
14
|
|
|
namespace CSVelte; |
15
|
|
|
|
16
|
|
|
use CSVelte\Exception\IOException; |
17
|
|
|
use CSVelte\IO\Stream; |
18
|
|
|
|
19
|
|
|
use Iterator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* CSVelte Facade. |
23
|
|
|
* |
24
|
|
|
* This class consists of static factory methods for easily generating commonly |
25
|
|
|
* used objects such as readers and writers, as well as convenience methods for |
26
|
|
|
* commonly used functionality such as exporting CSV data to a file. |
27
|
|
|
* |
28
|
|
|
* @package CSVelte |
29
|
|
|
* @subpackage Factory/Adapter |
30
|
|
|
* |
31
|
|
|
* @since v0.1 |
32
|
|
|
*/ |
33
|
|
|
class CSVelte |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* CSVelte\Reader Factory. |
37
|
|
|
* |
38
|
|
|
* Factory method for creating a new CSVelte\Reader object |
39
|
|
|
* Used to create a local file CSV reader object. |
40
|
|
|
* |
41
|
|
|
* @param string $filename The filename to read |
42
|
|
|
* @param Flavor|array|null $flavor An explicit flavor object that will be |
43
|
|
|
* passed to the reader or an array of flavor attributes to override the |
44
|
|
|
* default flavor attributes |
45
|
|
|
* |
46
|
|
|
* @throws IOException |
47
|
|
|
* |
48
|
|
|
* @return \CSVelte\Reader An iterator for specified CSV file |
49
|
|
|
*/ |
50
|
6 |
|
public static function reader($filename, $flavor = null) |
51
|
|
|
{ |
52
|
6 |
|
self::assertFileIsReadable($filename); |
53
|
4 |
|
$file = Stream::open($filename); |
54
|
|
|
|
55
|
4 |
|
return new Reader($file, $flavor); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* String Reader Factory. |
60
|
|
|
* |
61
|
|
|
* Factory method for creating a new CSVelte\Reader object for reading |
62
|
|
|
* from a PHP string |
63
|
|
|
* |
64
|
|
|
* @param string $str The CSV data to read |
65
|
|
|
* @param Flavor|array|null $flavor An explicit flavor object that will be |
66
|
|
|
* passed to the reader or an array of flavor attributes to override the |
67
|
|
|
* default flavor attributes |
68
|
|
|
* |
69
|
|
|
* @return Reader An iterator for provided CSV data |
70
|
|
|
*/ |
71
|
1 |
|
public static function stringReader($str, $flavor = null) |
72
|
|
|
{ |
73
|
1 |
|
return new Reader(streamize($str), $flavor); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* CSVelte\Writer Factory. |
78
|
|
|
* |
79
|
|
|
* Factory method for creating a new CSVelte\Writer object for writing |
80
|
|
|
* CSV data to a file. If file doesn't exist, it will be created. If it |
81
|
|
|
* already contains data, it will be overwritten. |
82
|
|
|
* |
83
|
|
|
* @param string $filename The filename to write to. |
84
|
|
|
* @param Flavor|array|null $flavor An explicit flavor object that will be |
85
|
|
|
* passed to the reader or an array of flavor attributes to override the |
86
|
|
|
* default flavor attributes |
87
|
|
|
* |
88
|
|
|
* @return Writer A writer object for writing to given filename |
89
|
|
|
*/ |
90
|
2 |
|
public static function writer($filename, $flavor = null) |
91
|
|
|
{ |
92
|
2 |
|
$file = Stream::open($filename, 'w+'); |
93
|
|
|
|
94
|
2 |
|
return new Writer($file, $flavor); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Export CSV data to local file. |
99
|
|
|
* |
100
|
|
|
* Facade method for exporting data to given filename. IF file doesn't exist |
101
|
|
|
* it will be created. If it does exist it will be overwritten. |
102
|
|
|
* |
103
|
|
|
* @param string $filename The filename to export data to |
104
|
|
|
* @param Iterator|array $data Data to write to CSV file |
105
|
|
|
* @param Flavor|array|null $flavor An explicit flavor object that will be |
106
|
|
|
* passed to the reader or an array of flavor attributes to override the |
107
|
|
|
* default flavor attributes |
108
|
|
|
* |
109
|
|
|
* @return int Number of rows written |
110
|
|
|
*/ |
111
|
2 |
|
public static function export($filename, $data, $flavor = null) |
112
|
|
|
{ |
113
|
2 |
|
$file = Stream::open($filename, 'w+'); |
114
|
2 |
|
$writer = new Writer($file, $flavor); |
115
|
|
|
|
116
|
2 |
|
return $writer->writeRows($data); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Assert that file is readable. |
121
|
|
|
* |
122
|
|
|
* Assert that a particular file exists and is readable (user has permission |
123
|
|
|
* to read/access it) |
124
|
|
|
* |
125
|
|
|
* @param string $filename The name of the file you wish to check |
126
|
|
|
* |
127
|
|
|
* @throws IOException |
128
|
|
|
* |
129
|
|
|
* @internal |
130
|
|
|
*/ |
131
|
6 |
|
protected static function assertFileIsReadable($filename) |
132
|
|
|
{ |
133
|
6 |
|
self::assertFileExists($filename); |
134
|
5 |
|
if (!is_readable($filename)) { |
135
|
1 |
|
throw new IOException('Permission denied for: ' . $filename, IOException::ERR_FILE_PERMISSION_DENIED); |
136
|
|
|
} |
137
|
4 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Assert that a particular file exists. |
141
|
|
|
* |
142
|
|
|
* @param string $filename The name of the file you wish to check |
143
|
|
|
* |
144
|
|
|
* @throws IOException |
145
|
|
|
* |
146
|
|
|
* @internal |
147
|
|
|
*/ |
148
|
6 |
|
protected static function assertFileExists($filename) |
149
|
|
|
{ |
150
|
6 |
|
if (!file_exists($filename)) { |
151
|
1 |
|
throw new IOException('File does not exist: ' . $filename, IOException::ERR_FILE_NOT_FOUND); |
152
|
|
|
} |
153
|
5 |
|
} |
154
|
|
|
} |
155
|
|
|
|