-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttribute.php
362 lines (313 loc) · 11.8 KB
/
Attribute.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
namespace Onimla\HTML;
/**
* An HTML attribute.
*
* @author AlminoMelo at gmail.com
*/
class Attribute {
protected $name;
protected $value;
protected $output;
static $log = FALSE;
public function __construct($name = FALSE, $value = FALSE) {
/*
# Log actions on a development enviroment
self::$log = (ENVIRONMENT !== 'production');
*/
if ($name !== FALSE) {
$this->setName($name);
}
if ($value !== FALSE) {
$this->setValue($value);
}
}
public function __toString() {
$value = $this->getValue(TRUE);
if (strlen($value) < 1) {
return $this->getName();
}
return "{$this->getName()}=\"{$value}\"";
}
public function __clone() {
$this->value = is_object($this->value) ? clone $this->value : $this->value;
}
public function selector() {
return "[{$this}]";
}
public static function name($name) {
return self::removeSpecialCharacters(self::convertAccentedCharacters($name));
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = self::name($name);
}
public function matchName($regexOrstring) {
if (@preg_match($regexOrstring, null) === false) {
$regexOrstring = "/" . preg_quote($regexOrstring) . "/";
}
return preg_match($regexOrstring, $this->getName());
}
public static function output($name, $value, $output) {
if ($output === FALSE) {
self::log('Simple output values as it is');
# Por padrão, retorna o valor assim como ele é
return $value;
} elseif (is_callable($output)) {
self::log('Output is callable.');
# Caso seja enviada uma função que pode ser chamada
return call_user_func($output, $value);
} else {
self::log('Output is a string.');
# Se a saída for uma das strings previstas
switch ($output) {
case 'safe':
$value = self::removeSpecialCharacters(
self::convertAccentedCharacters(strtolower($value)));
break;
case 'html':
case 'decode':
$value = html_entity_decode($value);
break;
case 'encode':
$value = htmlentities($value);
break;
case 'int':
case 'numeric':
case 'natural':
$value = intval($value);
default:
$value = ($value === TRUE) ? $name : (string) $value;
}
return $value;
}
}
/**
*
* @param string|callable $output Possible values are safe, html, decode, encode, int, numeric, natural
* @return mixed
*/
public function getValue($output = TRUE) {
if ($this->value instanceof self) {
return $this->value->getValue($output);
}
if ($output === TRUE AND $this->getOutput() !== FALSE) {
return self::output($this->getName(), $this->getValue(FALSE), $this->getOutput());
}
if ($output !== FALSE AND $output !== NULL AND is_callable($output)) {
return self::output($this->getName(), $this->value, $output);
}
# Por padrão, retorna o valor assim como ele é
return $this->value;
}
public function setValue($value) {
self::log("new value for `{$this->getName()}` is " . var_export($value, TRUE), TRUE);
$this->value = $value;
}
public function addValue($value) {
$this->value .= $value;
}
/**
*
* @param string $regexOrString Will be converted to an valid regular expression
* @return boolean
*/
public function matchValue($regexOrString, &$matches = null) {
if (@preg_match($regexOrString, NULL) === FALSE) {
$regexOrString = "/" . preg_quote($regexOrString) . "/";
}
return preg_match($regexOrString, $this->getValue(TRUE), $matches);
}
public function value($value = FALSE) {
if ($value === FALSE) {
return $this->getValue();
}
$this->setValue($value);
return $this;
}
public function val($value = FALSE) {
return $this->val($value);
}
/**
* Return if the value is set.
* @return boolean
*/
public function isValueSet() {
return strlen($this->getValue()) > 0;
}
public function getOutput() {
return $this->output;
}
/**
* You can set anything callable
* @param string|callable $output Possible values are safe, html, decode, encode, int, numeric, natural
*/
public function setOutput($output) {
$this->output = $output;
}
public static function removeSpecialCharacters($string) {
$strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
/* http://www.w3.org/TR/2008/REC-xml-20081126/#NT-Name */
return preg_replace('/[^:A-Z_a-z\-\.^\d^\[^\]]/u', '', $strtolower($string));
}
/**
* Convert Special Chars
*
* Found Via:
* http://us.php.net/manual/en/function.chr.php#72145
*
* @access public
* @param string the string
* @return string
*/
public static function convertAccentedCharacters($str) {
if (function_exists('convert_accented_characters')) {
return convert_accented_characters($str);
}
$foreignCharacters = array(
'/ä|æ|ǽ/' => 'ae',
'/ö|œ/' => 'oe',
'/ü/' => 'ue',
'/Ä/' => 'Ae',
'/Ü/' => 'Ue',
'/Ö/' => 'Oe',
'/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ|Α|Ά|Ả|Ạ|Ầ|Ẫ|Ẩ|Ậ|Ằ|Ắ|Ẵ|Ẳ|Ặ|А/' => 'A',
'/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª|α|ά|ả|ạ|ầ|ấ|ẫ|ẩ|ậ|ằ|ắ|ẵ|ẳ|ặ|а/' => 'a',
'/Б/' => 'B',
'/б/' => 'b',
'/Ç|Ć|Ĉ|Ċ|Č/' => 'C',
'/ç|ć|ĉ|ċ|č/' => 'c',
'/Д/' => 'D',
'/д/' => 'd',
'/Ð|Ď|Đ|Δ/' => 'Dj',
'/ð|ď|đ|δ/' => 'dj',
'/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě|Ε|Έ|Ẽ|Ẻ|Ẹ|Ề|Ế|Ễ|Ể|Ệ|Е|Э/' => 'E',
'/è|é|ê|ë|ē|ĕ|ė|ę|ě|έ|ε|ẽ|ẻ|ẹ|ề|ế|ễ|ể|ệ|е|э/' => 'e',
'/Ф/' => 'F',
'/ф/' => 'f',
'/Ĝ|Ğ|Ġ|Ģ|Γ|Г|Ґ/' => 'G',
'/ĝ|ğ|ġ|ģ|γ|г|ґ/' => 'g',
'/Ĥ|Ħ/' => 'H',
'/ĥ|ħ/' => 'h',
'/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ|Η|Ή|Ί|Ι|Ϊ|Ỉ|Ị|И|Ы/' => 'I',
'/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı|η|ή|ί|ι|ϊ|ỉ|ị|и|ы|ї/' => 'i',
'/Ĵ/' => 'J',
'/ĵ/' => 'j',
'/Ķ|Κ|К/' => 'K',
'/ķ|κ|к/' => 'k',
'/Ĺ|Ļ|Ľ|Ŀ|Ł|Λ|Л/' => 'L',
'/ĺ|ļ|ľ|ŀ|ł|λ|л/' => 'l',
'/М/' => 'M',
'/м/' => 'm',
'/Ñ|Ń|Ņ|Ň|Ν|Н/' => 'N',
'/ñ|ń|ņ|ň|ʼn|ν|н/' => 'n',
'/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ|Ο|Ό|Ω|Ώ|Ỏ|Ọ|Ồ|Ố|Ỗ|Ổ|Ộ|Ờ|Ớ|Ỡ|Ở|Ợ|О/' => 'O',
'/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º|ο|ό|ω|ώ|ỏ|ọ|ồ|ố|ỗ|ổ|ộ|ờ|ớ|ỡ|ở|ợ|о/' => 'o',
'/П/' => 'P',
'/п/' => 'p',
'/Ŕ|Ŗ|Ř|Ρ|Р/' => 'R',
'/ŕ|ŗ|ř|ρ|р/' => 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=> 'yu',
'/Я/' => 'Ya',
'/я/' => 'ya'
);
$from = array_keys($foreignCharacters);
$to = array_values($foreignCharacters);
return preg_replace($from, $to, $str);
}
public static function log($message, $showCaller = FALSE, $debugBacktrace = FALSE) {
if (self::$log) {
$dir = __DIR__ . DIRECTORY_SEPARATOR . 'logs';
$class = str_replace('\\', '-', __CLASS__);
$date = date('Y-m-d');
$timestamp = $date . date(' H:i:s -- ');
$caller = NULL;
$destination = $dir . DIRECTORY_SEPARATOR . "{$date}_{$class}.log";
$message = implode(PHP_EOL . str_repeat(' ', strlen($timestamp)), explode(PHP_EOL, $message));
if ($showCaller) {
$trace = ($debugBacktrace === FALSE) ? debug_backtrace() : $debugBacktrace;
$trace = (object) $trace[1];
$caller = (property_exists($trace, 'class') ? $trace->class . $trace->type : NULL)
. $trace->function . ' »»» ';
#var_dump($trace->file);
#var_dump(str_replace('\\', '/', $trace->class));
#var_dump(strstr($trace->file, str_replace('\\', '/', $trace->class)));
# Log file name if it differs from class name
if (property_exists($trace, 'file') AND strstr($trace->file, str_replace('\\', DIRECTORY_SEPARATOR, $trace->class)) === FALSE) {
# Try to remove unecessary stuff from path string
$filename = trim(substr($trace->file, strlen(__DIR__)), DIRECTORY_SEPARATOR) . ':' . $trace->line . PHP_EOL . str_repeat(' ', strlen($timestamp));
#var_dump($filename);
#var_dump($caller);
#var_dump($message);
$message = implode(PHP_EOL . str_repeat(' ', strlen($caller) - 3), explode(PHP_EOL, $message));
$caller = $filename . $caller;
}
#var_dump($caller . $message);
}
/*
echo '<pre>';
echo $timestamp;
echo $caller;
echo $message;
echo '</pre>';
die();
*/
# Creates a file, if it does not exists
# Creates a file, if it does not exists
if (!file_exists($destination)) {
mkdir($dir, 0755, TRUE);
}
return error_log($timestamp . $caller . trim($message) . PHP_EOL, 3, $destination);
#return file_put_contents($filename, $message, FILE_APPEND);
} else {
return TRUE;
}
}
}