Completed
Pull Request — master (#2)
by Robbert
07:15
created

Parser::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
ccs 5
cts 8
cp 0.625
rs 9.6666
cc 3
eloc 5
nc 3
nop 1
crap 3.4746
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 6
		}
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 1
		} 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
			$prefix  = <<<EOS
54
<head id='ar_html_parser_encoding_header'>
55
<meta http-equiv="content-type" content="text/html; charset=$encoding">
56 2
</head>
57 2
EOS;
58 2
		}
59 6
		libxml_disable_entity_loader(); // prevents XXE attacks
60 6
		$prevErrorSetting = libxml_use_internal_errors(true);
61 6
		if ( $dom->loadHTML( $prefix . $html, $this->options['libxml_options'] ) ) {
62 6
			if ( isset($encoding) ) {
63 2
				$elm  = $dom->getElementById('ar_html_parser_encoding_header');
64 2
				if ( isset($elm) ) {
65 2
					$elm->parentNode->removeChild($elm);
66 2
				}
67 2
			}
68 6
			libxml_use_internal_errors( $prevErrorSetting );
69 6
			return new \arc\html\Proxy( simplexml_import_dom( $dom ), $this );
70
		}
71
		$errors = libxml_get_errors();
72
		libxml_clear_errors();
73
		libxml_use_internal_errors( $prevErrorSetting );
74
		$message = 'Incorrect html passed.';
75
		foreach ( $errors as $error ) {
76
			$message .= "\nline: ".$error->line."; column: ".$error->column."; ".$error->message;
77
		}
78
		throw new \arc\Exception( $message, \arc\exceptions::ILLEGAL_ARGUMENT );
79
	}
80
81
}
82