1 | <?php |
||
9 | class HtmlTokenizer |
||
10 | { |
||
11 | /** @var boolean */ |
||
12 | private $throwOnError; |
||
13 | |||
14 | /** @var string */ |
||
15 | private static $allHtml = ''; |
||
16 | |||
17 | /** |
||
18 | * Constructor |
||
19 | * |
||
20 | * @param bool $throwOnError |
||
21 | */ |
||
22 | 9 | public function __construct(bool $throwOnError = true) |
|
26 | |||
27 | /** |
||
28 | * Will parse html into tokens. |
||
29 | * |
||
30 | * @param $html string The HTML to tokenize. |
||
31 | * |
||
32 | * @return TokenCollection |
||
33 | * @throws \Kevintweber\HtmlTokenizer\Exceptions\TokenMatchingException |
||
34 | */ |
||
35 | 9 | public function parse(string $html) : TokenCollection |
|
36 | { |
||
37 | 9 | self::$allHtml = $html; |
|
38 | 9 | $tokens = new TokenCollection(); |
|
39 | 9 | $remainingHtml = trim($html); |
|
40 | 9 | while (mb_strlen($remainingHtml) > 0) { |
|
41 | 9 | $token = TokenFactory::buildFromHtml( |
|
42 | 9 | $remainingHtml, |
|
43 | 9 | null, |
|
44 | 9 | $this->throwOnError |
|
45 | ); |
||
46 | 9 | if (!$token instanceof Token) { |
|
47 | // Error has occurred, so we stop. |
||
48 | 2 | break; |
|
49 | } |
||
50 | |||
51 | 9 | $remainingHtml = $token->parse($remainingHtml); |
|
52 | 9 | $tokens[] = $token; |
|
53 | } |
||
54 | |||
55 | 8 | return $tokens; |
|
56 | } |
||
57 | |||
58 | 90 | public static function getPosition(string $partialHtml) : array |
|
77 | } |
||
78 |