1 | <?php |
||
28 | abstract class AbstractCsv |
||
29 | { |
||
30 | use ControlsTrait; |
||
31 | use StreamTrait; |
||
32 | |||
33 | /** |
||
34 | * UTF-8 BOM sequence |
||
35 | */ |
||
36 | const BOM_UTF8 = "\xEF\xBB\xBF"; |
||
37 | |||
38 | /** |
||
39 | * UTF-16 BE BOM sequence |
||
40 | */ |
||
41 | const BOM_UTF16_BE = "\xFE\xFF"; |
||
42 | |||
43 | /** |
||
44 | * UTF-16 LE BOM sequence |
||
45 | */ |
||
46 | const BOM_UTF16_LE = "\xFF\xFE"; |
||
47 | |||
48 | /** |
||
49 | * UTF-32 BE BOM sequence |
||
50 | */ |
||
51 | const BOM_UTF32_BE = "\x00\x00\xFE\xFF"; |
||
52 | |||
53 | /** |
||
54 | * UTF-32 LE BOM sequence |
||
55 | */ |
||
56 | const BOM_UTF32_LE = "\xFF\xFE\x00\x00"; |
||
57 | |||
58 | /** |
||
59 | * The file open mode flag |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $open_mode; |
||
64 | |||
65 | /** |
||
66 | * Creates a new instance |
||
67 | * |
||
68 | * The file path can be: |
||
69 | * |
||
70 | * - a string |
||
71 | * - a SplFileObject |
||
72 | * - a StreamIterator |
||
73 | * |
||
74 | * @param mixed $path The file path |
||
75 | * @param string $open_mode The file open mode flag |
||
76 | */ |
||
77 | protected function __construct($path, string $open_mode = 'r+') |
||
83 | |||
84 | /** |
||
85 | * The destructor |
||
86 | */ |
||
87 | public function __destruct() |
||
91 | |||
92 | /** |
||
93 | * Return a new {@link AbstractCsv} from a SplFileObject |
||
94 | * |
||
95 | * @param SplFileObject $file |
||
96 | * |
||
97 | * @return static |
||
98 | */ |
||
99 | public static function createFromFileObject(SplFileObject $file): self |
||
111 | |||
112 | /** |
||
113 | * Return a new {@link AbstractCsv} from a PHP resource stream |
||
114 | * |
||
115 | 240 | * @param resource $stream |
|
116 | * |
||
117 | 240 | * @return static |
|
118 | 240 | */ |
|
119 | public static function createFromStream($stream): self |
||
123 | |||
124 | /** |
||
125 | * Return a new {@link AbstractCsv} from a string |
||
126 | * |
||
127 | 312 | * The string must be an object that implements the `__toString` method, |
|
128 | * or a string |
||
129 | 312 | * |
|
130 | * @param string $str the string |
||
131 | * |
||
132 | * @return static |
||
133 | */ |
||
134 | public static function createFromString(string $str): self |
||
141 | |||
142 | 27 | /** |
|
143 | * Return a new {@link AbstractCsv} from a file path |
||
144 | 27 | * |
|
145 | 27 | * @param string $path file path |
|
146 | * @param string $open_mode the file open mode flag |
||
147 | 27 | * |
|
148 | * @return static |
||
149 | */ |
||
150 | public static function createFromPath(string $path, string $open_mode = 'r+'): self |
||
154 | |||
155 | /** |
||
156 | * Return a new {@link AbstractCsv} instance from another {@link AbstractCsv} object |
||
157 | * |
||
158 | * @param string $class the class to be instantiated |
||
159 | 90 | * @param string $open_mode the file open mode flag |
|
160 | * |
||
161 | 90 | * @return static |
|
162 | 87 | */ |
|
163 | protected function newInstance(string $class, string $open_mode): self |
||
175 | |||
176 | /** |
||
177 | 54 | * Return a new {@link Writer} instance from a {@link AbstractCsv} object |
|
178 | * |
||
179 | 54 | * @param string $open_mode the file open mode flag |
|
180 | 3 | * |
|
181 | * @return Writer |
||
182 | */ |
||
183 | 51 | public function newWriter(string $open_mode = 'r+'): self |
|
187 | 51 | ||
188 | /** |
||
189 | * Return a new {@link Reader} instance from a {@link AbstractCsv} object |
||
190 | * |
||
191 | * @param string $open_mode the file open mode flag |
||
192 | * |
||
193 | * @return Reader |
||
194 | */ |
||
195 | public function newReader(string $open_mode = 'r+'): self |
||
199 | |||
200 | 6 | /** |
|
201 | 6 | * Set the Inner Iterator |
|
202 | 6 | * |
|
203 | 6 | * @return StreamIterator|SplFileObject |
|
204 | 6 | */ |
|
205 | 6 | protected function getCsvDocument() |
|
213 | |||
214 | /** |
||
215 | * Outputs all data on the CSV file |
||
216 | * |
||
217 | * @param string $filename CSV downloaded name if present adds extra headers |
||
218 | * |
||
219 | 3 | * @return int Returns the number of characters read from the handle |
|
220 | * and passed through to the output. |
||
221 | 3 | */ |
|
222 | public function output(string $filename = null): int |
||
233 | 3 | ||
234 | /** |
||
235 | * Outputs all data on the csv file to disk |
||
236 | * |
||
237 | * @param string $filename CSV File to be saved on disk |
||
238 | * |
||
239 | * @return int Returns the file |
||
240 | */ |
||
241 | 267 | public function outputFile(string $filename): int |
|
250 | 267 | ||
251 | /** |
||
252 | * Save csv to file in the disk |
||
253 | * |
||
254 | * @param string $filename CSV File to be saved on disk |
||
255 | * |
||
256 | * @return int Return the size of file |
||
257 | */ |
||
258 | protected function saveTofile(string $filename): int |
||
286 | |||
287 | |||
288 | /** |
||
289 | * Outputs all data from the CSV |
||
290 | * |
||
291 | * @return int Returns the number of characters read from the handle |
||
292 | * and passed through to the output. |
||
293 | */ |
||
294 | protected function fpassthru(): int |
||
311 | |||
312 | /** |
||
313 | * Retrieves the CSV content |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | public function __toString(): string |
||
324 | } |
||
325 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: