1 | <?php |
||
10 | class StringReader extends Reader |
||
11 | { |
||
12 | /** |
||
13 | * The raw string of bytes. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $string; |
||
18 | |||
19 | /** |
||
20 | * The current position in the string. |
||
21 | * |
||
22 | * @var int |
||
23 | */ |
||
24 | protected $position; |
||
25 | |||
26 | /** |
||
27 | * The string length. |
||
28 | * |
||
29 | * @var int |
||
30 | */ |
||
31 | protected $length; |
||
32 | |||
33 | /** |
||
34 | * Initializes the instance. |
||
35 | * |
||
36 | * @param string $string The raw string containing the data to be read. |
||
37 | */ |
||
38 | 23 | public function __construct($string) |
|
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | * |
||
48 | * @see Reader::setPosition() |
||
49 | */ |
||
50 | public function setPosition($position) |
||
51 | { |
||
52 | if ($position < 0 || $position > $this->length) { |
||
53 | throw new Exception('Failed to seek string to to position '.$position); |
||
54 | } |
||
55 | $this->position = $position; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | * |
||
61 | * @see Reader::getPosition() |
||
62 | */ |
||
63 | public function getPosition() |
||
64 | { |
||
65 | return $this->position; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | * |
||
71 | * @see Reader::getLength() |
||
72 | */ |
||
73 | public function getLength() |
||
74 | { |
||
75 | return $this->length; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | * |
||
81 | * @see Reader::readString() |
||
82 | */ |
||
83 | 23 | public function readString($length) |
|
96 | } |
||
97 |