|
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 IsWritable Trait. |
|
20
|
|
|
* |
|
21
|
|
|
* Write 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 IsWritable |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Write single line to file/stream. |
|
35
|
|
|
* |
|
36
|
|
|
* Writes a line to the file/stream (if it is writable) |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $line The line to be written to the stream |
|
39
|
|
|
* @param string $eol The end of line string |
|
40
|
|
|
* |
|
41
|
|
|
* @throws IOException |
|
42
|
|
|
* |
|
43
|
|
|
* @return int The number of bytes written to the stream |
|
44
|
|
|
*/ |
|
45
|
15 |
|
public function writeLine($line, $eol = PHP_EOL) |
|
46
|
|
|
{ |
|
47
|
15 |
|
return $this->write($line . $eol); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
abstract public function isWritable(); |
|
51
|
|
|
|
|
52
|
|
|
abstract public function write($str); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Assert that this file/stream object is readable. |
|
56
|
|
|
* |
|
57
|
|
|
* @throws IOException |
|
58
|
|
|
*/ |
|
59
|
18 |
|
protected function assertIsWritable() |
|
60
|
|
|
{ |
|
61
|
18 |
|
if (!$this->isWritable()) { |
|
62
|
2 |
|
throw new IOException('Stream not writable', IOException::ERR_NOT_WRITABLE); |
|
63
|
|
|
} |
|
64
|
16 |
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|