1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* The MIT License (MIT) |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
namespace danielgp\common_lib; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* DOM component functions |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait DomBasicComponentsByDanielGP |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
private function buildAttributesForTag($features) |
40
|
|
|
{ |
41
|
|
|
if (!is_array($features)) { |
42
|
|
|
return ''; |
43
|
|
|
} |
44
|
|
|
$attributes = ''; |
45
|
|
|
foreach ($features as $key => $value) { |
46
|
|
|
$val = $this->buildAttributesForTagValueArray($value); |
47
|
|
|
$attributes .= ' ' . $key . '="' . $val . '"'; |
48
|
|
|
} |
49
|
|
|
return $attributes; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function buildAttributesForTagValueArray($value) |
53
|
|
|
{ |
54
|
|
|
$val = $value; |
55
|
|
|
if (is_array($value)) { |
56
|
|
|
$valA = []; |
57
|
|
|
foreach ($value as $key2 => $value2) { |
58
|
|
|
$valA[] = $key2 . ':' . $value2; |
59
|
|
|
} |
60
|
|
|
$val = implode(';', $valA) . ';'; |
61
|
|
|
} |
62
|
|
|
return $val; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Capatalize first letter of each word |
67
|
|
|
* AND filters only letters and numbers |
68
|
|
|
* |
69
|
|
|
* @param string $givenString |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
protected function cleanStringForId($givenString) |
73
|
|
|
{ |
74
|
|
|
return preg_replace("/[^a-zA-Z0-9]/", '', ucwords($givenString)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Cleans a string for certain internal rules |
79
|
|
|
* |
80
|
|
|
* @param type $urlString |
81
|
|
|
* @return type |
82
|
|
|
*/ |
83
|
|
|
protected function setCleanUrl($urlString) |
84
|
|
|
{ |
85
|
|
|
$arrayToReplace = [ |
86
|
|
|
'&' => '&', |
87
|
|
|
'&' => '&', |
88
|
|
|
'&amp;' => '&', |
89
|
|
|
' ' => '%20', |
90
|
|
|
]; |
91
|
|
|
$kys = array_keys($arrayToReplace); |
92
|
|
|
$vls = array_values($arrayToReplace); |
93
|
|
|
return str_replace($kys, $vls, filter_var($urlString, FILTER_SANITIZE_URL)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns a div tag that clear any float |
98
|
|
|
* |
99
|
|
|
* @param integer $height |
100
|
|
|
*/ |
101
|
|
|
protected function setClearBoth1px($height = 1) |
102
|
|
|
{ |
103
|
|
|
$divStyle = implode('', [ |
104
|
|
|
'height:' . $height . 'px;', |
105
|
|
|
'line-height:' . $height . 'px;', |
106
|
|
|
'float:none;', |
107
|
|
|
'clear:both;', |
108
|
|
|
'margin:0px;' |
109
|
|
|
]); |
110
|
|
|
return $this->setStringIntoTag(' ', 'div', ['style' => $divStyle]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Sets the no-cache header |
115
|
|
|
*/ |
116
|
|
|
protected function setHeaderNoCache($contentType = 'application/json') |
117
|
|
|
{ |
118
|
|
|
header("Content-Type: " . $contentType . "; charset=utf-8"); |
119
|
|
|
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
120
|
|
|
header("Cache-Control: no-store, no-cache, must-revalidate"); |
121
|
|
|
header("Cache-Control: post-check=0, pre-check=0", false); |
122
|
|
|
header("Pragma: no-cache"); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Puts a given string into a specific short tag |
127
|
|
|
* |
128
|
|
|
* @param string $sTag |
129
|
|
|
* @param array $features |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
protected function setStringIntoShortTag($sTag, $features = null) |
133
|
|
|
{ |
134
|
|
|
return '<' . $sTag . $this->buildAttributesForTag($features) |
135
|
|
|
. (isset($features['dont_close']) ? '' : '/') . '>'; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Puts a given string into a specific tag |
140
|
|
|
* |
141
|
|
|
* @param string $sString |
142
|
|
|
* @param string $sTag |
143
|
|
|
* @param array $features |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
protected function setStringIntoTag($sString, $sTag, $features = null) |
147
|
|
|
{ |
148
|
|
|
return '<' . $sTag . $this->buildAttributesForTag($features) . '>' . $sString . '</' . $sTag . '>'; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|