| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the League.csv library |
| 4 |
* |
| 5 |
* @license http://opensource.org/licenses/MIT |
| 6 |
* @link https://github.com/thephpleague/csv/ |
| 7 |
* @version 8.2.2 |
| 8 |
* @package League.csv |
| 9 |
* |
| 10 |
* For the full copyright and license information, please view the LICENSE |
| 11 |
* file that was distributed with this source code. |
| 12 |
*/ |
| 13 |
namespace League\Csv\Modifier; |
| 14 |
|
| 15 |
use InvalidArgumentException; |
| 16 |
use Iterator; |
| 17 |
use SplFileObject; |
| 18 |
|
| 19 |
/** |
| 20 |
* A Stream Iterator |
| 21 |
* |
| 22 |
* @package League.csv |
| 23 |
* @since 8.2.0 |
| 24 |
* @internal used internally to iterate over a stream resource |
| 25 |
* |
| 26 |
*/ |
| 27 |
class StreamIterator implements Iterator |
| 28 |
{ |
| 29 |
/** |
| 30 |
* Stream pointer |
| 31 |
* |
| 32 |
* @var resource |
| 33 |
*/ |
| 34 |
protected $stream; |
| 35 |
|
| 36 |
/** |
| 37 |
* Current iterator value |
| 38 |
* |
| 39 |
* @var mixed |
| 40 |
*/ |
| 41 |
protected $current_line; |
| 42 |
|
| 43 |
/** |
| 44 |
* Current iterator key |
| 45 |
* |
| 46 |
* @var int |
| 47 |
*/ |
| 48 |
protected $current_line_number; |
| 49 |
|
| 50 |
/** |
| 51 |
* Flags for the StreamIterator |
| 52 |
* |
| 53 |
* @var int |
| 54 |
*/ |
| 55 |
protected $flags = 0; |
| 56 |
|
| 57 |
/** |
| 58 |
* the field delimiter (one character only) |
| 59 |
* |
| 60 |
* @var string |
| 61 |
*/ |
| 62 |
protected $delimiter = ','; |
| 63 |
|
| 64 |
/** |
| 65 |
* the field enclosure character (one character only) |
| 66 |
* |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
protected $enclosure = '"'; |
| 70 |
|
| 71 |
/** |
| 72 |
* the field escape character (one character only) |
| 73 |
* |
| 74 |
* @var string |
| 75 |
*/ |
| 76 |
protected $escape = '\\'; |
| 77 |
|
| 78 |
/** |
| 79 |
* New instance |
| 80 |
* |
| 81 |
* @param resource $stream stream type resource |
| 82 |
*/ |
| 83 |
public function __construct($stream) |
| 84 |
{ |
| 85 |
if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) { |
| 86 |
throw new InvalidArgumentException(sprintf( |
| 87 |
'Expected resource to be a stream, received %s instead', |
| 88 |
is_object($stream) ? get_class($stream) : gettype($stream) |
| 89 |
)); |
| 90 |
} |
| 91 |
|
| 92 |
$data = stream_get_meta_data($stream); |
| 93 |
if (!$data['seekable']) { |
| 94 |
throw new InvalidArgumentException('The stream must be seekable'); |
| 95 |
} |
| 96 |
|
| 97 |
$this->stream = $stream; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Set CSV control |
| 102 |
* |
| 103 |
* @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
| 104 |
* |
| 105 |
* @param string $delimiter |
| 106 |
* @param string $enclosure |
| 107 |
* @param string $escape |
| 108 |
*/ |
| 109 |
public function setCsvControl($delimiter = ',', $enclosure = '"', $escape = '\\') |
| 110 |
{ |
| 111 |
$this->delimiter = $this->filterControl($delimiter, 'delimiter'); |
| 112 |
$this->enclosure = $this->filterControl($enclosure, 'enclosure'); |
| 113 |
$this->escape = $this->filterControl($escape, 'escape'); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Filter Csv control character |
| 118 |
* |
| 119 |
* @param string $char Csv control character |
| 120 |
* @param string $type Csv control character type |
| 121 |
* |
| 122 |
* @throws InvalidArgumentException If the Csv control character is not one character only. |
| 123 |
* |
| 124 |
* @return string |
| 125 |
*/ |
| 126 |
private function filterControl($char, $type) |
| 127 |
{ |
| 128 |
if (1 == strlen($char)) { |
| 129 |
return $char; |
| 130 |
} |
| 131 |
|
| 132 |
throw new InvalidArgumentException(sprintf('The %s character must be a single character', $type)); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Set Flags |
| 137 |
* |
| 138 |
* @see http://php.net/manual/en/splfileobject.setflags.php |
| 139 |
* |
| 140 |
* @param int $flags |
| 141 |
*/ |
| 142 |
public function setFlags($flags) |
| 143 |
{ |
| 144 |
if (false === filter_var($flags, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { |
| 145 |
throw new InvalidArgumentException('The flags must be a positive integer'); |
| 146 |
} |
| 147 |
|
| 148 |
$this->flags = $flags; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Write a field array as a CSV line |
| 153 |
* |
| 154 |
* @see http://php.net/manual/en/splfileobject.fputcsv.php |
| 155 |
* |
| 156 |
* @param array $fields |
| 157 |
* @param string $delimiter |
| 158 |
* @param string $enclosure |
| 159 |
* @param string $escape |
| 160 |
* |
| 161 |
* @return int |
| 162 |
*/ |
| 163 |
public function fputcsv(array $fields, $delimiter = ',', $enclosure = '"', $escape = '\\') |
| 164 |
{ |
| 165 |
return fputcsv( |
| 166 |
$this->stream, |
| 167 |
$fields, |
| 168 |
$this->filterControl($delimiter, 'delimiter'), |
| 169 |
$this->filterControl($enclosure, 'enclosure'), |
| 170 |
$this->filterControl($escape, 'escape') |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Retrieves the current line of the file. |
| 176 |
* |
| 177 |
* @return mixed |
| 178 |
*/ |
| 179 |
public function current() |
| 180 |
{ |
| 181 |
if (false !== $this->current_line) { |
| 182 |
return $this->current_line; |
| 183 |
} |
| 184 |
|
| 185 |
if (($this->flags & SplFileObject::READ_CSV) == SplFileObject::READ_CSV) { |
| 186 |
$this->current_line = $this->getCurrentRecord(); |
| 187 |
|
| 188 |
return $this->current_line; |
| 189 |
} |
| 190 |
|
| 191 |
$this->current_line = $this->getCurrentLine(); |
| 192 |
|
| 193 |
return $this->current_line; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Retrieves the current line as a CSV Record |
| 198 |
* |
| 199 |
* @return array |
| 200 |
*/ |
| 201 |
protected function getCurrentRecord() |
| 202 |
{ |
| 203 |
do { |
| 204 |
$ret = fgetcsv($this->stream, 0, $this->delimiter, $this->enclosure, $this->escape); |
| 205 |
} while ($this->flags & SplFileObject::SKIP_EMPTY && $ret !== false && $ret[0] === null); |
| 206 |
|
| 207 |
return $ret; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Retrieves the current line as a string |
| 212 |
* |
| 213 |
* @return string |
| 214 |
*/ |
| 215 |
protected function getCurrentLine() |
| 216 |
{ |
| 217 |
do { |
| 218 |
$line = fgets($this->stream); |
| 219 |
} while ($this->flags & SplFileObject::SKIP_EMPTY && $line !== false && rtrim($line, "\r\n") !== ''); |
| 220 |
|
| 221 |
return $line; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Get line number |
| 226 |
* |
| 227 |
* @return int |
| 228 |
*/ |
| 229 |
public function key() |
| 230 |
{ |
| 231 |
return $this->current_line_number; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Read next line |
| 236 |
*/ |
| 237 |
public function next() |
| 238 |
{ |
| 239 |
$this->current_line = false; |
| 240 |
$this->current_line_number++; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Rewind the file to the first line |
| 245 |
*/ |
| 246 |
public function rewind() |
| 247 |
{ |
| 248 |
rewind($this->stream); |
| 249 |
$this->current_line_number = 0; |
| 250 |
$this->current_line = false; |
| 251 |
if ($this->flags & SplFileObject::READ_AHEAD) { |
| 252 |
$this->current(); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Not at EOF |
| 258 |
* |
| 259 |
* @return bool |
| 260 |
*/ |
| 261 |
public function valid() |
| 262 |
{ |
| 263 |
if ($this->flags & SplFileObject::READ_AHEAD) { |
| 264 |
return $this->current() !== false; |
| 265 |
} |
| 266 |
|
| 267 |
return !feof($this->stream); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Gets line from file |
| 272 |
* |
| 273 |
* @see http://php.net/manual/en/splfileobject.fgets.php |
| 274 |
* |
| 275 |
* @return string |
| 276 |
*/ |
| 277 |
public function fgets() |
| 278 |
{ |
| 279 |
if (false !== $this->current_line) { |
| 280 |
$this->next(); |
| 281 |
} |
| 282 |
return $this->current_line = $this->getCurrentLine(); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Output all remaining data on a file pointer |
| 287 |
* |
| 288 |
* @see http://php.net/manual/en/splfileobject.fpatssthru.php |
| 289 |
* |
| 290 |
* @return int |
| 291 |
*/ |
| 292 |
public function fpassthru() |
| 293 |
{ |
| 294 |
return fpassthru($this->stream); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Seek to a position |
| 299 |
* |
| 300 |
* @see http://php.net/manual/en/splfileobject.fseek.php |
| 301 |
* |
| 302 |
* @param int $offset |
| 303 |
* @param int $whence |
| 304 |
* |
| 305 |
* @return int |
| 306 |
*/ |
| 307 |
public function fseek($offset, $whence = SEEK_SET) |
| 308 |
{ |
| 309 |
return fseek($this->stream, $offset, $whence); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Write to stream |
| 314 |
* |
| 315 |
* @see http://php.net/manual/en/splfileobject.fwrite.php |
| 316 |
* |
| 317 |
* @param string $str |
| 318 |
* @param int $length |
| 319 |
* |
| 320 |
* @return int |
| 321 |
*/ |
| 322 |
public function fwrite($str, $length = 0) |
| 323 |
{ |
| 324 |
return fwrite($this->stream, $str, $length); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* close the file pointer |
| 329 |
*/ |
| 330 |
public function __destruct() |
| 331 |
{ |
| 332 |
$this->stream = null; |
| 333 |
} |
| 334 |
} |
| 335 |
|