Issues (994)

src/HTML/element.php (7 issues)

1
<?php
2
3
namespace HTML;
4
5
use DOMDocument;
6
use DOMElement;
7
use MVC\Exception;
8
use MVC\helper;
9
10
class element
11
{
12
  public $dom;
13
  public $version;
14
  public $encoding;
15
  public static $static;
16
  /**
17
   * DOMElement.
18
   *
19
   * @var DOMElement
20
   */
21
  public $element;
22
23
  public function __construct($version = '1.0', $encoding = 'utf-8')
24
  {
25
    $this->version = $version;
26
    $this->encoding = $encoding;
27
    $this->dom = new DOMDocument($version, $encoding);
28
    self::$static = new DOMDocument($version, $encoding);
29
  }
30
31
  /**
32
   * Array to element
33
   *
34
   * @return \HTML\array2element
35
   */
36
  public function array2el()
37
  {
38
    return new array2element();
39
  }
40
41
  public function pre($content, $options = ['echo' => true])
42
  {
43
    if ($this->isArrObj($content)) {
44
      $content = json_encode($content, false, true);
0 ignored issues
show
true of type true is incompatible with the type integer expected by parameter $depth of json_encode(). ( Ignorable by Annotation )

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

44
      $content = json_encode($content, false, /** @scrutinizer ignore-type */ true);
Loading history...
false of type false is incompatible with the type integer expected by parameter $flags of json_encode(). ( Ignorable by Annotation )

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

44
      $content = json_encode($content, /** @scrutinizer ignore-type */ false, true);
Loading history...
45
    }
46
    $element = "<pre>$content</pre>";
0 ignored issues
show
The assignment to $element is dead and can be removed.
Loading history...
47
    if ($options['echo']) {
48
      echo $content;
49
    } else {
50
      return $content;
51
    }
52
  }
53
54
  /**
55
   * var_dump inside pretext.
56
   */
57
  public function htmlvar_dump($content)
58
  {
59
    echo '<pre>';
60
    var_dump($content);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($content) looks like debug code. Are you sure you do not want to remove it?
Loading history...
61
    echo '</pre>';
62
  }
63
64
  public function script(array $source = [], $html = false, $print = false)
65
  {
66
    foreach ($source as $path) {
67
      $src_ = $this->get_local_asset($path);
68
      if ($html) {
69
        if ($src_) {
70
          $html = '<script src="' . $src_ . '"></script>';
71
          if ($print) {
72
            echo $html;
73
          } else {
74
            return $html;
75
          }
76
        } else {
77
          echo "<!-- $path not found -->";
78
        }
79
      } else {
80
        if ($src_) {
81
          helper::include_asset($src_);
82
        }
83
      }
84
    }
85
  }
86
87
  public function direct(string $element, string $content, array $attr = [])
88
  {
89
    $create = $this->dom->createElement($element, $content);
90
    if (!empty($attributes)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $attributes seems to never exist and therefore empty should always be true.
Loading history...
91
      $create = $this->fill_attributes($create, $attr);
92
    }
93
    var_dump($create);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($create) looks like debug code. Are you sure you do not want to remove it?
Loading history...
94
    $this->dom->appendChild($create);
95
96
    return $this->dom->saveHTML();
97
  }
98
99
  public function css(array $source)
100
  {
101
    $result = '';
102
    $config = defined('CONFIG') && isset(CONFIG['cache']['key']) ? '?cache=' . CONFIG['cache']['key'] : '';
103
    foreach ($source as $path) {
104
      $path .= $config;
105
      $result .= '<link rel="stylesheet" href="' . $path . '">';
106
    }
107
108
    return $result;
109
  }
110
111
  public function js(array $source)
112
  {
113
    $result = '';
114
    $config = defined('CONFIG') && isset(CONFIG['cache']['key']) ? '?cache=' . CONFIG['cache']['key'] : '';
115
    foreach ($source as $path) {
116
      if (!empty($path)) {
117
        $path .= $config;
118
        $result .= '<script src="' . $path . '"></script>';
119
      }
120
    }
121
122
    return $result;
123
  }
124
125
  /**
126
   * Proxy CSS.
127
   *
128
   * @param bool   $html
129
   * @param bool   $print
130
   * @param string $rel
131
   */
132
  public function link(array $source = [], $html = false, $print = false, $rel = 'stylesheet')
133
  {
134
    foreach ($source as $path) {
135
      $src_ = $this->get_local_asset($path);
136
      if ($html) {
137
        if ($src_) {
138
          $html = '<link href="' . $src_ . '" rel="' . $rel . '" />';
139
          if ($print) {
140
            echo $html;
141
          } else {
142
            return $html;
143
          }
144
        } else {
145
          echo "<!-- $path not found -->";
146
        }
147
      } else {
148
        if ($src_) {
149
          helper::include_asset($src_);
150
        }
151
      }
152
    }
153
  }
154
155
  public function get_local_asset($path)
156
  {
157
    if (is_string($path) && filter_var($path, FILTER_VALIDATE_URL)) {
158
      $src_ = $path;
159
    } else {
160
      if (is_string($path)) {
161
        $src_ = helper::asset_find([$path]);
162
      } elseif (is_array($path)) {
163
        $src_ = helper::asset_find($path);
164
      } else {
165
        throw new Exception('path required string or array, instead of ' . gettype($path), 1);
166
      }
167
      if ($src_) {
168
        $src_ = helper::webkit_asset($src_);
169
      }
170
    }
171
172
    return $src_;
173
  }
174
175
  /**
176
   * echo print_r in pretext.
177
   *
178
   * @param mixed $str
179
   */
180
  public function printr(...$str)
181
  {
182
    echo '<pre>';
183
    foreach ($str as $string) {
184
      print_r($string);
185
    }
186
    echo '</pre>';
187
  }
188
189
  public function select($attributes = [])
190
  {
191
    $select = $this->dom->createElement('select');
192
    foreach ($attributes as $key => $value) {
193
      if ('option' == $key) {
194
        continue;
195
      }
196
      $select->setAttribute($key, (string) $value);
197
    }
198
    if (isset($attributes['option'])) {
199
      foreach ($attributes['option'] as $option_attr) {
200
        $option = $this->single_create('option', $option_attr['inner'], $option_attr);
201
        $select->appendChild($option);
202
      }
203
    }
204
    $this->dom->appendChild($select);
205
206
    return $this;
207
  }
208
209
  public function single_create(string $element, $content = '', array $attributes = [])
210
  {
211
    if (empty($attributes) && is_array($content)) {
212
      $attributes = $content;
213
      $content = '';
214
    }
215
    $element = $this->dom->createElement($element, $content);
216
    if (!empty($attributes)) {
217
      $element = $this->fill_attributes($element, $attributes);
218
    }
219
220
    return $element;
221
  }
222
223
  public function create(string $element, $content = '', array $attributes = [], array $options = ['formatOutput' => false])
0 ignored issues
show
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

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

223
  public function create(string $element, $content = '', array $attributes = [], /** @scrutinizer ignore-unused */ array $options = ['formatOutput' => false])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
224
  {
225
    if ('select' == $element) {
226
      return $this->select($attributes);
227
    }
228
    if (empty($attributes) && is_array($content)) {
229
      $attributes = $content;
230
      $content = '';
231
    }
232
    //exit(var_dump($attributes, $content));
233
    $element = $this->dom->createElement($element, $content);
234
    if (!empty($attributes)) {
235
      $element = $this->fill_attributes($element, $attributes);
236
    }
237
    $this->dom->appendChild($element);
238
239
    return $this;
240
  }
241
242
  public function fill_attributes(DOMElement $element, array $attributes)
243
  {
244
    foreach ($attributes as $key => $value) {
245
      $element->setAttribute($key, (string) $value);
246
    }
247
248
    return $element;
249
  }
250
251
  public function outerText()
252
  {
253
    $save = $this->dom->saveHTML();
254
    $this->dom = new DOMDocument($this->version, $this->encoding);
255
256
    return $save;
257
  }
258
259
  public function isArrObj($str)
260
  {
261
    return is_array($str) || is_object($str);
262
  }
263
}
264