Completed
Push — develop ( a97288...ea3597 )
by Stéphane
02:09
created

Content::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Bee4\RobotsTxt;
4
5
/**
6
 * Class Content
7
 * Represent the content of a robots.txt file
8
 * It can be crawled as an Iterator
9
 *
10
 * @copyright Bee4 2015
11
 * @author    Stephane HULARD <[email protected]>
12
 */
13
class Content implements \Iterator
14
{
15
    const UTF8_BOM = "\xEF\xBB\xBF";
16
17
    /**
18
     * Robots.txt file content
19
     * @var string
20
     */
21
    protected $content;
22
23
    /**
24
     * Reader separator
25
     * @var string
26
     */
27
    protected $separator;
28
29
    /**
30
     * Current line
31
     * @var string
32
     */
33
    private $line;
34
35
    /**
36
     * Current iterator key
37
     * @var integer
38
     */
39
    private $read = 0;
40
41
    /**
42
     * @param string $content
43
     */
44
    public function __construct($content, $separator = "\r\n")
45
    {
46
        //Remove the UTF8 BOM
47 1
        $this->content = trim($content, self::UTF8_BOM);
48 1
        $this->separator = $separator;
49 1
    }
50
51
    /**
52
     * Retrieve current line
53
     * @return string
54
     */
55
    public function current()
56
    {
57 1
        return $this->line;
58
    }
59
60
    /**
61
     * Number of chars read
62
     * @return integer
63
     */
64
    public function key()
65
    {
66 1
        return $this->read;
67
    }
68
69
    /**
70
     * Get the next line in content
71
     */
72
    public function next()
73
    {
74 1
        if ($this->line !== null) {
75 1
            $this->line = strtok($this->separator);
76 1
        } else {
77 1
            $this->line = strtok($this->content, $this->separator);
78
        }
79 1
        $this->read += strlen($this->line);
80 1
    }
81
82
    /**
83
     * Rewind at beginning
84
     */
85
    public function rewind()
86
    {
87 1
        $this->line = null;
88 1
        $this->read = 0;
89 1
    }
90
91
    /**
92
     * Check if current item is valid or not
93
     * @return boolean
94
     */
95
    public function valid()
96
    {
97 1
        return $this->line !== false;
98
    }
99
}
100