Passed
Push — master ( ec5be4...a63c4d )
by
unknown
02:10
created

mappings.php ➔ select()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 1
nop 4
dl 0
loc 30
ccs 16
cts 16
cp 1
crap 7
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Boudewijn Schoon <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Itertools\mappings;
8
9
use Zicht\Itertools\conversions;
10
11
/**
12
 * Returns a closure that strips any matching $CHARS from the left of the input string
13
 *
14
 * @param string $chars
15
 * @return \Closure
16
 */
17
function lstrip($chars = " \t\n\r\0\x0B")
18
{
19
    return function ($value) use ($chars) {
20 5
        return ltrim($value, $chars);
21 5
    };
22
}
23
24
/**
25
 * Returns a closure that strips any matching $CHARS from the right of the input string
26
 *
27
 * @param string $chars
28
 * @return \Closure
29
 */
30
function rstrip($chars = " \t\n\r\0\x0B")
31
{
32
    return function ($value) use ($chars) {
33 5
        return rtrim($value, $chars);
34 5
    };
35
}
36
37
/**
38
 * Returns a closure that strips any matching $CHARS from the left and right of the input string
39
 *
40
 * @param string $chars
41
 * @return \Closure
42
 */
43
function strip($chars = " \t\n\r\0\x0B")
44
{
45
    return function ($value) use ($chars) {
46 5
        return trim($value, $chars);
47 6
    };
48
}
49
50
/**
51
 * Returns a closure that returns the length of the input
52
 *
53
 * @return \Closure
54
 */
55
function length()
56
{
57
    return function ($value) {
58 3
        if (is_null($value)) {
59 3
            return 0;
60
        }
61
62 3
        if (is_string($value)) {
63 3
            return strlen($value);
64
        }
65
66 3
        return sizeof($value);
67 3
    };
68
}
69
70
/**
71
 * Returns a closure that returns the key
72
 *
73
 * @return \Closure
74
 */
75
function key()
76
{
77
    return function ($value, $key) {
78 3
        return $key;
79 3
    };
80
}
81
82
/**
83
 * Returns a closure that returns the string value lower cased
84
 *
85
 * @return \Closure
86
 */
87
function lower()
88
{
89
    return function ($value) {
90 1
        return strtolower($value);
91 1
    };
92
}
93
94
/**
95
 * Returns a closure that returns the string value upper cased
96
 *
97
 * @return \Closure
98
 */
99
function upper()
100
{
101
    return function ($value) {
102 1
        return strtoupper($value);
103 1
    };
104
}
105
106
/**
107
 * Returns a closure that applies multiple $STRATEGIES to the value and returns the results
108
 *
109
 * > $compute = function ($value, $key) {
110
 * >    return 'some computation result';
111
 * > };
112
 * > $list = iter\iterable([new Data(1), new Data(2), new Data(3)]);
113
 * > $list->map(select(['data' => null, 'id' => 'Identifier', 'desc' => 'Value.DescriptionName', 'comp' => $compute]));
114
 * [
115
 *    [
116
 *       'data' => Data(1),
117
 *       'id' => Data(1)->Identifier,
118
 *       'desc' => Data(1)->Value->DescriptionName,
119
 *       'comp' => $compute(Data(1), 0),
120
 *    ],
121
 *    ...
122
 *    [
123
 *       'data' => Data(3),
124
 *       'id' => Data(3)->Identifier,
125
 *       'desc' => Data(3)->Value->DescriptionName,
126
 *       'comp' => $compute(Data(3), 2),
127
 *    ],
128
 * ]
129
 *
130
 * @param array $strategies
131
 * @param null|string|\Closure $strategy
132
 * @param boolean $discardNull
133
 * @param boolean $discardEmptyContainer
134
 * @return \Closure
135
 */
136
function select(array $strategies, $strategy = null, $discardNull = false, $discardEmptyContainer = false)
137
{
138 7
    $strategies = array_map('\Zicht\Itertools\conversions\mixed_to_value_getter', $strategies);
139 7
    $strategy = conversions\mixed_to_value_getter($strategy);
140
141
    return function ($value, $key) use ($strategies, $strategy, $discardNull, $discardEmptyContainer) {
142 7
        $value = $strategy($value);
143 7
        $res = [];
144 7
        foreach ($strategies as $strategyKey => $strategy) {
145 6
            $res[$strategyKey] = $strategy($value, $key);
146
        }
147 7
        if ($discardNull || $discardEmptyContainer) {
148 2
            $res = array_filter(
149
                $res,
150
                function ($value) use ($discardNull, $discardEmptyContainer) {
151 2
                    if (null === $value) {
152 1
                        return !$discardNull;
153
                    }
154
155 2
                    if (is_array($value) && 0 === sizeof($value)) {
156 1
                        return !$discardEmptyContainer;
157
                    }
158
159 2
                    return true;
160 2
                }
161
            );
162
        }
163 7
        return $res;
164 7
    };
165
}
166
167
/**
168
 * Returns a closure that returns random integer numbers between $MIN and $MAX
169
 *
170
 * @param int $min
171
 * @param null|int $max
172
 * @return \Closure
173
 */
174
function random($min = 0, $max = null)
175
{
176 5
    if (null === $max) {
177 1
        $max = getrandmax();
178
    }
179
180
    return function () use ($min, $max) {
181 5
        return rand($min, $max);
182 5
    };
183
}
184
185
/**
186
 * Returns a closure that returns either the class name, given an object, or otherwise the type
187
 *
188
 * @param null|string|\Closure $strategy
189
 * @return \Closure
190
 */
191
function type($strategy = null)
192
{
193 6
    $strategy = conversions\mixed_to_value_getter($strategy);
194
    return function ($value) use ($strategy) {
195 6
        $value = $strategy($value);
196 6
        return is_object($value) ? get_class($value) : gettype($value);
197 6
    };
198
}
199
200
/**
201
 * Returns a mapping closure
202
 *
203
 * @param string $name
204
 * @return \Closure
205
 * @throws \InvalidArgumentException
206
 *
207
 * @deprecated please use the mapping functions directly, will be removed in version 3.0
208
 */
209
function get_mapping($name /* [argument, [arguments, ...] */)
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
210
{
211
    if (is_string($name)) {
212
        switch ($name) {
213
            case 'ltrim':
214
            case 'lstrip':
215
                return call_user_func_array('\Zicht\Itertools\mappings\lstrip', array_slice(func_get_args(), 1));
216
217
            case 'rtrim':
218
            case 'rstrip':
219
                return call_user_func_array('\Zicht\Itertools\mappings\rstrip', array_slice(func_get_args(), 1));
220
221
            case 'trim':
222
            case 'strip':
223
                return call_user_func_array('\Zicht\Itertools\mappings\strip', array_slice(func_get_args(), 1));
224
225
            case 'length':
226
                return length();
227
228
            case 'key':
229
                return key();
230
231
            case 'select':
232
                return call_user_func_array('\Zicht\Itertools\mappings\select', array_slice(func_get_args(), 1));
233
234
            case 'random':
235
                return call_user_func_array('\Zicht\Itertools\mappings\random', array_slice(func_get_args(), 1));
236
237
            case 'type':
238
                return call_user_func_array('\Zicht\Itertools\mappings\type', array_slice(func_get_args(), 1));
239
        }
240
    }
241
242
    throw new \InvalidArgumentException(sprintf('$NAME "%s" is not a valid mapping.', $name));
243
}
244
245
/**
246
 * @param string $name
247
 * @return \Closure
248
 * @throws \InvalidArgumentException
249
 *
250
 * @deprecated please use the mapping functions directly, will be removed in version 3.0
251
 */
252
function getMapping($name /* [argument, [arguments, ...] */)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
253
{
254
    return call_user_func_array('\Zicht\Itertools\mappings\get_mapping', func_get_args());
255
}
256