1 | <?php |
||
37 | class Writer |
||
38 | { |
||
39 | /** |
||
40 | * @var \CSVelte\Flavor |
||
41 | */ |
||
42 | protected $flavor; |
||
43 | |||
44 | /** |
||
45 | * @var \CSVelte\Contract\Writable |
||
46 | */ |
||
47 | protected $output; |
||
48 | |||
49 | /** |
||
50 | * @var \Iterator |
||
51 | */ |
||
52 | protected $headers; |
||
53 | |||
54 | /** |
||
55 | * @var int lines of data written so far (not including header) |
||
56 | */ |
||
57 | protected $written = 0; |
||
58 | |||
59 | /** |
||
60 | * Class Constructor |
||
61 | * |
||
62 | * @param \CSVelte\Contract\Writable $output An output source to write to |
||
63 | * @param \CSVelte\Flavor|array $flavor A flavor or set of formatting params |
||
64 | */ |
||
65 | 17 | public function __construct(Writable $output, $flavor = null) |
|
66 | { |
||
67 | 17 | if (!($flavor instanceof Flavor)) $flavor = new Flavor($flavor); |
|
68 | 17 | $this->flavor = $flavor; |
|
69 | 17 | $this->output = $output; |
|
70 | 17 | } |
|
71 | |||
72 | /** |
||
73 | * Get the CSV flavor (or dialect) for this writer |
||
74 | * |
||
75 | * @param void |
||
76 | * @return \CSVelte\Flavor |
||
77 | * @access public |
||
78 | */ |
||
79 | 16 | public function getFlavor() |
|
83 | |||
84 | /** |
||
85 | * Sets the header row |
||
86 | * If any data has been written to the output, it is too late to write the |
||
87 | * header row and an exception will be thrown. Later implementations will |
||
88 | * likely buffer the output so that this may be called after writeRows() |
||
89 | * |
||
90 | * @param \Iterator|array A list of header values |
||
91 | * @return boolean |
||
92 | * @throws \CSVelte\Exception\WriterException |
||
93 | */ |
||
94 | 2 | public function setHeaderRow($headers) |
|
102 | |||
103 | /** |
||
104 | * Write a single row |
||
105 | * |
||
106 | * @param \Iterator|array $row The row to write to source |
||
107 | * @return int The number or bytes written |
||
108 | */ |
||
109 | 14 | public function writeRow($row) |
|
124 | |||
125 | 3 | protected function writeHeaderRow(HeaderRow $row) |
|
132 | |||
133 | /** |
||
134 | * Write multiple rows |
||
135 | * |
||
136 | * @param \Iterable|array $rows List of \Iterable|array |
||
137 | * @return int number of lines written |
||
138 | * @access public |
||
139 | */ |
||
140 | 11 | public function writeRows($rows) |
|
155 | |||
156 | /** |
||
157 | * Prepare a row of data to be written |
||
158 | * This means taking an array of data, and converting it to a Row object |
||
159 | * |
||
160 | * @param \Iterator|array of data items |
||
161 | * @return CSVelte\Table\AbstractRow |
||
162 | * @access protected |
||
163 | */ |
||
164 | 14 | protected function prepareRow(Iterator $row) |
|
173 | |||
174 | /** |
||
175 | * Prepare a cell of data to be written (convert to Data object) |
||
176 | * |
||
177 | * @param string $data A string containing cell data |
||
178 | * @return string quoted string data |
||
179 | * @access protected |
||
180 | */ |
||
181 | 14 | protected function prepareData($data) |
|
188 | |||
189 | 14 | protected function quoteString($str) |
|
223 | |||
224 | 14 | protected function escapeString($str, $isQuoted = true) |
|
232 | } |
||
233 |