1 | <?php |
||
42 | final class EmptyEscapeParser |
||
43 | { |
||
44 | /** |
||
45 | * @internal |
||
46 | */ |
||
47 | const FIELD_BREAKS = [false, '', "\r\n", "\n", "\r"]; |
||
48 | |||
49 | /** |
||
50 | * @var SplFileObject|Stream |
||
51 | */ |
||
52 | private static $document; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | private static $delimiter; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | private static $enclosure; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | private static $trim_mask; |
||
68 | |||
69 | /** |
||
70 | * @var string|bool |
||
71 | */ |
||
72 | private static $line; |
||
73 | |||
74 | /** |
||
75 | * @codeCoverageIgnore |
||
76 | */ |
||
77 | private function __construct() |
||
80 | |||
81 | /** |
||
82 | * Converts the document into a CSV record iterator. |
||
83 | * |
||
84 | * In PHP7.4+ you'll be able to do |
||
85 | * |
||
86 | * <code> |
||
87 | * $file = new SplFileObject('/path/to/file.csv', 'r'); |
||
88 | * $file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
||
89 | * $file->setCsvControl($delimiter, $enclosure, ''); |
||
90 | * foreach ($file as $record) { |
||
91 | * //$record escape mechanism is blocked by the empty string |
||
92 | * } |
||
93 | * </code> |
||
94 | * |
||
95 | * In PHP7.3- you can do |
||
96 | * |
||
97 | * <code> |
||
98 | * $file = new SplFileObject('/path/to/file.csv', 'r'); |
||
99 | * $it = EmptyEscapeParser::parse($file); //parsing will be done while ignoring the escape character value. |
||
100 | * foreach ($it as $record) { |
||
101 | * //fgetcsv is not directly use hence the escape char is not taken into account |
||
102 | * } |
||
103 | * </code> |
||
104 | * |
||
105 | * Each record array contains strings elements. |
||
106 | * |
||
107 | 42 | * @param SplFileObject|Stream $document |
|
108 | * |
||
109 | 42 | * @return Generator|array[] |
|
110 | 39 | */ |
|
111 | 39 | public static function parse($document): Generator |
|
125 | |||
126 | /** |
||
127 | * Filters the submitted document. |
||
128 | * |
||
129 | 6 | * @param SplFileObject|Stream $document |
|
130 | * |
||
131 | 6 | * @return SplFileObject|Stream |
|
132 | 3 | */ |
|
133 | private static function filterDocument($document) |
||
146 | 39 | ||
147 | /** |
||
148 | 39 | * Extracts a record form the CSV document. |
|
149 | 39 | */ |
|
150 | 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 | 39 | * - Trailing line-breaks must be removed. |
|
174 | * |
||
175 | 39 | * @return string|null |
|
176 | 3 | */ |
|
177 | private static function extractFieldContent() |
||
192 | |||
193 | /** |
||
194 | * Extracts the content from a field with enclosure. |
||
195 | * |
||
196 | * - Field content can spread on multiple document lines. |
||
197 | * - Content between consecutive enclosure characters must be preserved. |
||
198 | * - Double enclosure sequence must be replaced by single enclosure character. |
||
199 | * - Trailing line break must be removed if they are not part of the field content. |
||
200 | 30 | * - Invalid field content is treated as per fgetcsv behavior. |
|
201 | * |
||
202 | 30 | * @return string|null |
|
203 | 30 | */ |
|
204 | private static function extractEnclosedFieldContent() |
||
251 | } |
||
252 |