Passed
Push — master ( a265c4...b831fd )
by Henri
01:16
created

HelperTrait::replaceArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 4
dl 0
loc 10
rs 10
1
<?php
2
3
namespace HnrAzevedo\Viewer;
4
5
use Exception;
6
7
trait HelperTrait{
8
    use CheckTrait, JScriptTrait;
9
10
    public array $data = [];
11
12
    protected function getOB(string $require, array $data = []): string
13
    {
14
        foreach($data as $variable => $_){
15
            $$variable = $_;
16
        }
17
        
18
        $_ = (array_key_exists('_',$data)) ? $data['_'] : null;
19
20
        if(!file_exists($require)){
21
            $require = basename($require);
22
            throw new Exception("Importation file does not exist: {$require} .");
23
        }
24
25
        $this->data = array_merge($this->data,$data);
26
27
        ob_start();
28
        require($require);
29
        $response = ob_get_contents();
30
        ob_end_clean();
31
       
32
        return $this->treatHTML($response);
33
    }
34
35
    private function treatHTML(string $html): string
36
    {
37
        $arrayHtml = explode(PHP_EOL,$html);
38
        $html = [];
39
40
        $inScript = false;
41
        $inComment = false;
42
            
43
        foreach($arrayHtml as $index => $value){
44
            $inScript = $this->checkInScript($inScript, $value);
45
            
46
            if($inScript){
47
                $inComment = $this->checkCommentInScript($inComment, $value);
48
49
                if(!$this->checkScriptNeed($inComment, $value)){
50
                    continue;
51
                }else{
52
                    $value = $this->treatScript($value);
53
                }
54
            }
55
                
56
            $html[$index] = ltrim($value);
57
        }
58
        
59
        return implode('',$html);
60
    }
61
62
    protected function getVars(string $buffer): string
63
    {
64
        return $this->replaceVars($buffer, $this->data);
65
    }
66
67
    protected function replaceVars(string $buffer, array $vars, ?string $prefix = ''): string
68
    {
69
        foreach ($vars as $key => $value) {
70
            switch(gettype($value)){
71
                case 'array':
72
                    $buffer = $this->replaceArray($buffer, $value, $prefix, $key);
73
                    break;
74
                case 'object':
75
                    $buffer = $this->replaceObject($buffer, $value, $prefix, $key);
0 ignored issues
show
Bug introduced by
It seems like $prefix can also be of type null; however, parameter $prefix of HnrAzevedo\Viewer\HelperTrait::replaceObject() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
                    $buffer = $this->replaceObject($buffer, $value, /** @scrutinizer ignore-type */ $prefix, $key);
Loading history...
76
                    break;
77
                default:
78
                    $buffer = $this->replaceValue($buffer, $value, $prefix, $key);
79
                    break;
80
            }
81
        }
82
83
        return $buffer;
84
    }
85
86
    protected function replaceValue(string $buffer, $value, ?string $prefix, string $key): string
87
    {
88
        if(gettype($value)!=='array' && gettype($value)!=='object'){
89
            while(strstr($buffer,'{{ $'.$prefix.$key.' }}')){
90
                $buffer = str_replace('{{ $'.$prefix.$key.' }}', htmlspecialchars($value) ,$buffer);
91
            }
92
        }
93
        return $buffer;
94
    }
95
96
    protected function replaceObject(string $buffer, object $obj, string $prefix, string $key): string
97
    {
98
        foreach(get_object_vars($obj) as $field => $val){
99
            
100
            $buffer = $this->replaceValue($buffer, $val, $key.'.'.$field.'.' , $field);
101
102
            while(strstr($buffer,'{{ $'.$prefix.$key.'.'.$field.' }}')){
103
                $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val) ,$buffer);
104
            }
105
        }
106
        return $buffer;
107
    }
108
109
    protected function replaceArray(string $buffer, array $array, ?string $prefix = '', ?string $key = ''): string
110
    {
111
        foreach($array as $field => $val){
112
            $buffer = $this->replaceValue($buffer, $val, $key.'.'.$field.'.' , $field);
113
114
            while(strstr($buffer,'{{ $'.$prefix.$key.'.'.$field.' }}')){
115
                $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val) ,$buffer);
116
            }
117
        }
118
        return $buffer;
119
    }
120
121
    protected function removeComments(string $buffer): string
122
    {
123
        while(strstr($buffer,'<!--')){
124
            $comment = substr(
125
                $buffer,
126
                strpos($buffer,'<!--'),
127
                strpos(strstr($buffer,'<!--'),'-->')+3
128
            );
129
            $buffer = str_replace($comment,'',$buffer);
130
        }
131
        return $buffer;
132
    }
133
134
}