Completed
Push — master ( 26824d...9bbb30 )
by Kevin
02:15
created

HtmlTokenizer::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Kevintweber\HtmlTokenizer;
4
5
use Kevintweber\HtmlTokenizer\Tokens\TokenCollection;
6
use Kevintweber\HtmlTokenizer\Tokens\TokenFactory;
7
8
class HtmlTokenizer
9
{
10
    /** @var boolean */
11
    private $throwOnError;
12
13
    /**
14
     * Constructor
15
     */
16 2
    public function __construct($throwOnError = false)
17
    {
18 2
        $this->throwOnError = (boolean) $throwOnError;
19 2
    }
20
21
    /**
22
     * Will parse html into tokens.
23
     *
24
     * @param $html string The HTML to tokenize.
25
     *
26
     * @return TokenCollection
27
     */
28 2
    public function parse($html)
29
    {
30 2
        $tokens = new TokenCollection();
31 2
        $remainingHtml = trim((string) $html);
32 2
        while (strlen($remainingHtml) > 0) {
33 2
            $token = TokenFactory::buildFromHtml(
34 2
                $remainingHtml,
35 2
                null,
36 2
                $this->throwOnError
37 2
            );
38 2
            if ($token === false) {
39
                // Error has occurred, so we stop.
40 1
                break;
41
            }
42
43 2
            $remainingHtml = trim($token->parse($remainingHtml));
44 2
            $tokens[] = $token;
45 2
        }
46
47 2
        return $tokens;
48
    }
49
}
50