forked from jarves/jarves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.php
173 lines (145 loc) · 4.5 KB
/
Tools.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
<?php
/**
* This file is part of Jarves.
*
* (c) Marc J. Schmidt <[email protected]>
*
* J.A.R.V.E.S - Just A Rather Very Easy [content management] System.
*
* http://jarves.io
*
* To get the full copyright and license information, please view the
* LICENSE file, that was distributed with this source code.
*/
namespace Jarves;
class Tools {
public static function underscore2Camelcase($value)
{
return static::char2Camelcase($value, '_');
}
public static function char2Camelcase($value, $char = '_')
{
$ex = explode($char, $value);
$return = '';
foreach ($ex as $str) {
$return .= ucfirst($str);
}
return $return;
}
public static function camelcase2Underscore($value)
{
return static::camelcase2Char($value, '_');
}
public static function camelcase2Char($value, $char = '_')
{
return strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1' . $char . '$2', $value));
}
public static function indentString($string, $size = 2, $char = ' ')
{
return preg_replace('/^/mu', str_repeat($char, $size), $string);
}
public static function getArrayTrace($exception)
{
$trace = [];
foreach ($exception->getTrace() as $t) {
$args = [];
foreach ((array)@$t['args'] as $arg) {
$args[] = gettype($arg);
}
$trace[] = [
'function' => @$t['function'],
'class' => @$t['class'],
'file' => @$t['file'],
'line' => @$t['line'],
'type' => @$t['type'],
'args' => $args,
];
}
return $trace;
}
/**
* @param string $string a comma separated list of values
* @return array
*/
public static function listToArray($string)
{
if (is_string($string)) {
$array = !$string ? [] : array_unique(
explode(',', trim(preg_replace('/[^a-zA-Z0-9_\.\-,\*]/', '', $string)))
);
} else if (is_array($string)) {
$array = $string;
} else {
return [];
}
return array_keys(array_flip($array));
}
/**
* @param array|string $array array or comma separated list
* @param array|string $blacklist array or comma separated list
* @return array
*/
public static function filterArrayByBlacklist($array, $blacklist)
{
$array = static::listToArray($array);
$blacklist = static::listToArray($blacklist);
$blacklistIndexed = array_flip($blacklist);
foreach ($array as $idx => $item) {
if (isset($blacklistIndexed[$item])) {
unset($array[$idx]);
}
}
return $array;
}
/**
* Returns a relative path from $path to $current.
*
* @param string $from
* @param string $to relative to this
*
* @return string relative path without trailing slash
*/
public static function getRelativePath($from, $to)
{
$from = '/' . trim($from, '/');
$to = '/' . trim($to, '/');
if (0 === $pos = strpos($from, $to)) {
return substr($from, strlen($to) + ('/' === $to ? 0 : 1));
}
$result = '';
while ($to && false === strpos($from, $to)) {
$result .= '../';
$to = substr($to, 0, strrpos($to, '/'));
}
return !$to /*we reached root*/ ? $result . substr($from, 1) : $result. substr($from, strlen($to) + 1);
}
public static function dbQuote($value, $table = '')
{
if (is_array($value)) {
foreach ($value as &$v) {
$v = static::dbQuote($v);
}
return $value;
}
if (strpos($value, ',') !== false) {
$values = explode(',', str_replace(' ', '', $value));
$values = static::dbQuote($values);
return implode(', ', $values);
}
if ($table && strpos($value, '.') === false) {
return static::dbQuote($table) . '.' . static::dbQuote($value);
}
return preg_replace('/[^a-zA-Z0-9-_]/', '', $value);;
}
public static function urlEncode($string)
{
$string = rawurlencode($string);
$string = str_replace('%2F', '%252F', $string);
return $string;
}
public static function urlDecode($string)
{
$string = str_replace('%252F', '%2F', $string);
return rawurldecode($string);
}
}