1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BootPress\Bootstrap; |
4
|
|
|
|
5
|
|
|
trait Base |
6
|
|
|
{ |
7
|
16 |
|
protected function prefixClasses($base, array $prefix, $classes, $exclude_base = false) |
8
|
|
|
{ |
9
|
16 |
|
if (!is_array($classes)) { |
10
|
13 |
|
$classes = explode(' ', $classes); |
11
|
13 |
|
} |
12
|
16 |
|
$classes = array_filter($classes); |
13
|
16 |
|
foreach ($classes as $key => $class) { |
14
|
16 |
|
if (in_array($class, $prefix)) { |
15
|
16 |
|
$classes[$key] = $base.'-'.$class; |
16
|
16 |
|
} |
17
|
16 |
|
} |
18
|
16 |
|
if ($exclude_base === false) { |
19
|
15 |
|
array_unshift($classes, $base); |
20
|
15 |
|
} |
21
|
|
|
|
22
|
16 |
|
return implode(' ', array_unique($classes)); |
23
|
|
|
} |
24
|
|
|
|
25
|
13 |
|
protected function addClass($html, array $tags) |
26
|
|
|
{ |
27
|
13 |
|
$rnr = array(); |
28
|
13 |
|
foreach ($tags as $tag => $class) { |
29
|
13 |
|
$prefix = (is_array($class) && isset($class[2]) && is_array($class[1])) ? $class : false; |
30
|
13 |
|
if ($prefix) { |
31
|
1 |
|
$class = $class[2]; |
32
|
1 |
|
} |
33
|
13 |
|
if (is_string($class) && !empty($class)) { |
34
|
13 |
|
$class = explode(' ', $class); |
35
|
13 |
|
preg_match_all('/(\<'.$tag.'([^\>]*)\>)/i', $html, $matches); |
36
|
13 |
|
foreach (array_unique($matches[0]) as $add) { |
37
|
12 |
|
if ($this->firstTagAttributes($add, $match)) { |
38
|
12 |
|
list($add, $tag, $attributes) = $match; |
39
|
12 |
|
$merge = (isset($attributes['class'])) ? array_merge(explode(' ', $attributes['class']), $class) : $class; |
40
|
12 |
|
if ($prefix) { |
41
|
1 |
|
$prefix[2] = $merge; |
42
|
1 |
|
$attributes['class'] = call_user_func_array(array($this, 'prefixClasses'), $prefix); |
43
|
1 |
|
} else { |
44
|
12 |
|
$attributes['class'] = implode(' ', array_unique(array_filter($merge))); |
45
|
|
|
} |
46
|
12 |
|
foreach ($attributes as $key => $value) { |
47
|
12 |
|
$attributes[$key] = $key.'="'.$value.'"'; |
48
|
12 |
|
} |
49
|
12 |
|
$rnr[$add] = '<'.$tag.' '.implode(' ', $attributes).'>'; |
50
|
12 |
|
} |
51
|
13 |
|
} |
52
|
13 |
|
} |
53
|
13 |
|
} |
54
|
|
|
|
55
|
13 |
|
return (!empty($rnr)) ? str_replace(array_keys($rnr), array_values($rnr), $html) : $html; |
56
|
|
|
} |
57
|
|
|
|
58
|
12 |
|
protected function firstTagAttributes($html, &$matches, $find = '<') |
59
|
|
|
{ |
60
|
12 |
|
if (false !== $begin = strpos($html, $find)) { |
61
|
12 |
|
if (false !== $end = strpos($html, '>', $begin)) { |
62
|
12 |
|
$first = substr($html, $begin, $end - $begin + 1); |
63
|
12 |
|
$tag = trim(substr($first, 1, -1)); |
64
|
12 |
|
$dom = new \DOMDocument(); |
65
|
12 |
|
@$dom->loadHTML('<'.rtrim($tag.'/').' />'); |
|
|
|
|
66
|
12 |
|
foreach ($dom->getElementsByTagName('body')->item(0)->childNodes as $node) { |
67
|
12 |
|
$attributes = array(); |
68
|
12 |
|
foreach ($node->attributes as $attr) { |
69
|
8 |
|
$attributes[$attr->nodeName] = $attr->nodeValue; |
70
|
12 |
|
} |
71
|
12 |
|
$matches = array($first, $node->nodeName, $attributes); |
72
|
12 |
|
unset($dom); |
73
|
12 |
|
break; |
74
|
12 |
|
} |
75
|
12 |
|
} |
76
|
12 |
|
} |
77
|
|
|
|
78
|
12 |
|
return isset($attributes) ? true : false; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: