@@ -34,6 +34,9 @@ discard block |
||
34 | 34 | } |
35 | 35 | } |
36 | 36 | |
37 | + /** |
|
38 | + * @param string $html |
|
39 | + */ |
|
37 | 40 | private function parsePartial( $html, $encoding ) |
38 | 41 | { |
39 | 42 | $result = $this->parseFull( '<div id="ArcPartialHTML">'.$html.'</div>', $encoding ); |
@@ -45,6 +48,9 @@ discard block |
||
45 | 48 | return $result; |
46 | 49 | } |
47 | 50 | |
51 | + /** |
|
52 | + * @param string $html |
|
53 | + */ |
|
48 | 54 | private function parseFull( $html ) |
49 | 55 | { |
50 | 56 | $dom = new \DomDocument(); |
@@ -8,60 +8,60 @@ |
||
8 | 8 | 'libxml_options' => 0 |
9 | 9 | ]; |
10 | 10 | |
11 | - public function __construct( $options = array() ) |
|
11 | + public function __construct($options = array()) |
|
12 | 12 | { |
13 | - $optionList = [ 'libxml_options' ]; |
|
14 | - foreach( $options as $option => $optionValue ) { |
|
15 | - if ( in_array( $option, $optionList ) ) { |
|
13 | + $optionList = ['libxml_options']; |
|
14 | + foreach ($options as $option => $optionValue) { |
|
15 | + if (in_array($option, $optionList)) { |
|
16 | 16 | $this->{$option} = $optionValue; |
17 | 17 | } |
18 | 18 | } |
19 | 19 | } |
20 | 20 | |
21 | - public function parse( $html, $encoding = null ) |
|
21 | + public function parse($html, $encoding = null) |
|
22 | 22 | { |
23 | - if ( !$html ) { |
|
24 | - return \arc\html\Proxy( null ); |
|
23 | + if (!$html) { |
|
24 | + return \arc\html\Proxy(null); |
|
25 | 25 | } |
26 | - if ( $html instanceof Proxy ) { // already parsed |
|
26 | + if ($html instanceof Proxy) { // already parsed |
|
27 | 27 | return $html; |
28 | 28 | } |
29 | - $html = (string) $html; |
|
30 | - if ( stripos($html, '<html>')!==false ) { |
|
31 | - return $this->parseFull( $html, $encoding ); |
|
29 | + $html = (string)$html; |
|
30 | + if (stripos($html, '<html>') !== false) { |
|
31 | + return $this->parseFull($html, $encoding); |
|
32 | 32 | } else { |
33 | - return $this->parsePartial( $html, $encoding ); |
|
33 | + return $this->parsePartial($html, $encoding); |
|
34 | 34 | } |
35 | 35 | } |
36 | 36 | |
37 | - private function parsePartial( $html, $encoding ) |
|
37 | + private function parsePartial($html, $encoding) |
|
38 | 38 | { |
39 | - $result = $this->parseFull( '<div id="ArcPartialHTML">'.$html.'</div>', $encoding ); |
|
40 | - if ( $result ) { |
|
41 | - $result = new \arc\html\Proxy( $result->find('#ArcPartialHTML')[0]->children(), $this ); |
|
39 | + $result = $this->parseFull('<div id="ArcPartialHTML">'.$html.'</div>', $encoding); |
|
40 | + if ($result) { |
|
41 | + $result = new \arc\html\Proxy($result->find('#ArcPartialHTML')[0]->children(), $this); |
|
42 | 42 | } else { |
43 | 43 | throw new \arc\Exception('parse error'); |
44 | 44 | } |
45 | 45 | return $result; |
46 | 46 | } |
47 | 47 | |
48 | - private function parseFull( $html ) |
|
48 | + private function parseFull($html) |
|
49 | 49 | { |
50 | 50 | $dom = new \DomDocument(); |
51 | 51 | libxml_disable_entity_loader(); // prevents XXE attacks |
52 | 52 | $prevErrorSetting = libxml_use_internal_errors(true); |
53 | - if ( $dom->loadHTML( $html, $this->options['libxml_options'] ) ) { |
|
54 | - libxml_use_internal_errors( $prevErrorSetting ); |
|
55 | - return new \arc\html\Proxy( simplexml_import_dom( $dom ), $this ); |
|
53 | + if ($dom->loadHTML($html, $this->options['libxml_options'])) { |
|
54 | + libxml_use_internal_errors($prevErrorSetting); |
|
55 | + return new \arc\html\Proxy(simplexml_import_dom($dom), $this); |
|
56 | 56 | } |
57 | 57 | $errors = libxml_get_errors(); |
58 | 58 | libxml_clear_errors(); |
59 | - libxml_use_internal_errors( $prevErrorSetting ); |
|
59 | + libxml_use_internal_errors($prevErrorSetting); |
|
60 | 60 | $message = 'Incorrect html passed.'; |
61 | - foreach ( $errors as $error ) { |
|
61 | + foreach ($errors as $error) { |
|
62 | 62 | $message .= "\nline: ".$error->line."; column: ".$error->column."; ".$error->message; |
63 | 63 | } |
64 | - throw new \arc\Exception( $message, \arc\exceptions::ILLEGAL_ARGUMENT ); |
|
64 | + throw new \arc\Exception($message, \arc\exceptions::ILLEGAL_ARGUMENT); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | } |
68 | 68 | \ No newline at end of file |
@@ -13,27 +13,27 @@ discard block |
||
13 | 13 | |
14 | 14 | class html extends xml |
15 | 15 | { |
16 | - static private $formBuilder=null; |
|
16 | + static private $formBuilder = null; |
|
17 | 17 | |
18 | - static public function __callStatic( $name, $args ) |
|
18 | + static public function __callStatic($name, $args) |
|
19 | 19 | { |
20 | - return call_user_func_array( [ new html\Writer(), $name ], $args ); |
|
20 | + return call_user_func_array([new html\Writer(), $name], $args); |
|
21 | 21 | } |
22 | 22 | |
23 | - static public function parse( $html, $encoding = null ) |
|
23 | + static public function parse($html, $encoding = null) |
|
24 | 24 | { |
25 | 25 | $parser = new html\Parser(); |
26 | - return $parser->parse( $html, $encoding ); |
|
26 | + return $parser->parse($html, $encoding); |
|
27 | 27 | } |
28 | 28 | |
29 | - static public function formBuilder( $fields, $formAttributes=[] ) { |
|
30 | - if ( !self::$formBuilder ) { |
|
29 | + static public function formBuilder($fields, $formAttributes = []) { |
|
30 | + if (!self::$formBuilder) { |
|
31 | 31 | self::$formBuilder = \arc\prototype::create([ |
32 | 32 | 'fields' => [], |
33 | 33 | 'attributes' => [], |
34 | 34 | ':parseField' => function($self, $field, $key) { |
35 | - if ( !is_array($field) ) { |
|
36 | - $field = [ 'name' => $field, 'label' => $field ]; |
|
35 | + if (!is_array($field)) { |
|
36 | + $field = ['name' => $field, 'label' => $field]; |
|
37 | 37 | } |
38 | 38 | $defaults = [ |
39 | 39 | 'type' => 'text', |
@@ -49,9 +49,9 @@ discard block |
||
49 | 49 | }, |
50 | 50 | ':getValue' => function($self, $field) { |
51 | 51 | $selected = null; |
52 | - if ( $field['value'] ) { |
|
52 | + if ($field['value']) { |
|
53 | 53 | $selected = $field['value']; |
54 | - } else if ( $field['default'] ) { |
|
54 | + } else if ($field['default']) { |
|
55 | 55 | $selected = $field['default']; |
56 | 56 | } |
57 | 57 | return $selected; |
@@ -59,9 +59,9 @@ discard block |
||
59 | 59 | ':renderOptions' => function($self, $field) { |
60 | 60 | $selected = $self->getValue($field); |
61 | 61 | $options = ''; |
62 | - foreach ( (array)$field['options'] as $key => $option ) { |
|
63 | - $attributes = [ 'value' => $key ]; |
|
64 | - if ( $key === $selected ) { |
|
62 | + foreach ((array)$field['options'] as $key => $option) { |
|
63 | + $attributes = ['value' => $key]; |
|
64 | + if ($key === $selected) { |
|
65 | 65 | $attributes['selected'] = true; |
66 | 66 | } |
67 | 67 | $options .= \arc\html::option($attributes, $option); |
@@ -73,16 +73,16 @@ discard block |
||
73 | 73 | [ |
74 | 74 | 'id' => $field['name'], |
75 | 75 | 'name' => $field['name'], |
76 | - ]+(array)$field['inputAttributes'], |
|
76 | + ] + (array)$field['inputAttributes'], |
|
77 | 77 | $self->renderOptions($field) |
78 | 78 | ); |
79 | 79 | }, |
80 | - ':renderInputRadioGroup' => function($self, $field ) { |
|
80 | + ':renderInputRadioGroup' => function($self, $field) { |
|
81 | 81 | $selected = $self->getValue($field); |
82 | 82 | $radios = ''; |
83 | - foreach( (array) $field['options'] as $key => $option ) { |
|
83 | + foreach ((array)$field['options'] as $key => $option) { |
|
84 | 84 | $attributes = $field['inputAttributes']; |
85 | - if ( $key === $selected ) { |
|
85 | + if ($key === $selected) { |
|
86 | 86 | $attributes['checked'] = true; |
87 | 87 | } |
88 | 88 | $radios .= $self->renderInputRadio(['inputAttributes' => $attributes] + $field); |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | $radios |
95 | 95 | ); |
96 | 96 | }, |
97 | - ':renderInputRadio' => function($self, $field ) { |
|
97 | + ':renderInputRadio' => function($self, $field) { |
|
98 | 98 | return \arc\html::input( |
99 | 99 | [ |
100 | 100 | 'type' => 'radio', |
@@ -145,31 +145,31 @@ discard block |
||
145 | 145 | }, |
146 | 146 | ':renderInput' => function($self, $field) { |
147 | 147 | $renderMethod = 'renderInput'.ucfirst($field['type']); |
148 | - if ( !isset($self->{$renderMethod}) ) { |
|
148 | + if (!isset($self->{$renderMethod})) { |
|
149 | 149 | throw new \arc\ExceptionMethodNotFound('No render method for input type '.$field['type'], 404); |
150 | 150 | } |
151 | 151 | return $self->{$renderMethod}($field); |
152 | 152 | }, |
153 | 153 | ':renderLabel' => function($self, $field) { |
154 | - if ( $field['label'] ) { |
|
154 | + if ($field['label']) { |
|
155 | 155 | return \arc\html::label(['for' => $field['name']], $field['label']); |
156 | 156 | } else { |
157 | 157 | return ''; |
158 | 158 | } |
159 | 159 | }, |
160 | - ':renderField' => function($self, $field, $key=null) { |
|
160 | + ':renderField' => function($self, $field, $key = null) { |
|
161 | 161 | $field = $self->parseField($field, $key); |
162 | 162 | $contents = $self->renderLabel($field); |
163 | 163 | $contents .= $self->renderInput($field); |
164 | 164 | $attributes = isset($field['attributes']) ? $field['attributes'] : []; |
165 | - if ( isset($field['class']) ) { |
|
165 | + if (isset($field['class'])) { |
|
166 | 166 | $attributes['class'] = $field['class']; |
167 | 167 | } |
168 | - return \arc\html::div( $attributes, $contents); |
|
168 | + return \arc\html::div($attributes, $contents); |
|
169 | 169 | }, |
170 | 170 | ':__toString' => function($self) { |
171 | 171 | $fields = ''; |
172 | - foreach ( $self->fields as $key => $field ) { |
|
172 | + foreach ($self->fields as $key => $field) { |
|
173 | 173 | $fields .= $self->renderField($field, $key); |
174 | 174 | } |
175 | 175 | return \arc\html::form($self->attributes, $fields); |
@@ -5,22 +5,22 @@ |
||
5 | 5 | class NodeList extends \ArrayObject { |
6 | 6 | use \arc\xml\NodeListTrait; |
7 | 7 | |
8 | - protected function canHaveContent( $tagName ) { |
|
8 | + protected function canHaveContent($tagName) { |
|
9 | 9 | $cantHaveContent = [ |
10 | 10 | 'area', 'base', 'basefont', 'br', |
11 | 11 | 'col', 'frame', 'hr', 'img', 'input', |
12 | 12 | 'isindex', 'link', 'meta', 'param' |
13 | 13 | ]; |
14 | - return !in_array( trim( strtolower( $tagName ) ), $cantHaveContent ); |
|
14 | + return !in_array(trim(strtolower($tagName)), $cantHaveContent); |
|
15 | 15 | } |
16 | 16 | |
17 | - protected function element( $tagName, $attributes, $content ) { |
|
18 | - $tagName = $this->writer->name( $tagName ); |
|
19 | - $el = '<' . $tagName; |
|
20 | - $el .= $this->getAttributes( $attributes ); |
|
21 | - if ( $this->canHaveContent( $tagName ) ) { |
|
22 | - $el .= '>' . self::indent( $this->writer->indent, $content ); |
|
23 | - $el .= '</' . $tagName . '>'; |
|
17 | + protected function element($tagName, $attributes, $content) { |
|
18 | + $tagName = $this->writer->name($tagName); |
|
19 | + $el = '<'.$tagName; |
|
20 | + $el .= $this->getAttributes($attributes); |
|
21 | + if ($this->canHaveContent($tagName)) { |
|
22 | + $el .= '>'.self::indent($this->writer->indent, $content); |
|
23 | + $el .= '</'.$tagName.'>'; |
|
24 | 24 | } else { |
25 | 25 | $el .= '>'; |
26 | 26 | } |
@@ -7,10 +7,10 @@ |
||
7 | 7 | |
8 | 8 | class Proxy extends \arc\xml\Proxy { |
9 | 9 | |
10 | - public function __toString() { |
|
11 | - $dom = dom_import_simplexml($this->target); |
|
12 | - $result = ''.$dom->ownerDocument->saveHTML($dom); |
|
13 | - return $result; |
|
14 | - } |
|
10 | + public function __toString() { |
|
11 | + $dom = dom_import_simplexml($this->target); |
|
12 | + $result = ''.$dom->ownerDocument->saveHTML($dom); |
|
13 | + return $result; |
|
14 | + } |
|
15 | 15 | |
16 | 16 | } |
@@ -6,51 +6,51 @@ discard block |
||
6 | 6 | |
7 | 7 | public $indent = false; |
8 | 8 | |
9 | - public function __construct( $options = [] ) |
|
9 | + public function __construct($options = []) |
|
10 | 10 | { |
11 | 11 | $optionList = ['indent']; |
12 | - foreach( $options as $option => $optionValue ) { |
|
13 | - if ( in_array( $option, $optionList ) ) { |
|
12 | + foreach ($options as $option => $optionValue) { |
|
13 | + if (in_array($option, $optionList)) { |
|
14 | 14 | $this->{$option} = $optionValue; |
15 | 15 | } |
16 | 16 | } |
17 | 17 | } |
18 | 18 | |
19 | - public function __call( $name, $args ) |
|
19 | + public function __call($name, $args) |
|
20 | 20 | { |
21 | - return call_user_func_array( [ new \arc\html\NodeList( [], $this), $name], $args ); |
|
21 | + return call_user_func_array([new \arc\html\NodeList([], $this), $name], $args); |
|
22 | 22 | } |
23 | 23 | |
24 | - static public function name( $name ) |
|
24 | + static public function name($name) |
|
25 | 25 | { |
26 | - return strtolower( \arc\xml::name( $name ) ); |
|
26 | + return strtolower(\arc\xml::name($name)); |
|
27 | 27 | } |
28 | 28 | |
29 | - static public function value( $value ) |
|
29 | + static public function value($value) |
|
30 | 30 | { |
31 | - if ( is_array( $value ) ) { |
|
32 | - $content = array_reduce( $value, function( $result, $value ) { |
|
33 | - return $result . ' ' . self::value( $value ); |
|
31 | + if (is_array($value)) { |
|
32 | + $content = array_reduce($value, function($result, $value) { |
|
33 | + return $result.' '.self::value($value); |
|
34 | 34 | } ); |
35 | - } else if ( is_bool( $value ) ) { |
|
35 | + } else if (is_bool($value)) { |
|
36 | 36 | $content = $value ? 'true' : 'false'; |
37 | 37 | } else { |
38 | - $content = htmlspecialchars( (string) $value, ENT_QUOTES, 'UTF-8' ); |
|
38 | + $content = htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); |
|
39 | 39 | } |
40 | 40 | return $content; |
41 | 41 | } |
42 | 42 | |
43 | - static public function attribute( $name, $value ) |
|
43 | + static public function attribute($name, $value) |
|
44 | 44 | { |
45 | - return \arc\xml::attribute( $name, $value ); |
|
45 | + return \arc\xml::attribute($name, $value); |
|
46 | 46 | } |
47 | 47 | |
48 | - static public function comment( $content ) |
|
48 | + static public function comment($content) |
|
49 | 49 | { |
50 | - return \arc\xml::comment( $content ); |
|
50 | + return \arc\xml::comment($content); |
|
51 | 51 | } |
52 | 52 | |
53 | - static public function doctype( $version='html5' ) |
|
53 | + static public function doctype($version = 'html5') |
|
54 | 54 | { |
55 | 55 | $doctypes = [ |
56 | 56 | 'html5' => '<!doctype html>', |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | 'frameset' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">', |
60 | 60 | 'xhtml' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' |
61 | 61 | ]; |
62 | - return isset( $doctypes[$version] ) ? $doctypes[$version] : $doctypes['html5']; |
|
62 | + return isset($doctypes[$version]) ? $doctypes[$version] : $doctypes['html5']; |
|
63 | 63 | } |
64 | 64 | } |
65 | 65 |