Completed
Pull Request — master (#212)
by Alexandru
43:58 queued 01:14
created

H::value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Rennokki\DynamoDb;
4
5
/**
6
 * Class H.
7
 *
8
 * Short for "Helper".
9
 * We often get breaking changes from Laravel Helpers, so to ensure this won't happen again, we port the helpers here.
10
 */
11
class H
12
{
13 5
    public static function array_first($array, callable $callback = null, $default = null)
14
    {
15 5
        if (is_null($callback)) {
16
            if (empty($array)) {
17
                return static::value($default);
18
            }
19
            foreach ($array as $item) {
20
                return $item;
21
            }
22
        }
23 5
        foreach ($array as $key => $value) {
24 5
            if (call_user_func($callback, $value, $key)) {
25 5
                return $value;
26
            }
27
        }
28
29 2
        return static::value($default);
30
    }
31
32 2
    public static function value($value)
33
    {
34 2
        return $value instanceof \Closure ? $value() : $value;
35
    }
36
}
37