Completed
Pull Request — master (#212)
by Alexandru
45:00 queued 03:00
created

H::array_first()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 8.304

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
ccs 6
cts 10
cp 0.6
rs 9.2222
c 0
b 0
f 0
cc 6
nc 8
nop 3
crap 8.304
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
 * @package Rennokki\DynamoDb
12
 */
13
class H
14
{
15 5
    public static function array_first($array, callable $callback = null, $default = null)
16
    {
17 5
        if (is_null($callback)) {
18
            if (empty($array)) {
19
                return static::value($default);
20
            }
21
            foreach ($array as $item) {
22
                return $item;
23
            }
24
        }
25 5
        foreach ($array as $key => $value) {
26 5
            if (call_user_func($callback, $value, $key)) {
27 5
                return $value;
28
            }
29
        }
30 2
        return static::value($default);
31
    }
32
33 2
    public static function value($value)
34
    {
35 2
        return $value instanceof \Closure ? $value() : $value;
36
    }
37
}
38