1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
if (!function_exists('array_merge_relation')) { |
13
|
|
|
/** |
14
|
|
|
* Associate another dict to a dict based on a common key. |
15
|
|
|
* |
16
|
|
|
* @param array $input |
17
|
|
|
* @param array $relations |
18
|
|
|
* @param string $relation |
19
|
|
|
* @param string $column |
20
|
|
|
*/ |
21
|
|
|
function array_merge_relation(array &$input, array &$relations, $relation, $column) |
22
|
|
|
{ |
23
|
|
|
foreach ($input as &$row) { |
24
|
|
|
if (isset($row[$column]) && isset($relations[$row[$column]])) { |
25
|
|
|
$row[$relation] = $relations[$row[$column]]; |
26
|
|
|
} else { |
27
|
|
|
$row[$relation] = []; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (!function_exists('array_column_index')) { |
34
|
|
|
/** |
35
|
|
|
* Create indexed array from a list of dict. |
36
|
|
|
* |
37
|
|
|
* $input = [ |
38
|
|
|
* ['k1' => 1, 'k2' => 2], ['k1' => 3, 'k2' => 4], ['k1' => 1, 'k2' => 5] |
39
|
|
|
* ] |
40
|
|
|
* |
41
|
|
|
* array_column_index($input, 'k1') will returns: |
42
|
|
|
* |
43
|
|
|
* [ |
44
|
|
|
* 1 => [['k1' => 1, 'k2' => 2], ['k1' => 1, 'k2' => 5]], |
45
|
|
|
* 3 => [['k1' => 3, 'k2' => 4]], |
46
|
|
|
* ] |
47
|
|
|
* |
48
|
|
|
* @param array $input |
49
|
|
|
* @param string $column |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
function array_column_index(array &$input, $column) |
54
|
|
|
{ |
55
|
|
|
$result = []; |
56
|
|
|
|
57
|
|
|
foreach ($input as &$row) { |
58
|
|
|
if (isset($row[$column])) { |
59
|
|
|
$result[$row[$column]][] = $row; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $result; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!function_exists('array_column_sum')) { |
68
|
|
|
/** |
69
|
|
|
* Sum all values from a single column in the input array. |
70
|
|
|
* |
71
|
|
|
* $input = [ |
72
|
|
|
* ['column' => 2], ['column' => 3] |
73
|
|
|
* ] |
74
|
|
|
* |
75
|
|
|
* array_column_sum($input, 'column') returns 5 |
76
|
|
|
* |
77
|
|
|
* @param array $input |
78
|
|
|
* @param string $column |
79
|
|
|
* |
80
|
|
|
* @return float |
81
|
|
|
*/ |
82
|
|
|
function array_column_sum(array &$input, $column) |
83
|
|
|
{ |
84
|
|
|
$sum = 0.0; |
85
|
|
|
|
86
|
|
|
foreach ($input as &$row) { |
87
|
|
|
if (isset($row[$column])) { |
88
|
|
|
$sum += (float) $row[$column]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $sum; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!function_exists('get_upload_max_size')) { |
97
|
|
|
/** |
98
|
|
|
* Get upload max size. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
function get_upload_max_size() |
103
|
|
|
{ |
104
|
|
|
return min(ini_get('upload_max_filesize'), ini_get('post_max_size')); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (!function_exists('bcrypt')) { |
109
|
|
|
/** |
110
|
|
|
* Hash the given value. |
111
|
|
|
* |
112
|
|
|
* @param string $value |
113
|
|
|
* @param array $options |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
function bcrypt($value, array $options = []) |
118
|
|
|
{ |
119
|
|
|
$cost = isset($options['rounds']) ? $options['rounds'] : 10; |
120
|
|
|
$hash = password_hash($value, PASSWORD_BCRYPT, ['cost' => $cost]); |
121
|
|
|
if ($hash === false) { |
122
|
|
|
trigger_error('Bcrypt hashing not supported.', E_USER_WARNING); |
123
|
|
|
|
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $hash; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
View Code Duplication |
if (!function_exists('t')) { |
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Translate a string. |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
function t() |
138
|
|
|
{ |
139
|
|
|
return call_user_func_array([\Jitamin\Foundation\Translator::getInstance(), 'translate'], func_get_args()); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
View Code Duplication |
if (!function_exists('l')) { |
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Translate a string with no HTML escaping language (raw data). |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
function l() |
150
|
|
|
{ |
151
|
|
|
return call_user_func_array([\Jitamin\Foundation\Translator::getInstance(), 'translateNoEscaping'], func_get_args()); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (!function_exists('n')) { |
156
|
|
|
/** |
157
|
|
|
* Translate a number. |
158
|
|
|
* |
159
|
|
|
* @param mixed $value |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
function n($value) |
164
|
|
|
{ |
165
|
|
|
return \Jitamin\Foundation\Translator::getInstance()->number($value); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.