1 | <?php |
||
40 | final class EmptyEscapeParser |
||
41 | { |
||
42 | /** |
||
43 | * @internal |
||
44 | */ |
||
45 | const FIELD_BREAKS = [false, '', "\r\n", "\n", "\r"]; |
||
46 | |||
47 | /** |
||
48 | * @var SplFileObject|Stream |
||
49 | */ |
||
50 | private static $document; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private static $delimiter; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private static $enclosure; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private static $trim_mask; |
||
66 | |||
67 | /** |
||
68 | * @var string|false |
||
69 | */ |
||
70 | private static $line; |
||
71 | |||
72 | /** |
||
73 | * @codeCoverageIgnore |
||
74 | */ |
||
75 | private function __construct() |
||
78 | |||
79 | /** |
||
80 | * Converts the document into a CSV record iterator. |
||
81 | * |
||
82 | * In PHP7.4+ you'll be able to do |
||
83 | * |
||
84 | * <code> |
||
85 | * $file = new SplFileObject('/path/to/file.csv', 'r'); |
||
86 | * $file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
||
87 | * $file->setCsvControl($delimiter, $enclosure, ''); |
||
88 | * foreach ($file as $record) { |
||
89 | * //$record escape mechanism is blocked by the empty string |
||
90 | * } |
||
91 | * </code> |
||
92 | * |
||
93 | * In PHP7.3- you can do |
||
94 | * |
||
95 | * <code> |
||
96 | * $file = new SplFileObject('/path/to/file.csv', 'r'); |
||
97 | * $it = EmptyEscapeParser::parse($file); //parsing will be done while ignoring the escape character value. |
||
98 | * foreach ($it as $record) { |
||
99 | * //fgetcsv is not directly use hence the escape char is not taken into account |
||
100 | * } |
||
101 | * </code> |
||
102 | * |
||
103 | * Each record array contains strings elements. |
||
104 | * |
||
105 | * @param SplFileObject|Stream $document |
||
106 | * |
||
107 | * @return Generator|array[] |
||
108 | */ |
||
109 | 37 | public static function parse($document): Generator |
|
123 | |||
124 | /** |
||
125 | * Filters the submitted document. |
||
126 | * |
||
127 | * @return SplFileObject|Stream |
||
128 | */ |
||
129 | 6 | private static function filterDocument(object $document) |
|
130 | { |
||
131 | 6 | if ($document instanceof Stream || $document instanceof SplFileObject) { |
|
132 | 3 | return $document; |
|
133 | } |
||
134 | |||
135 | 3 | throw new TypeError(sprintf( |
|
136 | 3 | '%s::parse expects parameter 1 to be a %s or a SplFileObject object, %s given', |
|
137 | 3 | self::class, |
|
138 | 3 | Stream::class, |
|
139 | 3 | get_class($document) |
|
140 | )); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Extracts a record form the CSV document. |
||
145 | */ |
||
146 | 34 | private static function extractRecord(): array |
|
167 | |||
168 | /** |
||
169 | * Extracts the content from a field without enclosure. |
||
170 | * |
||
171 | * - Field content can not spread on multiple document lines. |
||
172 | * - Content must be preserved. |
||
173 | * - Trailing line-breaks must be removed. |
||
174 | * |
||
175 | * @return string|null |
||
176 | */ |
||
177 | 34 | private static function extractFieldContent() |
|
198 | |||
199 | /** |
||
200 | * Extracts the content from a field with enclosure. |
||
201 | * |
||
202 | * - Field content can spread on multiple document lines. |
||
203 | * - Content between consecutive enclosure characters must be preserved. |
||
204 | * - Double enclosure sequence must be replaced by single enclosure character. |
||
205 | * - Trailing line break must be removed if they are not part of the field content. |
||
206 | * - Invalid field content is treated as per fgetcsv behavior. |
||
207 | * |
||
208 | * @return string|null |
||
209 | */ |
||
210 | 24 | private static function extractEnclosedFieldContent() |
|
259 | } |
||
260 |
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: