Completed
Push — master ( 68f73c...f03bb1 )
by Kevin
03:04
created

HtmlTokenizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Kevintweber\HtmlTokenizer;
4
5
use Kevintweber\HtmlTokenizer\Tokens\Token;
6
use Kevintweber\HtmlTokenizer\Tokens\TokenCollection;
7
use Kevintweber\HtmlTokenizer\Tokens\TokenFactory;
8
9
class HtmlTokenizer
10
{
11
    /** @var boolean */
12
    private $throwOnError;
13
14
    /** @var string */
15
    private static $allHtml = '';
16
17
    /**
18
     * Constructor
19
     */
20
    public function __construct(bool $throwOnError = true)
21
    {
22
        $this->throwOnError = $throwOnError;
23
    }
24
25
    /**
26
     * Will parse html into tokens.
27
     *
28
     * @param $html string The HTML to tokenize.
29
     *
30
     * @return TokenCollection
31
     */
32
    public function parse(string $html) : TokenCollection
33
    {
34
        self::$allHtml = $html;
35
        $tokens = new TokenCollection();
36
        $remainingHtml = trim((string) $html);
37
        while (mb_strlen($remainingHtml) > 0) {
38
            $token = TokenFactory::buildFromHtml(
39
                $remainingHtml,
40
                null,
41
                $this->throwOnError
42
            );
43
            if (!$token instanceof Token) {
44
                // Error has occurred, so we stop.
45
                break;
46
            }
47
48
            $remainingHtml = $token->parse($remainingHtml);
49
            $tokens[] = $token;
50
        }
51
52
        return $tokens;
53
    }
54
55
    public static function getPosition(string $partialHtml) : array
56
    {
57
        $position = mb_strrpos(self::$allHtml, $partialHtml);
58
        $parsedHtml = mb_substr(self::$allHtml, 0, $position);
59
        $line = mb_substr_count($parsedHtml, "\n");
60
        if ($line === 0) {
61
            return array(
62
                'line' => 0,
63
                'position' => $position
64
            );
65
        }
66
67
        $lastNewLinePosition = mb_strrpos($parsedHtml, "\n");
68
69
        return array(
70
            'line' => $line,
71
            'position' => mb_strlen(mb_substr($parsedHtml, $lastNewLinePosition))
72
        );
73
    }
74
}
75