OutputStreamWriter::write()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
namespace WebStream\IO\Writer;
4
5
use WebStream\IO\OutputStream;
6
7
/**
8
 * OutputStreamWriter
9
 * @author Ryuichi TANAKA.
10
 * @since 2016/02/24
11
 * @version 0.7
12
 */
13
class OutputStreamWriter
14
{
15
    /**
16
     * @var OutputStream 出力ストリーム
17
     */
18
    protected OutputStream $stream;
19
20
    /**
21
     * constructor
22
     * @param OutputStream $stream 出力ストリーム
23
     */
24 15
    public function __construct(OutputStream $stream)
25
    {
26 15
        $this->stream = $stream;
27
    }
28
29
    /**
30
     * destructor
31
     */
32 17
    public function __destruct()
33
    {
34 17
        $this->close();
35
    }
36
37
    /**
38
     * バッファリングしているすべての出力バイトを書き出し、出力ストリームを閉じる
39
     * @throws WebStream\Exception\Extend\IOException
40
     */
41 15
    public function close()
42
    {
43 15
        $this->stream->close();
44
    }
45
46
    /**
47
     * 出力ストリームに書き出す
48
     * @param mixed $buf 出力データ
49
     * @throws WebStream\Exception\Extend\IOException
50
     */
51 12
    public function write($buf)
52
    {
53 12
        $args = func_get_args();
54 12
        $off = array_key_exists(1, $args) ? $args[1] : null;
55 12
        $len = array_key_exists(2, $args) ? $args[2] : null;
56
57 12
        $this->stream->write($buf, $off, $len);
58
    }
59
60
    /**
61
     * バッファリングしているすべての出力バイトを出力ストリームを閉じずに強制的に書き出す
62
     * writeを実行した時点では書き出されておらず、flushした時点ですべて書き出す
63
     * @throws WebStream\Exception\Extend\IOException
64
     */
65 12
    public function flush()
66
    {
67 12
        $this->stream->flush();
68
    }
69
70
    /**
71
     * 改行を書き出す
72
     * @throws WebStream\Exception\Extend\IOException
73
     */
74
    public function newLine()
75
    {
76
        $this->stream->write(PHP_EOL);
77
    }
78
}
79