Completed
Pull Request — master (#2)
by Robbert
14:41 queued 06:07
created

Parser::parseFull()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.3329

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 28
ccs 14
cts 21
cp 0.6667
rs 8.439
cc 6
eloc 21
nc 10
nop 2
crap 7.3329
1
<?php
2
3
namespace arc\html;
4
5
class Parser 
6
{
7
	public $options = [
8
		'libxml_options' => 0
9
	];
10
11 6 View Code Duplication
	public function __construct( $options = array() )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
	{
13 6
		$optionList = [ 'libxml_options' ];
14 6
		foreach( $options as $option => $optionValue ) {
15
			if ( in_array( $option, $optionList ) ) {
16
				$this->{$option} = $optionValue;
17
			}
18
		}
19 6
	}
20
21 6
	public function parse( $html, $encoding = 'UTF-8' )
22
	{
23 6
		if ( !$html ) {
24
			return \arc\html\Proxy( null );
25
		}
26 6
		if ( $html instanceof Proxy ) { // already parsed
27
			return $html;
28
		}
29 6
		$html = (string) $html;
30 6
		if ( stripos($html, '<html>')!==false ) {
31 5
			return $this->parseFull( $html, $encoding );
32
		} else {		
33 1
			return $this->parsePartial( $html, $encoding );
34
		}
35
	}
36
37 1
	private function parsePartial( $html, $encoding = 'UTF-8' )
38
	{
39 1
		$result = $this->parseFull( '<div id="ArcPartialHTML">'.$html.'</div>', $encoding );
40 1
		if ( $result ) {
41 1
			$result = new \arc\html\Proxy( $result->find('#ArcPartialHTML')[0]->children(), $this );
42
		} else {
43
			throw new \arc\Exception('parse error');
44
		}
45 1
		return $result;
46
	}
47
48 6
	private function parseFull( $html, $encoding = 'UTF-8')
49
	{
50 6
		$dom = new \DomDocument('1.0', $encoding);
51 6
		$prefix = '';
52 6
		if ( isset($encoding) ) {
53 2
			$prefix  = "<head id='ar_html_parser_encoding_header'><meta charset='$encoding'></head>";
54
		}
55 6
		libxml_disable_entity_loader(); // prevents XXE attacks
56 6
		$prevErrorSetting = libxml_use_internal_errors(true);
57 6
		if ( $dom->loadHTML( $prefix . $html, $this->options['libxml_options'] ) ) {
58 6
			if ( isset($encoding) ) {
59 2
				$elm  = $dom->getElementById('ar_html_parser_encoding_header');
60 2
				if ( isset($elm) ) {
61 2
					$elm->parentNode->removeChild($elm);
62
				}
63
			}
64 6
			libxml_use_internal_errors( $prevErrorSetting );
65 6
			return new \arc\html\Proxy( simplexml_import_dom( $dom ), $this );
66
		}
67
		$errors = libxml_get_errors();
68
		libxml_clear_errors();
69
		libxml_use_internal_errors( $prevErrorSetting );
70
		$message = 'Incorrect html passed.';
71
		foreach ( $errors as $error ) {
72
			$message .= "\nline: ".$error->line."; column: ".$error->column."; ".$error->message;
73
		}
74
		throw new \arc\Exception( $message, \arc\exceptions::ILLEGAL_ARGUMENT );
75
	}
76
77
}
78