1 | <?php |
||
44 | final class RFC4180Parser implements IteratorAggregate |
||
45 | { |
||
46 | /** |
||
47 | * @internal |
||
48 | */ |
||
49 | const FIELD_BREAKS = [false, '', "\r\n", "\n", "\r"]; |
||
50 | |||
51 | /** |
||
52 | * @var SplFileObject|Stream |
||
53 | */ |
||
54 | private $document; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | private $delimiter; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $enclosure; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | private $trim_mask; |
||
70 | |||
71 | /** |
||
72 | * New instance. |
||
73 | * |
||
74 | * @param SplFileObject|Stream $document |
||
75 | */ |
||
76 | 12 | public function __construct($document, string $delimiter = ',', string $enclosure = '"') |
|
99 | |||
100 | /** |
||
101 | * @inheritdoc |
||
102 | * |
||
103 | * Converts the stream into a CSV record iterator by extracting records one by one |
||
104 | * |
||
105 | * The returned record array is similar to the returned value of fgetcsv |
||
106 | * |
||
107 | * - If the line is empty the record will be an array with a single value equals to null |
||
108 | * - Otherwise the array contains strings. |
||
109 | */ |
||
110 | 30 | public function getIterator() |
|
128 | |||
129 | /** |
||
130 | * Extract field without enclosure as per RFC4180. |
||
131 | * |
||
132 | * - Leading and trailing whitespaces must be removed. |
||
133 | * - trailing line-breaks must be removed. |
||
134 | * |
||
135 | * @param bool|string $line |
||
136 | * |
||
137 | * @return null|string |
||
138 | */ |
||
139 | 30 | private function extractFieldContent(&$line) |
|
154 | |||
155 | /** |
||
156 | * Extract field with enclosure as per RFC4180. |
||
157 | * |
||
158 | * - Field content can spread on multiple document lines. |
||
159 | * - Content inside enclosure must be preserved. |
||
160 | * - Double enclosure sequence must be replaced by single enclosure character. |
||
161 | * - Trailing line break must be removed if they are not part of the field content. |
||
162 | * - Invalid field do not throw as per fgetcsv behavior. |
||
163 | * |
||
164 | * @param bool|string $line |
||
165 | */ |
||
166 | 24 | private function extractEnclosedFieldContent(&$line): string |
|
201 | } |
||
202 |