-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
123 lines (115 loc) · 2.72 KB
/
functions.php
File metadata and controls
123 lines (115 loc) · 2.72 KB
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
<?php
/**
* Indexing to array, and fallback if no key exists
*
* @param array $x
* @param scalar $i
* @param mixed $default
* @return mixed item of $x indexed by $i if exists, $default otherwise
*/
function I(&$x,$i,$default=null) {
return isset($x[$i]) ? $x[$i] : $default;
}
/**
* Prints the given string encoded to html-safe.
* If other parameters are given, use sprintf() first to all parameters.
*
* @param string $str
* @param mixed ...
*/
function h($str) {
if (func_num_args()>1) {
$str = call_user_func_array('sprintf', func_get_args());
}
print htmlspecialchars($str);
}
/**
* Like func_get_args(), returns its invoker function call's parameters, but without the first item
*
* @return array (mixed)
*/
function func_get_args_but_first() {
$st = debug_backtrace();
$args = $st[1]['args'];
array_shift($args);
return $args;
}
/**
* Returns the first not null argument.
* If there's no such argument, returns null
*
* @param mixed Any data
* @return mixed The first not-null parameter, if exists
*/
function coalesce() {
foreach (func_get_args() as $arg) {
if (!is_null($arg)) {
return $arg;
}
}
return null;
}
/**
* Implodes the content of the given associative array.
*
* @param array (mixed) $array
* @param string $betweenKeyAndValue
* @param string $beforeItems
* @param string $afterItems
* @return string
*/
function implode_assoc(array $array, $betweenKeyAndValue, $beforeItems='', $afterItems='') {
$result = '';
foreach ($array as $k=>$v) {
$result .= $beforeItems.$k.$betweenKeyAndValue.$v.$afterItems;
}
return $result;
}
/**
* Return true, if the given parameter is empty.
* Wrapper function for php empty predicate
*
* @param mixed $data
* @return boolean
*/
function is_empty($data) {
return empty($data);
}
/**
* Merges the given parameters to one associative array. When two
* arrays found at the same position, then they will be merged, too.
*
* @param array $array1
* @param array ...
* @return array
*/
function config_merge(array $array1) {
$result = array();
foreach (func_get_args() as $param) {
foreach ($param as $key=>$value) {
if (is_array($value) && array_key_exists($key, $result) && is_array($result[$key])) {
$result[$key] = config_merge($result[$key], $value);
} else {
$result[$key] = $value;
}
}
}
return $result;
}
/**
* Defines getallheaders(), if needed.
*
* @see http://php.net/manual/en/function.getallheaders.php
*/
if (!function_exists('getallheaders')) {
function getallheaders() {
$out = array();
foreach($_SERVER as $key=>$value) {
if ('HTTP_' == substr($key,0,5)) {
$key = str_replace(' ','-',ucwords(strtolower(str_replace('_',' ',substr($key,5)))));
$out[$key] = $value;
}
}
return $out;
}
}