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