@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Viewer; |
4 | 4 | |
5 | -class Viewer{ |
|
5 | +class Viewer { |
|
6 | 6 | use EspecialHelperTrait, CheckTrait; |
7 | 7 | |
8 | 8 | private static $instance = null; |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | |
16 | 16 | public static function getInstance(string $path): Viewer |
17 | 17 | { |
18 | - if(is_null(self::$instance)){ |
|
18 | + if (is_null(self::$instance)) { |
|
19 | 19 | self::$instance = new self(); |
20 | 20 | } |
21 | 21 | self::$instance->path = $path; |
@@ -29,20 +29,20 @@ discard block |
||
29 | 29 | |
30 | 30 | public function render(string $file, array $data = [], bool $return = false): string |
31 | 31 | { |
32 | - if(!headers_sent()){ |
|
32 | + if (!headers_sent()) { |
|
33 | 33 | header('Content-Type: text/html; charset=utf-8'); |
34 | 34 | } |
35 | 35 | |
36 | 36 | $this->check_viewExist($file); |
37 | 37 | |
38 | - $buffer = $this->getOB($this->path . DIRECTORY_SEPARATOR . $file . '.view.php', $data); |
|
38 | + $buffer = $this->getOB($this->path.DIRECTORY_SEPARATOR.$file.'.view.php', $data); |
|
39 | 39 | |
40 | 40 | $buffer = $this->getVars($buffer); |
41 | 41 | $buffer = $this->getEspecialVars($buffer); |
42 | 42 | |
43 | 43 | $buffer = $this->removeComments($buffer); |
44 | 44 | |
45 | - if(!$return){ |
|
45 | + if (!$return) { |
|
46 | 46 | echo $buffer; |
47 | 47 | return ''; |
48 | 48 | } |
@@ -52,12 +52,12 @@ discard block |
||
52 | 52 | |
53 | 53 | public function include(string $file): void |
54 | 54 | { |
55 | - try{ |
|
55 | + try { |
|
56 | 56 | $buffer = $this->getOB($this->path.$file.'.inc.php'); |
57 | 57 | $buffer = $this->getVars($buffer); |
58 | 58 | $buffer = $this->getEspecialVars($buffer); |
59 | 59 | echo $buffer; |
60 | - }catch(\Exception $er){ |
|
60 | + }catch (\Exception $er) { |
|
61 | 61 | echo "<div class='view error'>Component error: {$er->getMessage()}</div>"; |
62 | 62 | } |
63 | 63 | } |
@@ -57,7 +57,7 @@ |
||
57 | 57 | $buffer = $this->getVars($buffer); |
58 | 58 | $buffer = $this->getEspecialVars($buffer); |
59 | 59 | echo $buffer; |
60 | - }catch(\Exception $er){ |
|
60 | + } catch(\Exception $er){ |
|
61 | 61 | echo "<div class='view error'>Component error: {$er->getMessage()}</div>"; |
62 | 62 | } |
63 | 63 | } |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Viewer; |
4 | 4 | |
5 | -trait EspecialHelperTrait{ |
|
5 | +trait EspecialHelperTrait { |
|
6 | 6 | use HelperTrait; |
7 | 7 | |
8 | 8 | protected function getEspecialVars(string $buffer): string |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | protected function replaceEspecialVars(string $buffer, array $vars, ?string $prefix = ''): string |
14 | 14 | { |
15 | 15 | foreach ($vars as $key => $value) { |
16 | - switch(gettype($value)){ |
|
16 | + switch (gettype($value)) { |
|
17 | 17 | case 'array': |
18 | 18 | $buffer = $this->replaceEspecialArray($buffer, $value, $prefix, $key); |
19 | 19 | break; |
@@ -31,9 +31,9 @@ discard block |
||
31 | 31 | |
32 | 32 | protected function replaceEspecialValue(string $buffer, $value, ?string $prefix, string $key): string |
33 | 33 | { |
34 | - if(gettype($value)!=='array' && gettype($value)!=='object'){ |
|
35 | - while(strstr($buffer,'{{!! $'.$prefix.$key.' !!}}')){ |
|
36 | - $buffer = str_replace('{{!! $'.$prefix.$key.' !!}}', $value ,$buffer); |
|
34 | + if (gettype($value)!=='array' && gettype($value)!=='object') { |
|
35 | + while (strstr($buffer, '{{!! $'.$prefix.$key.' !!}}')) { |
|
36 | + $buffer = str_replace('{{!! $'.$prefix.$key.' !!}}', $value, $buffer); |
|
37 | 37 | } |
38 | 38 | } |
39 | 39 | return $buffer; |
@@ -41,14 +41,14 @@ discard block |
||
41 | 41 | |
42 | 42 | protected function replaceEspecialObject(string $buffer, object $obj, ?string $prefix, string $key): string |
43 | 43 | { |
44 | - $vars = method_exists($obj,'getVars') ? $obj->getVars() : []; |
|
44 | + $vars = method_exists($obj, 'getVars') ? $obj->getVars() : []; |
|
45 | 45 | $vars = array_merge($vars, get_object_vars($obj)); |
46 | - foreach($vars as $field => $val){ |
|
46 | + foreach ($vars as $field => $val) { |
|
47 | 47 | |
48 | - $buffer = $this->replaceEspecialValue($buffer, $val, $key.'.'.$field.'.' , $field); |
|
48 | + $buffer = $this->replaceEspecialValue($buffer, $val, $key.'.'.$field.'.', $field); |
|
49 | 49 | |
50 | - while(strstr($buffer,'{{!! $'.$prefix.$key.'.'.$field.' !!}}')){ |
|
51 | - $buffer = str_replace('{{!! $'.$prefix.$key.'.'.$field.' !!}}', $obj->$field ,$buffer); |
|
50 | + while (strstr($buffer, '{{!! $'.$prefix.$key.'.'.$field.' !!}}')) { |
|
51 | + $buffer = str_replace('{{!! $'.$prefix.$key.'.'.$field.' !!}}', $obj->$field, $buffer); |
|
52 | 52 | } |
53 | 53 | } |
54 | 54 | return $buffer; |
@@ -56,11 +56,11 @@ discard block |
||
56 | 56 | |
57 | 57 | protected function replaceEspecialArray(string $buffer, array $array, ?string $prefix = '', ?string $key = ''): string |
58 | 58 | { |
59 | - foreach($array as $field => $val){ |
|
60 | - $buffer = $this->replaceEspecialValue($buffer, $val, $key.'.'.$field.'.' , $field); |
|
59 | + foreach ($array as $field => $val) { |
|
60 | + $buffer = $this->replaceEspecialValue($buffer, $val, $key.'.'.$field.'.', $field); |
|
61 | 61 | |
62 | - while(strstr($buffer,'{{!! $'.$prefix.$key.'.'.$field.' !!}}')){ |
|
63 | - $buffer = str_replace('{{!! $'.$prefix.$key.'.'.$field.' !!}}', $val ,$buffer); |
|
62 | + while (strstr($buffer, '{{!! $'.$prefix.$key.'.'.$field.' !!}}')) { |
|
63 | + $buffer = str_replace('{{!! $'.$prefix.$key.'.'.$field.' !!}}', $val, $buffer); |
|
64 | 64 | } |
65 | 65 | } |
66 | 66 | return $buffer; |
@@ -4,25 +4,25 @@ discard block |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -trait HelperTrait{ |
|
7 | +trait HelperTrait { |
|
8 | 8 | use CheckTrait, JScriptTrait; |
9 | 9 | |
10 | 10 | public array $data = []; |
11 | 11 | |
12 | 12 | protected function getOB(string $require, array $data = []): string |
13 | 13 | { |
14 | - foreach($data as $variable => $_){ |
|
14 | + foreach ($data as $variable => $_) { |
|
15 | 15 | $$variable = $_; |
16 | 16 | } |
17 | 17 | |
18 | - $_ = (array_key_exists('_',$data)) ? $data['_'] : null; |
|
18 | + $_ = (array_key_exists('_', $data)) ? $data['_'] : null; |
|
19 | 19 | |
20 | - if(!file_exists($require)){ |
|
20 | + if (!file_exists($require)) { |
|
21 | 21 | $require = basename($require); |
22 | 22 | throw new Exception("Importation file does not exist: {$require} ."); |
23 | 23 | } |
24 | 24 | |
25 | - $this->data = array_merge($this->data,$data); |
|
25 | + $this->data = array_merge($this->data, $data); |
|
26 | 26 | |
27 | 27 | ob_start(); |
28 | 28 | require($require); |
@@ -34,21 +34,21 @@ discard block |
||
34 | 34 | |
35 | 35 | private function treatHTML(string $html): string |
36 | 36 | { |
37 | - $arrayHtml = explode(PHP_EOL,$html); |
|
37 | + $arrayHtml = explode(PHP_EOL, $html); |
|
38 | 38 | $html = []; |
39 | 39 | |
40 | 40 | $inScript = false; |
41 | 41 | $inComment = false; |
42 | 42 | |
43 | - foreach($arrayHtml as $index => $value){ |
|
43 | + foreach ($arrayHtml as $index => $value) { |
|
44 | 44 | $inScript = $this->checkInScript($inScript, $value); |
45 | 45 | |
46 | - if($inScript){ |
|
46 | + if ($inScript) { |
|
47 | 47 | $inComment = $this->checkCommentInScript($inComment, $value); |
48 | 48 | |
49 | - if(!$this->checkScriptNeed($inComment, $value)){ |
|
49 | + if (!$this->checkScriptNeed($inComment, $value)) { |
|
50 | 50 | continue; |
51 | - }else{ |
|
51 | + }else { |
|
52 | 52 | $value = $this->treatScript($value); |
53 | 53 | } |
54 | 54 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | $html[$index] = ltrim($value); |
57 | 57 | } |
58 | 58 | |
59 | - return implode('',$html); |
|
59 | + return implode('', $html); |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | protected function getVars(string $buffer): string |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | protected function replaceVars(string $buffer, array $vars, ?string $prefix = ''): string |
68 | 68 | { |
69 | 69 | foreach ($vars as $key => $value) { |
70 | - switch(gettype($value)){ |
|
70 | + switch (gettype($value)) { |
|
71 | 71 | case 'array': |
72 | 72 | $buffer = $this->replaceArray($buffer, $value, $prefix, $key); |
73 | 73 | break; |
@@ -85,9 +85,9 @@ discard block |
||
85 | 85 | |
86 | 86 | protected function replaceValue(string $buffer, $value, ?string $prefix, string $key): string |
87 | 87 | { |
88 | - if(gettype($value)!=='array' && gettype($value)!=='object'){ |
|
89 | - while(strstr($buffer,'{{ $'.$prefix.$key.' }}')){ |
|
90 | - $buffer = str_replace('{{ $'.$prefix.$key.' }}', htmlspecialchars($value) ,$buffer); |
|
88 | + if (gettype($value)!=='array' && gettype($value)!=='object') { |
|
89 | + while (strstr($buffer, '{{ $'.$prefix.$key.' }}')) { |
|
90 | + $buffer = str_replace('{{ $'.$prefix.$key.' }}', htmlspecialchars($value), $buffer); |
|
91 | 91 | } |
92 | 92 | } |
93 | 93 | return $buffer; |
@@ -95,14 +95,14 @@ discard block |
||
95 | 95 | |
96 | 96 | protected function replaceObject(string $buffer, object $obj, ?string $prefix, string $key): string |
97 | 97 | { |
98 | - $vars = method_exists($obj,'getVars') ? $obj->getVars() : []; |
|
98 | + $vars = method_exists($obj, 'getVars') ? $obj->getVars() : []; |
|
99 | 99 | $vars = array_merge($vars, get_object_vars($obj)); |
100 | - foreach($vars as $field => $val){ |
|
100 | + foreach ($vars as $field => $val) { |
|
101 | 101 | |
102 | - $buffer = $this->replaceValue($buffer, $val, $key.'.'.$field.'.' , $field); |
|
102 | + $buffer = $this->replaceValue($buffer, $val, $key.'.'.$field.'.', $field); |
|
103 | 103 | |
104 | - while(strstr($buffer,'{{ $'.$prefix.$key.'.'.$field.' }}')){ |
|
105 | - $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($obj->$field) ,$buffer); |
|
104 | + while (strstr($buffer, '{{ $'.$prefix.$key.'.'.$field.' }}')) { |
|
105 | + $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($obj->$field), $buffer); |
|
106 | 106 | } |
107 | 107 | } |
108 | 108 | return $buffer; |
@@ -110,11 +110,11 @@ discard block |
||
110 | 110 | |
111 | 111 | protected function replaceArray(string $buffer, array $array, ?string $prefix = '', ?string $key = ''): string |
112 | 112 | { |
113 | - foreach($array as $field => $val){ |
|
114 | - $buffer = $this->replaceValue($buffer, $val, $key.'.'.$field.'.' , $field); |
|
113 | + foreach ($array as $field => $val) { |
|
114 | + $buffer = $this->replaceValue($buffer, $val, $key.'.'.$field.'.', $field); |
|
115 | 115 | |
116 | - while(strstr($buffer,'{{ $'.$prefix.$key.'.'.$field.' }}')){ |
|
117 | - $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val) ,$buffer); |
|
116 | + while (strstr($buffer, '{{ $'.$prefix.$key.'.'.$field.' }}')) { |
|
117 | + $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val), $buffer); |
|
118 | 118 | } |
119 | 119 | } |
120 | 120 | return $buffer; |
@@ -122,13 +122,13 @@ discard block |
||
122 | 122 | |
123 | 123 | protected function removeComments(string $buffer): string |
124 | 124 | { |
125 | - while(strstr($buffer,'<!--')){ |
|
125 | + while (strstr($buffer, '<!--')) { |
|
126 | 126 | $comment = substr( |
127 | 127 | $buffer, |
128 | - strpos($buffer,'<!--'), |
|
129 | - strpos(strstr($buffer,'<!--'),'-->')+3 |
|
128 | + strpos($buffer, '<!--'), |
|
129 | + strpos(strstr($buffer, '<!--'), '-->')+3 |
|
130 | 130 | ); |
131 | - $buffer = str_replace($comment,'',$buffer); |
|
131 | + $buffer = str_replace($comment, '', $buffer); |
|
132 | 132 | } |
133 | 133 | return $buffer; |
134 | 134 | } |