StringReader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A read() 0 8 1
A seekto() 0 6 2
1
<?php
2
3
namespace Gettext\Utils;
4
5
class StringReader
6
{
7
    public $pos;
8
    public $str;
9
    public $strlen;
10
11
    /**
12
     * Constructor.
13
     *
14
     * @param string $str The string to read
15
     */
16
    public function __construct($str)
17
    {
18
        $this->str = $str;
19
        $this->strlen = strlen($this->str);
20
    }
21
22
    /**
23
     * Read and returns a part of the string.
24
     *
25
     * @param int $bytes The number of bytes to read
26
     *
27
     * @return string
28
     */
29
    public function read($bytes)
30
    {
31
        $data = substr($this->str, $this->pos, $bytes);
32
33
        $this->seekto($this->pos + $bytes);
34
35
        return $data;
36
    }
37
38
    /**
39
     * Move the cursor to a specific position.
40
     *
41
     * @param int $pos The amount of bytes to move
42
     *
43
     * @return int The new position
44
     */
45
    public function seekto($pos)
46
    {
47
        $this->pos = ($this->strlen < $pos) ? $this->strlen : $pos;
48
49
        return $this->pos;
50
    }
51
}
52