Completed
Pull Request — master (#168)
by Phecho
04:49
created

helpers.php ➔ l()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
/**
13
 * Associate another dict to a dict based on a common key.
14
 *
15
 * @param array  $input
16
 * @param array  $relations
17
 * @param string $relation
18
 * @param string $column
19
 */
20
function array_merge_relation(array &$input, array &$relations, $relation, $column)
21
{
22
    foreach ($input as &$row) {
23
        if (isset($row[$column]) && isset($relations[$row[$column]])) {
24
            $row[$relation] = $relations[$row[$column]];
25
        } else {
26
            $row[$relation] = [];
27
        }
28
    }
29
}
30
31
/**
32
 * Create indexed array from a list of dict.
33
 *
34
 * $input = [
35
 *   ['k1' => 1, 'k2' => 2], ['k1' => 3, 'k2' => 4], ['k1' => 1, 'k2' => 5]
36
 * ]
37
 *
38
 * array_column_index($input, 'k1') will returns:
39
 *
40
 * [
41
 *   1 => [['k1' => 1, 'k2' => 2], ['k1' => 1, 'k2' => 5]],
42
 *   3 => [['k1' => 3, 'k2' => 4]],
43
 * ]
44
 *
45
 * @param array  $input
46
 * @param string $column
47
 *
48
 * @return array
49
 */
50
function array_column_index(array &$input, $column)
51
{
52
    $result = [];
53
54
    foreach ($input as &$row) {
55
        if (isset($row[$column])) {
56
            $result[$row[$column]][] = $row;
57
        }
58
    }
59
60
    return $result;
61
}
62
63
/**
64
 * Sum all values from a single column in the input array.
65
 *
66
 * $input = [
67
 *   ['column' => 2], ['column' => 3]
68
 * ]
69
 *
70
 * array_column_sum($input, 'column') returns 5
71
 *
72
 * @param array  $input
73
 * @param string $column
74
 *
75
 * @return float
76
 */
77
function array_column_sum(array &$input, $column)
78
{
79
    $sum = 0.0;
80
81
    foreach ($input as &$row) {
82
        if (isset($row[$column])) {
83
            $sum += (float) $row[$column];
84
        }
85
    }
86
87
    return $sum;
88
}
89
90
/**
91
 * Get upload max size.
92
 *
93
 * @return string
94
 */
95
function get_upload_max_size()
96
{
97
    return min(ini_get('upload_max_filesize'), ini_get('post_max_size'));
98
}
99
100
/**
101
 * Hash the given value.
102
 *
103
 * @param string $value
104
 * @param array  $options
105
 *
106
 * @return string
107
 */
108
function bcrypt($value, array $options = [])
109
{
110
    $cost = isset($options['rounds']) ? $options['rounds'] : 10;
111
    $hash = password_hash($value, PASSWORD_BCRYPT, ['cost' => $cost]);
112
    if ($hash === false) {
113
        trigger_error('Bcrypt hashing not supported.', E_USER_WARNING);
114
115
        return;
116
    }
117
118
    return $hash;
119
}
120
121
/**
122
 * Translate a string.
123
 *
124
 * @return string
125
 */
126
function t()
127
{
128
    return call_user_func_array([\Jitamin\Foundation\Translator::getInstance(), 'translate'], func_get_args());
129
}
130
131
/**
132
 * Translate a string with no HTML escaping language (raw data).
133
 *
134
 * @return string
135
 */
136
function l()
137
{
138
    return call_user_func_array([\Jitamin\Foundation\Translator::getInstance(), 'translateNoEscaping'], func_get_args());
139
}
140
141
/**
142
 * Translate a number.
143
 *
144
 * @param mixed $value
145
 *
146
 * @return string
147
 */
148
function n($value)
149
{
150
    return \Jitamin\Foundation\Translator::getInstance()->number($value);
151
}
152