Completed
Push — master ( 89bfcf...4ab803 )
by Vladimir
02:41
created

ZipFilter::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Twig;
9
10
class ZipFilter implements StakxTwigFilter
11
{
12 10
    public function __invoke(array $array1, array $array2, $glue = '', $strict = false)
13
    {
14 10
        $result = array();
15 10
        $arr1_length = count($array1);
16 10
        $arr2_length = count($array2);
17
18 10
        for ($i = 0; $i < $arr1_length; $i++)
19
        {
20 10
            if ($i >= $arr2_length)
21
            {
22 2
                break;
23
            }
24
25 10
            $rhs = self::safe_get($array1, $i);
26 10
            $lhs = self::safe_get($array2, $i);
27
28 10
            if (empty($rhs))
29
            {
30
                $result[] = $lhs;
31
                continue;
32
            }
33
            elseif (empty($lhs))
34
            {
35 2
                $result[] = $rhs;
36 2
                continue;
37
            }
38
39 10
            $result[] = self::safe_get($array1, $i) . $glue . self::safe_get($array2, $i);
40
        }
41
42 10
        if (!$strict)
43
        {
44 6
            if ($arr2_length > $arr1_length)
45
            {
46 2
                $result = array_merge($result, array_slice($array2, $arr1_length));
47
            }
48
            else
49
            {
50 4
                $result = array_merge($result, array_slice($array1, $arr2_length));
51
            }
52
        }
53
54 10
        return $result;
55
    }
56
57 2
    public static function get()
58
    {
59 2
        return new \Twig_SimpleFilter('zip', new self());
60
    }
61
62 10
    private static function safe_get(array &$array, $key, $default = '')
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
63
    {
64 10
        return (isset($array[$key]) ? (string)$array[$key] : $default);
65
    }
66
}
67