Reader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace vakata\asn1;
4
5
class Reader
6
{
7
    protected $stream = null;
8
9
    public function __construct($stream)
10
    {
11
        $this->stream = $stream;
12
    }
13
    public static function fromString($data)
14
    {
15
        $stream = fopen('php://temp', 'r+');
16
        if ($stream !== false) {
17
            fwrite($stream, $data);
18
            rewind($stream);
19
            return new static($stream);
20
        }
21
        throw new ASN1Exception('Could not create temp stream');
22
    }
23
    public static function fromFile($path)
24
    {
25
        return new static(fopen($path, 'r+'));
26
    }
27
28
    public function pos()
29
    {
30
        return ftell($this->stream);
31
    }
32
    public function byte()
33
    {
34
        return $this->bytes(1);
35
    }
36
    public function bytes($amount = null)
37
    {
38
        if ($amount === null) {
39
            $buff = '';
40
            while (!feof($this->stream)) {
41
                $buff .= fread($this->stream, 4096);
42
            }
43
            return $buff;
44
        }
45
        return fread($this->stream, $amount);
46
    }
47
    public function readUntil($val, $include = true)
48
    {
49
        $tmp = '';
50
        while (!feof($this->stream)) {
51
            $tmp .= $this->byte();
52
            if (substr($tmp, strlen($val) * -1) === $val) {
53
                break;
54
            }
55
        }
56
        return $include ? $tmp : substr($tmp, 0, strlen($val) * -1);
57
    }
58
    public function chunk($beg = 0, $length = null)
59
    {
60
        return $this->seek($beg)->bytes($length);
61
    }
62
63
    public function eof()
64
    {
65
        return feof($this->stream);
66
    }
67
    public function seek($pos)
68
    {
69
        fseek($this->stream, $pos);
70
        return $this;
71
    }
72
    public function rewind()
73
    {
74
        rewind($this->stream);
75
        return $this;
76
    }
77
}
78