1 | <?php |
||
37 | final class EmptyEscapeParser |
||
38 | { |
||
39 | /** |
||
40 | * @internal |
||
41 | */ |
||
42 | const FIELD_BREAKS = [false, '', "\r\n", "\n", "\r"]; |
||
43 | |||
44 | /** |
||
45 | * @var \SplFileObject|Stream |
||
46 | */ |
||
47 | private static $document; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private static $delimiter; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | private static $enclosure; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | private static $trim_mask; |
||
63 | |||
64 | /** |
||
65 | * @var string|false |
||
66 | */ |
||
67 | private static $line; |
||
68 | |||
69 | /** |
||
70 | * @codeCoverageIgnore |
||
71 | */ |
||
72 | private function __construct() |
||
75 | |||
76 | /** |
||
77 | * Converts the document into a CSV record iterator. |
||
78 | * |
||
79 | * In PHP7.4+ you'll be able to do |
||
80 | * |
||
81 | * <code> |
||
82 | * $file = new \SplFileObject('/path/to/file.csv', 'r'); |
||
83 | * $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY); |
||
84 | * $file->setCsvControl($delimiter, $enclosure, ''); |
||
85 | * foreach ($file as $record) { |
||
86 | * //$record escape mechanism is blocked by the empty string |
||
87 | * } |
||
88 | * </code> |
||
89 | * |
||
90 | * In PHP7.3- you can do |
||
91 | * |
||
92 | * <code> |
||
93 | * $file = new \SplFileObject('/path/to/file.csv', 'r'); |
||
94 | * $it = EmptyEscapeParser::parse($file); //parsing will be done while ignoring the escape character value. |
||
95 | * foreach ($it as $record) { |
||
96 | * //fgetcsv is not directly use hence the escape char is not taken into account |
||
97 | * } |
||
98 | * </code> |
||
99 | * |
||
100 | * Each record array contains strings elements. |
||
101 | * |
||
102 | * @param \SplFileObject|Stream $document |
||
103 | * |
||
104 | * @return \Generator|array[] |
||
105 | */ |
||
106 | 45 | public static function parse($document): \Generator |
|
120 | |||
121 | /** |
||
122 | * Filters the submitted document. |
||
123 | * |
||
124 | * @return \SplFileObject|Stream |
||
125 | */ |
||
126 | 3 | private static function filterDocument(object $document) |
|
139 | |||
140 | /** |
||
141 | * Extracts a record form the CSV document. |
||
142 | */ |
||
143 | 42 | private static function extractRecord(): array |
|
164 | |||
165 | /** |
||
166 | * Extracts the content from a field without enclosure. |
||
167 | * |
||
168 | * - Field content can not spread on multiple document lines. |
||
169 | * - Content must be preserved. |
||
170 | * - Trailing line-breaks must be removed. |
||
171 | * |
||
172 | * @return string|null |
||
173 | */ |
||
174 | 42 | private static function extractFieldContent() |
|
195 | |||
196 | /** |
||
197 | * Extracts the content from a field with enclosure. |
||
198 | * |
||
199 | * - Field content can spread on multiple document lines. |
||
200 | * - Content between consecutive enclosure characters must be preserved. |
||
201 | * - Double enclosure sequence must be replaced by single enclosure character. |
||
202 | * - Trailing line break must be removed if they are not part of the field content. |
||
203 | * - Invalid field content is treated as per fgetcsv behavior. |
||
204 | * |
||
205 | * @return string|null |
||
206 | */ |
||
207 | 30 | private static function extractEnclosedFieldContent() |
|
256 | } |
||
257 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: