Completed
Push — master ( 59cd4c...8588a0 )
by Dimas
10:00
created

element::script()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 6
nop 3
dl 0
loc 18
rs 9.2222
c 0
b 0
f 0
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
Bug introduced by
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...
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $options 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
Unused Code introduced by
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
      $path .= $config;
117
      $result .= '<script src="' . $path . '"></script>';
118
    }
119
120
    return $result;
121
  }
122
123
  /**
124
   * Proxy CSS.
125
   *
126
   * @param bool   $html
127
   * @param bool   $print
128
   * @param string $rel
129
   */
130
  public function link(array $source = [], $html = false, $print = false, $rel = 'stylesheet')
131
  {
132
    foreach ($source as $path) {
133
      $src_ = $this->get_local_asset($path);
134
      if ($html) {
135
        if ($src_) {
136
          $html = '<link href="' . $src_ . '" rel="' . $rel . '" />';
137
          if ($print) {
138
            echo $html;
139
          } else {
140
            return $html;
141
          }
142
        } else {
143
          echo "<!-- $path not found -->";
144
        }
145
      } else {
146
        if ($src_) {
147
          helper::include_asset($src_);
148
        }
149
      }
150
    }
151
  }
152
153
  public function get_local_asset($path)
154
  {
155
    if (is_string($path) && filter_var($path, FILTER_VALIDATE_URL)) {
156
      $src_ = $path;
157
    } else {
158
      if (is_string($path)) {
159
        $src_ = helper::asset_find([$path]);
160
      } elseif (is_array($path)) {
161
        $src_ = helper::asset_find($path);
162
      } else {
163
        throw new Exception('path required string or array, instead of ' . gettype($path), 1);
164
      }
165
      if ($src_) {
166
        $src_ = helper::webkit_asset($src_);
167
      }
168
    }
169
170
    return $src_;
171
  }
172
173
  /**
174
   * echo print_r in pretext.
175
   *
176
   * @param mixed $str
177
   */
178
  public function printr(...$str)
179
  {
180
    echo '<pre>';
181
    foreach ($str as $string) {
182
      print_r($string);
183
    }
184
    echo '</pre>';
185
  }
186
187
  public function select($attributes = [])
188
  {
189
    $select = $this->dom->createElement('select');
190
    foreach ($attributes as $key => $value) {
191
      if ('option' == $key) {
192
        continue;
193
      }
194
      $select->setAttribute($key, (string) $value);
195
    }
196
    if (isset($attributes['option'])) {
197
      foreach ($attributes['option'] as $option_attr) {
198
        $option = $this->single_create('option', $option_attr['inner'], $option_attr);
199
        $select->appendChild($option);
200
      }
201
    }
202
    $this->dom->appendChild($select);
203
204
    return $this;
205
  }
206
207
  public function single_create(string $element, $content = '', array $attributes = [])
208
  {
209
    if (empty($attributes) && is_array($content)) {
210
      $attributes = $content;
211
      $content = '';
212
    }
213
    $element = $this->dom->createElement($element, $content);
214
    if (!empty($attributes)) {
215
      $element = $this->fill_attributes($element, $attributes);
216
    }
217
218
    return $element;
219
  }
220
221
  public function create(string $element, $content = '', array $attributes = [], array $options = ['formatOutput' => false])
0 ignored issues
show
Unused Code introduced by
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

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