|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
You may not change or alter any portion of this comment or credits |
|
4
|
|
|
of supporting developers from this source code or any supporting source code |
|
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
|
6
|
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
|
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Xoops; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Utils |
|
16
|
|
|
* |
|
17
|
|
|
* @category Xoops\Utils |
|
18
|
|
|
* @package Xoops |
|
19
|
|
|
* @author trabis <[email protected]> |
|
20
|
|
|
* @copyright 2011-2015 XOOPS Project (http://xoops.org) |
|
21
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
|
22
|
|
|
* @link http://xoops.org |
|
23
|
|
|
*/ |
|
24
|
|
|
class Utils |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Output a dump of a variable |
|
28
|
|
|
* |
|
29
|
|
|
* @param mixed $var variable to dump |
|
30
|
|
|
* @param bool $echo true to echo dump, false to return dump as string |
|
31
|
|
|
* |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function dumpVar($var, $echo = true) |
|
35
|
|
|
{ |
|
36
|
|
|
$myts = \Xoops\Core\Text\Sanitizer::getInstance(); |
|
37
|
|
|
$msg = $myts->displayTarea(var_export($var, true)); |
|
38
|
|
|
$msg = "<div style='padding: 5px; font-weight: bold'>{$msg}</div>"; |
|
39
|
|
|
if ($echo) { |
|
40
|
|
|
echo $msg; |
|
41
|
|
|
} |
|
42
|
|
|
return $msg; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Output a dump of a file |
|
47
|
|
|
* |
|
48
|
|
|
* @param mixed $file file to dump |
|
49
|
|
|
* @param bool $echo true to echo dump, false to return dump as string |
|
50
|
|
|
* |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function dumpFile($file, $echo = true) |
|
54
|
|
|
{ |
|
55
|
|
|
$msg = highlight_file($file, true); |
|
56
|
|
|
$msg = "<div style='padding: 5px; font-weight: bold'>{$msg}</div>"; |
|
57
|
|
|
if ($echo) { |
|
58
|
|
|
echo $msg; |
|
59
|
|
|
} |
|
60
|
|
|
return $msg; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Support for recursive array_diff |
|
65
|
|
|
* |
|
66
|
|
|
* Compares first array against the second and returns the difference - that is |
|
67
|
|
|
* the values in the first, but not in the second array |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $aArray1 first array |
|
70
|
|
|
* @param mixed $aArray2 second array |
|
71
|
|
|
* |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function arrayRecursiveDiff(array $aArray1, array $aArray2) |
|
75
|
|
|
{ |
|
76
|
|
|
$aReturn = array(); |
|
77
|
|
|
|
|
78
|
|
|
foreach ($aArray1 as $mKey => $mValue) { |
|
79
|
|
|
if (array_key_exists($mKey, $aArray2)) { |
|
80
|
|
|
if (is_array($mValue) && is_array($aArray2[$mKey])) { |
|
81
|
|
|
$aRecursiveDiff = self::arrayRecursiveDiff($mValue, $aArray2[$mKey]); |
|
82
|
|
|
if (count($aRecursiveDiff)) { |
|
83
|
|
|
$aReturn[$mKey] = $aRecursiveDiff; |
|
84
|
|
|
} |
|
85
|
|
|
} else { |
|
86
|
|
|
if ($mValue != $aArray2[$mKey]) { |
|
87
|
|
|
$aReturn[$mKey] = $mValue; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} else { |
|
91
|
|
|
$aReturn[$mKey] = $mValue; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
return $aReturn; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* This function can be thought of as a hybrid between PHP's `array_merge` and `array_merge_recursive`. |
|
99
|
|
|
* The difference between this method and the built-in ones, is that if an array key contains another array, then |
|
100
|
|
|
* Utils::arrayRecursiveMerge() will behave in a recursive fashion (unlike `array_merge`). But it |
|
101
|
|
|
* will not act recursively for keys that contain scalar values (unlike `array_merge_recursive`). |
|
102
|
|
|
* |
|
103
|
|
|
* Note: This function will work with an unlimited amount of arguments and typecasts non-array parameters |
|
104
|
|
|
* into arrays. |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $data Array to be merged |
|
107
|
|
|
* @param mixed $merge Array to merge with. The argument and all trailing arguments will be array cast when merged |
|
108
|
|
|
* |
|
109
|
|
|
* @return array Merged array |
|
110
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::merge |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function arrayRecursiveMerge(array $data, $merge) |
|
113
|
|
|
{ |
|
114
|
|
|
$args = func_get_args(); |
|
115
|
|
|
$return = current($args); |
|
116
|
|
|
|
|
117
|
|
|
while (($arg = next($args)) !== false) { |
|
118
|
|
|
foreach ((array)$arg as $key => $val) { |
|
119
|
|
|
if (!empty($return[$key]) && is_array($return[$key]) && is_array($val)) { |
|
120
|
|
|
$return[$key] = self::arrayRecursiveMerge($return[$key], $val); |
|
121
|
|
|
} elseif (is_int($key)) { |
|
122
|
|
|
if (!in_array($val, $return)) { |
|
123
|
|
|
$return[] = $val; |
|
124
|
|
|
} // merge only once $val |
|
125
|
|
|
} else { |
|
126
|
|
|
$return[$key] = $val; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
return $return; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|