Completed
Pull Request — develop (#27)
by Chris
20:10 queued 05:10
created

AbstractStream::read()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Stream;
4
5
abstract class AbstractStream implements StreamInterface
6
{
7
    public function read()
8
    {
9
        static $line;
10
        static $lineInfo;
11
        if ($this->getLine() !== $line) {
12
            $line = $this->getLine();
13
            $lineInfo = $this->getLineInfo($line);
14
        }
15
16
        return $lineInfo;
17
    }
18
19
    public function write(array $lineInfo)
20
    {
21
        if (empty($lineInfo['tag'])) {
22
            $this->putLine($lineInfo['value']);
23
24
            return;
25
        }
26
27
        if (empty($lineInfo['value'])) {
28
            $this->putLine($lineInfo['tag']);
29
30
            return;
31
        }
32
33
        $this->putLine(sprintf('%s:%s', $lineInfo['tag'], $lineInfo['value']));
34
    }
35
36
    abstract protected function getLine();
37
38
    abstract protected function putLine($line);
39
40
    private function getLineInfo($line)
41
    {
42
        if (empty($line)) {
43
            return;
44
        }
45
46
        if ('#' !== $line[0]) {
47
            return ['value' => $line];
48
        }
49
50
        list($tag, $value) = array_pad(explode(':', $line, 2), 2, null);
51
52
        return compact('tag', 'value');
53
    }
54
}
55