1 | <?php |
||
45 | final class EmptyEscapeParser |
||
46 | { |
||
47 | /** |
||
48 | * @internal |
||
49 | */ |
||
50 | const FIELD_BREAKS = [false, '', "\r\n", "\n", "\r"]; |
||
51 | |||
52 | /** |
||
53 | * @var SplFileObject|Stream |
||
54 | */ |
||
55 | private static $document; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private static $delimiter; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private static $enclosure; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | private static $trim_mask; |
||
71 | |||
72 | /** |
||
73 | * @var string|bool |
||
74 | */ |
||
75 | private static $line; |
||
76 | |||
77 | /** |
||
78 | * Converts the document into a CSV record iterator. |
||
79 | * |
||
80 | * In PH7.4+ you'll be able to do |
||
81 | * |
||
82 | * <code> |
||
83 | * $file = new SplFileObject('/path/to/file.csv', 'r'); |
||
84 | * $file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
||
85 | * $file->setCsvControl($delimiter, $enclosure, ''); |
||
86 | * foreach ($file as $record) { |
||
87 | * //$record escape mechanism is blocked by the empty string |
||
88 | * } |
||
89 | * </code> |
||
90 | * |
||
91 | * In PHP7.3- you can do |
||
92 | * |
||
93 | * <code> |
||
94 | * $file = new SplFileObject('/path/to/file.csv', 'r'); |
||
95 | * $it = EmptyEscapeParser::parse($file); //parsing will be done while ignoring the escape character value. |
||
96 | * foreach ($it as $record) { |
||
97 | * //fgetcsv is not directly use hence the escape char is not taken into account |
||
98 | * } |
||
99 | * </code> |
||
100 | * |
||
101 | * Each record array contains strings elements. |
||
102 | * |
||
103 | * @param SplFileObject|Stream $document |
||
104 | * |
||
105 | * @return Generator|array[] |
||
106 | */ |
||
107 | 42 | public static function parse($document): Generator |
|
121 | |||
122 | /** |
||
123 | * Filters the submitted document. |
||
124 | * |
||
125 | * @param SplFileObject|Stream $document |
||
126 | * |
||
127 | * @return SplFileObject|Stream |
||
128 | */ |
||
129 | 6 | private static function filterDocument($document) |
|
142 | |||
143 | /** |
||
144 | * Extracts a record form the CSV document. |
||
145 | */ |
||
146 | 39 | private static function extractRecord(): array |
|
163 | |||
164 | /** |
||
165 | * Extracts the content from a field without enclosure. |
||
166 | * |
||
167 | * - Field content can not spread on multiple document lines. |
||
168 | * - Content must be preserved. |
||
169 | * - Trailing line-breaks must be removed. |
||
170 | * |
||
171 | * @return string|null |
||
172 | */ |
||
173 | 39 | private static function extractFieldContent() |
|
188 | |||
189 | /** |
||
190 | * Extracts the content from a field with enclosure. |
||
191 | * |
||
192 | * - Field content can spread on multiple document lines. |
||
193 | * - Content between consecutive enclosure characters must be preserved. |
||
194 | * - Double enclosure sequence must be replaced by single enclosure character. |
||
195 | * - Trailing line break must be removed if they are not part of the field content. |
||
196 | * - Invalid field content is treated as per fgetcsv behavior. |
||
197 | * |
||
198 | * @return string|null |
||
199 | */ |
||
200 | 30 | private static function extractEnclosedFieldContent() |
|
255 | } |
||
256 |