| @@ -18,7 +18,7 @@ | ||
| 18 | 18 | Viewer::create(__DIR__.'/Views/') | 
| 19 | 19 |            ->render('default', $data); | 
| 20 | 20 | |
| 21 | -}catch(Exception $er){ | |
| 21 | +} catch(Exception $er){ | |
| 22 | 22 | |
| 23 | 23 |      die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); | 
| 24 | 24 | |
| @@ -8,7 +8,7 @@ discard block | ||
| 8 | 8 | |
| 9 | 9 | /* NOTE: in case of error an exception is thrown */ | 
| 10 | 10 | |
| 11 | -try{ | |
| 11 | +try { | |
| 12 | 12 | |
| 13 | 13 | $data = ['parameter'=> | 
| 14 | 14 | [ | 
| @@ -20,7 +20,7 @@ discard block | ||
| 20 | 20 | Viewer::create(__DIR__.'/Views/') | 
| 21 | 21 |            ->render('default', $data); | 
| 22 | 22 | |
| 23 | -}catch(Exception $er){ | |
| 23 | +}catch (Exception $er) { | |
| 24 | 24 | |
| 25 | 25 |      die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); | 
| 26 | 26 | |
| @@ -4,11 +4,11 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Exception; | 
| 6 | 6 | |
| 7 | -trait CheckTrait{ | |
| 7 | +trait CheckTrait { | |
| 8 | 8 | |
| 9 | -    protected function check_viewExist(string $viewfile){ | |
| 10 | -        if(!file_exists($this->path . $viewfile . '.view.php')){ | |
| 11 | - $v = $this->path . $viewfile; | |
| 9 | +    protected function check_viewExist(string $viewfile) { | |
| 10 | +        if (!file_exists($this->path.$viewfile.'.view.php')) { | |
| 11 | + $v = $this->path.$viewfile; | |
| 12 | 12 |              throw new Exception("Preview file {$v} not found"); | 
| 13 | 13 | } | 
| 14 | 14 | } | 
| @@ -59,7 +59,7 @@ | ||
| 59 | 59 | $buffer = $this->getOB($this->path.$file.'.inc.php'); | 
| 60 | 60 | $buffer = $this->getVars($buffer); | 
| 61 | 61 | $buffer = $this->getEspecialVars($buffer); | 
| 62 | -        }catch(\Exception $er){ | |
| 62 | +        } catch(\Exception $er){ | |
| 63 | 63 |              $buffer = "<div class='view error'>Component error: {$er->getMessage()}</div>"; | 
| 64 | 64 | } | 
| 65 | 65 | echo $buffer; | 
| @@ -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 | } | 
| @@ -53,11 +53,11 @@ discard block | ||
| 53 | 53 | public function include(string $file): void | 
| 54 | 54 |      { | 
| 55 | 55 | $buffer = ''; | 
| 56 | -        try{ | |
| 56 | +        try { | |
| 57 | 57 | $buffer = $this->getOB($this->path.$file.'.inc.php'); | 
| 58 | 58 | $buffer = $this->getVars($buffer); | 
| 59 | 59 | $buffer = $this->getEspecialVars($buffer); | 
| 60 | -        }catch(\Exception $er){ | |
| 60 | +        }catch (\Exception $er) { | |
| 61 | 61 |              $buffer = "<div class='view error'>Component error: {$er->getMessage()}</div>"; | 
| 62 | 62 | } | 
| 63 | 63 | echo $buffer; | 
| @@ -3,14 +3,14 @@ discard block | ||
| 3 | 3 | namespace HnrAzevedo\Viewer; | 
| 4 | 4 | |
| 5 | 5 | |
| 6 | -trait JScriptTrait{ | |
| 6 | +trait JScriptTrait { | |
| 7 | 7 | |
| 8 | 8 | protected function checkInScript(bool $inScript, string $value): bool | 
| 9 | 9 |      { | 
| 10 | -        if((substr(ltrim($value),0,8) === '<script>' && !strpos($value,'src'))){ | |
| 10 | +        if ((substr(ltrim($value), 0, 8)==='<script>' && !strpos($value, 'src'))) { | |
| 11 | 11 | $inScript = true; | 
| 12 | 12 | } | 
| 13 | -        if((substr(ltrim($value),0,9) === '</script>')){ | |
| 13 | +        if ((substr(ltrim($value), 0, 9)==='</script>')) { | |
| 14 | 14 | $inScript = false; | 
| 15 | 15 | } | 
| 16 | 16 | return $inScript; | 
| @@ -18,17 +18,17 @@ discard block | ||
| 18 | 18 | |
| 19 | 19 | protected function checkCommentInScript(bool $inComment, string $value): bool | 
| 20 | 20 |      { | 
| 21 | -        if(strpos($value,'/*') && !strpos($value,'*/')){ | |
| 21 | +        if (strpos($value, '/*') && !strpos($value, '*/')) { | |
| 22 | 22 | $inComment = true; | 
| 23 | -        }else{ | |
| 24 | - $inComment = (strpos($value,'*/')) ? false : $inComment; | |
| 23 | +        }else { | |
| 24 | + $inComment = (strpos($value, '*/')) ? false : $inComment; | |
| 25 | 25 | } | 
| 26 | 26 | return $inComment; | 
| 27 | 27 | } | 
| 28 | 28 | |
| 29 | 29 | protected function checkScriptNeed(bool $inComment, string $value): bool | 
| 30 | 30 |      { | 
| 31 | -        if(($inComment || strpos($value,'//'))){ | |
| 31 | +        if (($inComment || strpos($value, '//'))) { | |
| 32 | 32 | return false; | 
| 33 | 33 | } | 
| 34 | 34 | return true; | 
| @@ -36,12 +36,12 @@ discard block | ||
| 36 | 36 | |
| 37 | 37 | protected function treatScript(string $value): string | 
| 38 | 38 |      { | 
| 39 | -        while(  strpos($value,'/*') && strpos($value,'*/')  ){ | |
| 40 | - $replace = substr($value,strripos ($value,'/*'),strripos ($value,'*/')+2); | |
| 41 | - $value = str_replace($replace,'',$value); | |
| 39 | +        while (strpos($value, '/*') && strpos($value, '*/')) { | |
| 40 | + $replace = substr($value, strripos($value, '/*'), strripos($value, '*/')+2); | |
| 41 | + $value = str_replace($replace, '', $value); | |
| 42 | 42 | } | 
| 43 | -        if(strpos($value,'*/')){ | |
| 44 | - $value = substr($value,strpos($value,'*/')+2); | |
| 43 | +        if (strpos($value, '*/')) { | |
| 44 | + $value = substr($value, strpos($value, '*/')+2); | |
| 45 | 45 | } | 
| 46 | 46 | return $value; | 
| 47 | 47 | } | 
| @@ -20,7 +20,7 @@ | ||
| 20 | 20 |      { | 
| 21 | 21 |          if(strpos($value,'/*') && !strpos($value,'*/')){ | 
| 22 | 22 | $inComment = true; | 
| 23 | -        }else{ | |
| 23 | +        } else{ | |
| 24 | 24 | $inComment = (strpos($value,'*/')) ? false : $inComment; | 
| 25 | 25 | } | 
| 26 | 26 | return $inComment; | 
| @@ -51,7 +51,7 @@ | ||
| 51 | 51 | |
| 52 | 52 |                  if(!$this->checkScriptNeed($inComment, $value)){ | 
| 53 | 53 | continue; | 
| 54 | -                }else{ | |
| 54 | +                } else{ | |
| 55 | 55 | $value = $this->treatScript($value); | 
| 56 | 56 | } | 
| 57 | 57 | } | 
| @@ -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,12 +95,12 @@ discard block | ||
| 95 | 95 | |
| 96 | 96 | protected function replaceObject(string $buffer, object $obj, string $prefix, string $key): string | 
| 97 | 97 |      { | 
| 98 | -        foreach(get_object_vars($obj) as $field => $val){ | |
| 98 | +        foreach (get_object_vars($obj) as $field => $val) { | |
| 99 | 99 | |
| 100 | - $buffer = $this->replaceValue($buffer, $val, $key.'.'.$field.'.' , $field); | |
| 100 | + $buffer = $this->replaceValue($buffer, $val, $key.'.'.$field.'.', $field); | |
| 101 | 101 | |
| 102 | -            while(strstr($buffer,'{{ $'.$prefix.$key.'.'.$field.' }}')){ | |
| 103 | -                $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val) ,$buffer); | |
| 102 | +            while (strstr($buffer, '{{ $'.$prefix.$key.'.'.$field.' }}')) { | |
| 103 | +                $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val), $buffer); | |
| 104 | 104 | } | 
| 105 | 105 | } | 
| 106 | 106 | return $buffer; | 
| @@ -108,11 +108,11 @@ discard block | ||
| 108 | 108 | |
| 109 | 109 | protected function replaceArray(string $buffer, array $array, ?string $prefix = '', ?string $key = ''): string | 
| 110 | 110 |      { | 
| 111 | -        foreach($array as $field => $val){ | |
| 112 | - $buffer = $this->replaceValue($buffer, $val, $key.'.'.$field.'.' , $field); | |
| 111 | +        foreach ($array as $field => $val) { | |
| 112 | + $buffer = $this->replaceValue($buffer, $val, $key.'.'.$field.'.', $field); | |
| 113 | 113 | |
| 114 | -            while(strstr($buffer,'{{ $'.$prefix.$key.'.'.$field.' }}')){ | |
| 115 | -                $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val) ,$buffer); | |
| 114 | +            while (strstr($buffer, '{{ $'.$prefix.$key.'.'.$field.' }}')) { | |
| 115 | +                $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val), $buffer); | |
| 116 | 116 | } | 
| 117 | 117 | } | 
| 118 | 118 | return $buffer; | 
| @@ -120,13 +120,13 @@ discard block | ||
| 120 | 120 | |
| 121 | 121 | protected function removeComments(string $buffer): string | 
| 122 | 122 |      { | 
| 123 | -        while(strstr($buffer,'<!--')){ | |
| 123 | +        while (strstr($buffer, '<!--')) { | |
| 124 | 124 | $comment = substr( | 
| 125 | 125 | $buffer, | 
| 126 | - strpos($buffer,'<!--'), | |
| 127 | - strpos(strstr($buffer,'<!--'),'-->')+3 | |
| 126 | + strpos($buffer, '<!--'), | |
| 127 | + strpos(strstr($buffer, '<!--'), '-->')+3 | |
| 128 | 128 | ); | 
| 129 | - $buffer = str_replace($comment,'',$buffer); | |
| 129 | + $buffer = str_replace($comment, '', $buffer); | |
| 130 | 130 | } | 
| 131 | 131 | return $buffer; | 
| 132 | 132 | } | 
| @@ -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,12 +41,12 @@ discard block | ||
| 41 | 41 | |
| 42 | 42 | protected function replaceEspecialObject(string $buffer, object $obj, string $prefix, string $key): string | 
| 43 | 43 |      { | 
| 44 | -        foreach(get_object_vars($obj) as $field => $val){ | |
| 44 | +        foreach (get_object_vars($obj) as $field => $val) { | |
| 45 | 45 | |
| 46 | - $buffer = $this->replaceEspecialValue($buffer, $val, $key.'.'.$field.'.' , $field); | |
| 46 | + $buffer = $this->replaceEspecialValue($buffer, $val, $key.'.'.$field.'.', $field); | |
| 47 | 47 | |
| 48 | -            while(strstr($buffer,'{{!! $'.$prefix.$key.'.'.$field.' !!}}')){ | |
| 49 | -                $buffer = str_replace('{{!! $'.$prefix.$key.'.'.$field.' !!}}', $val ,$buffer); | |
| 48 | +            while (strstr($buffer, '{{!! $'.$prefix.$key.'.'.$field.' !!}}')) { | |
| 49 | +                $buffer = str_replace('{{!! $'.$prefix.$key.'.'.$field.' !!}}', $val, $buffer); | |
| 50 | 50 | } | 
| 51 | 51 | } | 
| 52 | 52 | return $buffer; | 
| @@ -54,11 +54,11 @@ discard block | ||
| 54 | 54 | |
| 55 | 55 | protected function replaceEspecialArray(string $buffer, array $array, ?string $prefix = '', ?string $key = ''): string | 
| 56 | 56 |      { | 
| 57 | -        foreach($array as $field => $val){ | |
| 58 | - $buffer = $this->replaceEspecialValue($buffer, $val, $key.'.'.$field.'.' , $field); | |
| 57 | +        foreach ($array as $field => $val) { | |
| 58 | + $buffer = $this->replaceEspecialValue($buffer, $val, $key.'.'.$field.'.', $field); | |
| 59 | 59 | |
| 60 | -            while(strstr($buffer,'{{!! $'.$prefix.$key.'.'.$field.' !!}}')){ | |
| 61 | -                $buffer = str_replace('{{!! $'.$prefix.$key.'.'.$field.' !!}}', $val ,$buffer); | |
| 60 | +            while (strstr($buffer, '{{!! $'.$prefix.$key.'.'.$field.' !!}}')) { | |
| 61 | +                $buffer = str_replace('{{!! $'.$prefix.$key.'.'.$field.' !!}}', $val, $buffer); | |
| 62 | 62 | } | 
| 63 | 63 | } | 
| 64 | 64 | return $buffer; |