AbstractCsv.php
5 days ago
ByteSequence.php
5 days ago
CannotInsertRecord.php
5 days ago
CharsetConverter.php
5 days ago
ColumnConsistency.php
5 days ago
EncloseField.php
5 days ago
EscapeFormula.php
5 days ago
Exception.php
9 months ago
HTMLConverter.php
5 days ago
Info.php
5 days ago
InvalidArgument.php
9 months ago
MapIterator.php
5 days ago
RFC4180Field.php
5 days ago
Reader.php
5 days ago
ResultSet.php
5 days ago
Statement.php
5 days ago
Stream.php
5 days ago
SyntaxError.php
5 days ago
TabularDataReader.php
5 days ago
UnableToProcessCsv.php
9 months ago
UnavailableFeature.php
9 months ago
UnavailableStream.php
9 months ago
Writer.php
5 days ago
XMLConverter.php
5 days ago
functions.php
9 months ago
functions_include.php
9 months ago
Stream.php
390 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * League.Csv (https://csv.thephpleague.com) |
| 5 | * |
| 6 | * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | declare (strict_types=1); |
| 12 | namespace IAWPSCOPED\League\Csv; |
| 13 | |
| 14 | use ReturnTypeWillChange; |
| 15 | use SeekableIterator; |
| 16 | use SplFileObject; |
| 17 | use TypeError; |
| 18 | use function array_keys; |
| 19 | use function array_walk_recursive; |
| 20 | use function fclose; |
| 21 | use function feof; |
| 22 | use function fflush; |
| 23 | use function fgetcsv; |
| 24 | use function fgets; |
| 25 | use function fopen; |
| 26 | use function fpassthru; |
| 27 | use function fputcsv; |
| 28 | use function fread; |
| 29 | use function fseek; |
| 30 | use function fwrite; |
| 31 | use function get_resource_type; |
| 32 | use function gettype; |
| 33 | use function is_array; |
| 34 | use function is_resource; |
| 35 | use function rewind; |
| 36 | use function stream_filter_append; |
| 37 | use function stream_filter_remove; |
| 38 | use function stream_get_meta_data; |
| 39 | use function strlen; |
| 40 | use const PHP_VERSION_ID; |
| 41 | use const SEEK_SET; |
| 42 | /** |
| 43 | * An object oriented API to handle a PHP stream resource. |
| 44 | * |
| 45 | * @internal used internally to iterate over a stream resource |
| 46 | */ |
| 47 | final class Stream implements SeekableIterator |
| 48 | { |
| 49 | /** @var array<string, array<resource>> Attached filters. */ |
| 50 | private array $filters = []; |
| 51 | /** @var resource */ |
| 52 | private $stream; |
| 53 | private bool $should_close_stream = \false; |
| 54 | /** @var mixed can be a null false or a scalar type value. Current iterator value. */ |
| 55 | private $value; |
| 56 | /** Current iterator key. */ |
| 57 | private int $offset; |
| 58 | /** Flags for the Document.*/ |
| 59 | private int $flags = 0; |
| 60 | private string $delimiter = ','; |
| 61 | private string $enclosure = '"'; |
| 62 | private string $escape = '\\'; |
| 63 | private bool $is_seekable = \false; |
| 64 | /** |
| 65 | * @param mixed $stream stream type resource |
| 66 | */ |
| 67 | public function __construct($stream) |
| 68 | { |
| 69 | if (!is_resource($stream)) { |
| 70 | throw new TypeError('Argument passed must be a stream resource, ' . gettype($stream) . ' given.'); |
| 71 | } |
| 72 | if ('stream' !== ($type = get_resource_type($stream))) { |
| 73 | throw new TypeError('Argument passed must be a stream resource, ' . $type . ' resource given'); |
| 74 | } |
| 75 | $this->is_seekable = stream_get_meta_data($stream)['seekable']; |
| 76 | $this->stream = $stream; |
| 77 | } |
| 78 | public function __destruct() |
| 79 | { |
| 80 | array_walk_recursive($this->filters, fn($filter): bool => @stream_filter_remove($filter)); |
| 81 | if ($this->should_close_stream && is_resource($this->stream)) { |
| 82 | fclose($this->stream); |
| 83 | } |
| 84 | unset($this->stream); |
| 85 | } |
| 86 | public function __clone() |
| 87 | { |
| 88 | throw UnavailableStream::dueToForbiddenCloning(self::class); |
| 89 | } |
| 90 | public function __debugInfo() : array |
| 91 | { |
| 92 | return stream_get_meta_data($this->stream) + ['delimiter' => $this->delimiter, 'enclosure' => $this->enclosure, 'escape' => $this->escape, 'stream_filters' => array_keys($this->filters)]; |
| 93 | } |
| 94 | /** |
| 95 | * Return a new instance from a file path. |
| 96 | * |
| 97 | * @param resource|null $context |
| 98 | * |
| 99 | * @throws Exception if the stream resource can not be created |
| 100 | */ |
| 101 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null) : self |
| 102 | { |
| 103 | $args = [$path, $open_mode]; |
| 104 | if (null !== $context) { |
| 105 | $args[] = \false; |
| 106 | $args[] = $context; |
| 107 | } |
| 108 | $resource = @fopen(...$args); |
| 109 | if (!is_resource($resource)) { |
| 110 | throw UnavailableStream::dueToPathNotFound($path); |
| 111 | } |
| 112 | $instance = new self($resource); |
| 113 | $instance->should_close_stream = \true; |
| 114 | return $instance; |
| 115 | } |
| 116 | /** |
| 117 | * Return a new instance from a string. |
| 118 | */ |
| 119 | public static function createFromString(string $content = '') : self |
| 120 | { |
| 121 | /** @var resource $resource */ |
| 122 | $resource = fopen('php://temp', 'r+'); |
| 123 | fwrite($resource, $content); |
| 124 | $instance = new self($resource); |
| 125 | $instance->should_close_stream = \true; |
| 126 | return $instance; |
| 127 | } |
| 128 | /** |
| 129 | * returns the URI of the underlying stream. |
| 130 | * |
| 131 | * @see https://www.php.net/manual/en/splfileinfo.getpathname.php |
| 132 | */ |
| 133 | public function getPathname() : string |
| 134 | { |
| 135 | return stream_get_meta_data($this->stream)['uri']; |
| 136 | } |
| 137 | /** |
| 138 | * append a filter. |
| 139 | * |
| 140 | * @see http://php.net/manual/en/function.stream-filter-append.php |
| 141 | * |
| 142 | * @throws InvalidArgument if the filter can not be appended |
| 143 | */ |
| 144 | public function appendFilter(string $filtername, int $read_write, array $params = null) : void |
| 145 | { |
| 146 | $res = @stream_filter_append($this->stream, $filtername, $read_write, $params ?? []); |
| 147 | if (!is_resource($res)) { |
| 148 | throw InvalidArgument::dueToStreamFilterNotFound($filtername); |
| 149 | } |
| 150 | $this->filters[$filtername][] = $res; |
| 151 | } |
| 152 | /** |
| 153 | * Set CSV control. |
| 154 | * |
| 155 | * @see http://php.net/manual/en/SplFileObject.setcsvcontrol.php |
| 156 | */ |
| 157 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') : void |
| 158 | { |
| 159 | [$this->delimiter, $this->enclosure, $this->escape] = $this->filterControl($delimiter, $enclosure, $escape, __METHOD__); |
| 160 | } |
| 161 | /** |
| 162 | * Filter Csv control characters. |
| 163 | * |
| 164 | * @throws InvalidArgument If the Csv control character is not one character only. |
| 165 | */ |
| 166 | private function filterControl(string $delimiter, string $enclosure, string $escape, string $caller) : array |
| 167 | { |
| 168 | if (1 !== strlen($delimiter)) { |
| 169 | throw InvalidArgument::dueToInvalidDelimiterCharacter($delimiter, $caller); |
| 170 | } |
| 171 | if (1 !== strlen($enclosure)) { |
| 172 | throw InvalidArgument::dueToInvalidEnclosureCharacter($enclosure, $caller); |
| 173 | } |
| 174 | if (1 === strlen($escape) || '' === $escape && 70400 <= PHP_VERSION_ID) { |
| 175 | return [$delimiter, $enclosure, $escape]; |
| 176 | } |
| 177 | throw InvalidArgument::dueToInvalidEscapeCharacter($escape, $caller); |
| 178 | } |
| 179 | /** |
| 180 | * Set CSV control. |
| 181 | * |
| 182 | * @see http://php.net/manual/en/SplFileObject.getcsvcontrol.php |
| 183 | * |
| 184 | * @return array<string> |
| 185 | */ |
| 186 | public function getCsvControl() : array |
| 187 | { |
| 188 | return [$this->delimiter, $this->enclosure, $this->escape]; |
| 189 | } |
| 190 | /** |
| 191 | * Set CSV stream flags. |
| 192 | * |
| 193 | * @see http://php.net/manual/en/SplFileObject.setflags.php |
| 194 | */ |
| 195 | public function setFlags(int $flags) : void |
| 196 | { |
| 197 | $this->flags = $flags; |
| 198 | } |
| 199 | /** |
| 200 | * Write a field array as a CSV line. |
| 201 | * |
| 202 | * @see http://php.net/manual/en/SplFileObject.fputcsv.php |
| 203 | * |
| 204 | * @return int|false |
| 205 | */ |
| 206 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\', string $eol = "\n") |
| 207 | { |
| 208 | $controls = $this->filterControl($delimiter, $enclosure, $escape, __METHOD__); |
| 209 | if (80100 <= PHP_VERSION_ID) { |
| 210 | $controls[] = $eol; |
| 211 | } |
| 212 | return fputcsv($this->stream, $fields, ...$controls); |
| 213 | } |
| 214 | /** |
| 215 | * Get line number. |
| 216 | * |
| 217 | * @see http://php.net/manual/en/SplFileObject.key.php |
| 218 | */ |
| 219 | public function key() : int |
| 220 | { |
| 221 | return $this->offset; |
| 222 | } |
| 223 | /** |
| 224 | * Read next line. |
| 225 | * |
| 226 | * @see http://php.net/manual/en/SplFileObject.next.php |
| 227 | */ |
| 228 | public function next() : void |
| 229 | { |
| 230 | $this->value = \false; |
| 231 | $this->offset++; |
| 232 | } |
| 233 | /** |
| 234 | * Rewind the file to the first line. |
| 235 | * |
| 236 | * @see http://php.net/manual/en/SplFileObject.rewind.php |
| 237 | * |
| 238 | * @throws Exception if the stream resource is not seekable |
| 239 | */ |
| 240 | public function rewind() : void |
| 241 | { |
| 242 | if (!$this->is_seekable) { |
| 243 | throw UnavailableFeature::dueToMissingStreamSeekability(); |
| 244 | } |
| 245 | rewind($this->stream); |
| 246 | $this->offset = 0; |
| 247 | $this->value = \false; |
| 248 | if (0 !== ($this->flags & SplFileObject::READ_AHEAD)) { |
| 249 | $this->current(); |
| 250 | } |
| 251 | } |
| 252 | /** |
| 253 | * Not at EOF. |
| 254 | * |
| 255 | * @see http://php.net/manual/en/SplFileObject.valid.php |
| 256 | */ |
| 257 | public function valid() : bool |
| 258 | { |
| 259 | if (0 !== ($this->flags & SplFileObject::READ_AHEAD)) { |
| 260 | return $this->current() !== \false; |
| 261 | } |
| 262 | return !feof($this->stream); |
| 263 | } |
| 264 | /** |
| 265 | * Retrieves the current line of the file. |
| 266 | * |
| 267 | * @see http://php.net/manual/en/SplFileObject.current.php |
| 268 | * |
| 269 | * @return mixed The value of the current element. |
| 270 | */ |
| 271 | #[ReturnTypeWillChange] |
| 272 | public function current() |
| 273 | { |
| 274 | if (\false !== $this->value) { |
| 275 | return $this->value; |
| 276 | } |
| 277 | $this->value = $this->getCurrentRecord(); |
| 278 | return $this->value; |
| 279 | } |
| 280 | /** |
| 281 | * Retrieves the current line as a CSV Record. |
| 282 | * |
| 283 | * @return array|false |
| 284 | */ |
| 285 | private function getCurrentRecord() |
| 286 | { |
| 287 | $flag = 0 !== ($this->flags & SplFileObject::SKIP_EMPTY); |
| 288 | do { |
| 289 | $ret = fgetcsv($this->stream, 0, $this->delimiter, $this->enclosure, $this->escape); |
| 290 | } while ($flag && is_array($ret) && null === $ret[0]); |
| 291 | return $ret; |
| 292 | } |
| 293 | /** |
| 294 | * Seek to specified line. |
| 295 | * |
| 296 | * @see http://php.net/manual/en/SplFileObject.seek.php |
| 297 | * |
| 298 | * @param int $position |
| 299 | * @throws Exception if the position is negative |
| 300 | */ |
| 301 | public function seek($position) : void |
| 302 | { |
| 303 | if ($position < 0) { |
| 304 | throw InvalidArgument::dueToInvalidSeekingPosition($position, __METHOD__); |
| 305 | } |
| 306 | $this->rewind(); |
| 307 | while ($this->key() !== $position && $this->valid()) { |
| 308 | $this->current(); |
| 309 | $this->next(); |
| 310 | } |
| 311 | if (0 !== $position) { |
| 312 | $this->offset--; |
| 313 | } |
| 314 | $this->current(); |
| 315 | } |
| 316 | /** |
| 317 | * Output all remaining data on a file pointer. |
| 318 | * |
| 319 | * @see http://php.net/manual/en/SplFileObject.fpatssthru.php |
| 320 | * |
| 321 | * @return int|false |
| 322 | */ |
| 323 | public function fpassthru() |
| 324 | { |
| 325 | return fpassthru($this->stream); |
| 326 | } |
| 327 | /** |
| 328 | * Read from file. |
| 329 | * |
| 330 | * @see http://php.net/manual/en/SplFileObject.fread.php |
| 331 | * |
| 332 | * @param int<0, max> $length The number of bytes to read |
| 333 | * |
| 334 | * @return string|false |
| 335 | */ |
| 336 | public function fread(int $length) |
| 337 | { |
| 338 | return fread($this->stream, $length); |
| 339 | } |
| 340 | /** |
| 341 | * Gets a line from file. |
| 342 | * |
| 343 | * @see http://php.net/manual/en/SplFileObject.fgets.php |
| 344 | * |
| 345 | * @return string|false |
| 346 | */ |
| 347 | public function fgets() |
| 348 | { |
| 349 | return fgets($this->stream); |
| 350 | } |
| 351 | /** |
| 352 | * Seek to a position. |
| 353 | * |
| 354 | * @see http://php.net/manual/en/SplFileObject.fseek.php |
| 355 | * |
| 356 | * @throws Exception if the stream resource is not seekable |
| 357 | */ |
| 358 | public function fseek(int $offset, int $whence = SEEK_SET) : int |
| 359 | { |
| 360 | if (!$this->is_seekable) { |
| 361 | throw UnavailableFeature::dueToMissingStreamSeekability(); |
| 362 | } |
| 363 | return fseek($this->stream, $offset, $whence); |
| 364 | } |
| 365 | /** |
| 366 | * Write to stream. |
| 367 | * |
| 368 | * @see http://php.net/manual/en/SplFileObject.fwrite.php |
| 369 | * |
| 370 | * @return int|false |
| 371 | */ |
| 372 | public function fwrite(string $str, int $length = null) |
| 373 | { |
| 374 | $args = [$this->stream, $str]; |
| 375 | if (null !== $length) { |
| 376 | $args[] = $length; |
| 377 | } |
| 378 | return fwrite(...$args); |
| 379 | } |
| 380 | /** |
| 381 | * Flushes the output to a file. |
| 382 | * |
| 383 | * @see http://php.net/manual/en/SplFileObject.fwrite.php |
| 384 | */ |
| 385 | public function fflush() : bool |
| 386 | { |
| 387 | return fflush($this->stream); |
| 388 | } |
| 389 | } |
| 390 |