1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CSVelte: Slender, elegant CSV for PHP |
4
|
|
|
* |
5
|
|
|
* Inspired by Python's CSV module and Frictionless Data and the W3C's CSV |
6
|
|
|
* standardization efforts, CSVelte was written in an effort to take all the |
7
|
|
|
* suck out of working with CSV. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) 2018 Luke Visinoni |
10
|
|
|
* @author Luke Visinoni <[email protected]> |
11
|
|
|
* @license See LICENSE file (MIT license) |
12
|
|
|
*/ |
13
|
|
|
namespace CSVelte\Traits; |
14
|
|
|
|
15
|
|
|
use CSVelte\Exception\IOException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* IO IsReadable Trait. |
19
|
|
|
* |
20
|
|
|
* Read methods shared between CSVelte\IO classes. |
21
|
|
|
*/ |
22
|
|
|
trait IsReadable |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Read single line. |
26
|
|
|
* Read the next line from the file (moving the internal pointer down a line). |
27
|
|
|
* Returns multiple lines if newline character(s) fall within a quoted string. |
28
|
|
|
* |
29
|
|
|
* @param string|array $eol A string or array of strings to be used as EOL char/sequence |
30
|
|
|
* @param int $maxLength Maximum number of bytes to return (line will be truncated to this -1 if set) |
31
|
|
|
* |
32
|
|
|
* @throws IOException |
33
|
|
|
* |
34
|
|
|
* @return string A single line read from the file. |
35
|
|
|
* |
36
|
|
|
* @todo Should this add a newline if maxlength is reached? |
37
|
|
|
* @todo I could actually buffer this by reading x chars at a time and doing |
38
|
|
|
* the same thing with looping char by char if this is too IO intensive. |
39
|
|
|
*/ |
40
|
19 |
|
public function readLine($eol = PHP_EOL, $maxLength = null) |
41
|
|
|
{ |
42
|
19 |
|
$size = 0; |
43
|
19 |
|
$buffer = false; |
44
|
19 |
|
if (!is_array($eol)) { |
45
|
18 |
|
$eol = [$eol]; |
46
|
18 |
|
} |
47
|
19 |
|
while (!$this->eof()) { |
48
|
|
|
// Using a loose equality here to match on '' and false. |
49
|
19 |
|
if (null == ($byte = $this->read(1))) { |
50
|
|
|
return $buffer; |
51
|
|
|
} |
52
|
19 |
|
$buffer .= $byte; |
53
|
|
|
// Break when a new line is found or the max length - 1 is reached |
54
|
19 |
|
if (array_reduce($eol, function ($carry, $eol) use ($buffer) { |
55
|
19 |
|
if (!$carry) { |
56
|
19 |
|
$eollen = 0 - strlen($eol); |
57
|
|
|
|
58
|
19 |
|
return substr($buffer, $eollen) === $eol; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return true; |
62
|
19 |
|
}, false) || ++$size === $maxLength - 1) { |
63
|
19 |
|
break; |
64
|
|
|
} |
65
|
19 |
|
} |
66
|
|
|
|
67
|
19 |
|
return $buffer; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
abstract public function isReadable(); |
71
|
|
|
|
72
|
|
|
abstract public function read($length); |
73
|
|
|
|
74
|
|
|
abstract public function eof(); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Assert that this file/stream object is readable. |
78
|
|
|
* |
79
|
|
|
* @throws IOException |
80
|
|
|
*/ |
81
|
36 |
|
protected function assertIsReadable() |
82
|
|
|
{ |
83
|
36 |
|
if (!$this->isReadable()) { |
84
|
2 |
|
throw new IOException('Stream not readable', IOException::ERR_NOT_READABLE); |
85
|
|
|
} |
86
|
34 |
|
} |
87
|
|
|
} |
88
|
|
|
|