1 | <?php |
||
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 The filename to read |
||
42 | * @param CSVelte\Flavor|array|null 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 | * @return CSVelte\Reader An iterator for specified CSV file |
||
46 | * @throws CSVelte\Exception\PermissionDeniedException |
||
47 | * @throws CSVelte\Exception\FileNotFoundException |
||
48 | */ |
||
49 | 6 | public static function reader($filename, $flavor = null) |
|
55 | |||
56 | /** |
||
57 | * String Reader Factory |
||
58 | * |
||
59 | * Factory method for creating a new CSVelte\Reader object for reading |
||
60 | * from a PHP string |
||
61 | * |
||
62 | * @param string The CSV data to read |
||
63 | * @param CSVelte\Flavor|array|null An explicit flavor object that will be |
||
64 | * passed to the reader or an array of flavor attributes to override the |
||
65 | * default flavor attributes |
||
66 | * @return CSVelte\Reader An iterator for provided CSV data |
||
67 | */ |
||
68 | 1 | public static function stringReader($str, $flavor = null) |
|
69 | { |
||
70 | 1 | return new Reader($str, $flavor); |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * CSVelte\Writer Factory |
||
75 | * |
||
76 | * Factory method for creating a new CSVelte\Writer object for writing |
||
77 | * CSV data to a file. If file doesn't exist, it will be created. If it |
||
78 | * already contains data, it will be overwritten. |
||
79 | * |
||
80 | * @param string The filename to write to. |
||
81 | * @param CSVelte\Flavor|array|null An explicit flavor object that will be |
||
82 | * passed to the reader or an array of flavor attributes to override the |
||
83 | * default flavor attributes |
||
84 | * @return CSVelte\Writer A writer object for writing to given filename |
||
85 | */ |
||
86 | 2 | public static function writer($filename, $flavor = null) |
|
87 | { |
||
88 | 2 | $file = new Stream($filename, 'w+'); |
|
89 | 2 | return new Writer($file, $flavor); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Export CSV data to local file |
||
94 | * |
||
95 | * Facade method for exporting data to given filename. IF file doesn't exist |
||
96 | * it will be created. If it does exist it will be overwritten. |
||
97 | * |
||
98 | * @param string The filename to export data to |
||
99 | * @param Iterator|array Data to write to CSV file |
||
100 | * @param CSVelte\Flavor|array|null An explicit flavor object that will be |
||
101 | * passed to the reader or an array of flavor attributes to override the |
||
102 | * default flavor attributes |
||
103 | * @return int Number of rows written |
||
104 | */ |
||
105 | 2 | public static function export($filename, $data, $flavor = null) |
|
106 | { |
||
107 | 2 | $file = new Stream($filename, 'w+'); |
|
108 | 2 | $writer = new Writer($file, $flavor); |
|
109 | 2 | return $writer->writeRows($data); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * Assert that file is readable |
||
114 | * |
||
115 | * Assert that a particular file exists and is readable (user has permission |
||
116 | * to read/access it) |
||
117 | * |
||
118 | * @param string The name of the file you wish to check |
||
119 | * @throws CSVelte\Exception\IOException |
||
120 | * @internal |
||
121 | */ |
||
122 | 6 | protected static function assertFileIsReadable($filename) |
|
123 | { |
||
124 | 6 | self::assertFileExists($filename); |
|
125 | 5 | if (!is_readable($filename)) { |
|
126 | 1 | throw new IOException('Permission denied for: ' . $filename, IOException::ERR_FILE_PERMISSION_DENIED); |
|
127 | } |
||
128 | 4 | } |
|
129 | |||
130 | /** |
||
131 | * Assert that a particular file exists |
||
132 | * |
||
133 | * @param string The name of the file you wish to check |
||
134 | * @throws CSVelte\Exception\IOException |
||
135 | * @internal |
||
136 | */ |
||
137 | 6 | protected static function assertFileExists($filename) |
|
143 | } |
||
144 |