Passed
Push — master ( c5b925...fe5022 )
by Dante
01:48
created

ArrayHelper::intersect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2018 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace App\View\Helper;
14
15
use Cake\Utility\Hash;
16
use Cake\View\Helper;
17
18
/**
19
 * Helper for array utils
20
 */
21
class ArrayHelper extends Helper
22
{
23
    /**
24
     * Return array_combine of array using values as keys.
25
     *
26
     * @param array $arr The array.
27
     * @return array combined array.
28
     */
29
    public function combine(array $arr): array
30
    {
31
        return array_combine(array_values($arr), array_values($arr));
32
    }
33
34
    /**
35
     * Return array without specified keys.
36
     *
37
     * @param array $arr The array.
38
     * @param array $keys The keys to remove.
39
     * @return array The array without keys.
40
     */
41
    public function removeKeys(array $arr, array $keys): array
42
    {
43
        return array_diff_key($arr, array_flip($keys));
44
    }
45
46
    /**
47
     * Return array with only specified keys maintaining order.
48
     *
49
     * @param array $arr The array.
50
     * @param array $keys The ordered keys to maintain.
51
     * @return array The array with only selected keys.
52
     */
53
    public function onlyKeys(array $arr, array $keys): array
54
    {
55
        $res = [];
56
        foreach ($keys as $k) {
57
            $res[$k] = null;
58
            if (isset($arr[$k])) {
59
                $res[$k] = $arr[$k];
60
            }
61
        }
62
63
        return $res;
64
    }
65
66
    /**
67
     * Gets the values from an array matching the $path expression.
68
     *
69
     * The path expression is a dot separated expression, that can contain a set of patterns and expressions:
70
     *  {n} Matches any numeric key, or integer.
71
     *  {s} Matches any string key.
72
     *  {*} Matches any value.
73
     *  Foo Matches any key with the exact same value.
74
     *
75
     * @see \Cake\Utility\Hash::extract
76
     * @param array $data The array
77
     * @param string $path The path expression
78
     * @return array The result array
79
     */
80
    public function extract($data, $path): array
81
    {
82
        return (array)Hash::extract($data, $path);
83
    }
84
85
    /**
86
     * Return intersection of two arrays reindexing values.
87
     *
88
     * @param array $arr1 First array.
89
     * @param array $arr2 Second array.
90
     * @return array The intersection of two arrays.
91
     */
92
    public function intersect(array $arr1, array $arr2): array
93
    {
94
        return array_values(array_intersect($arr1, $arr2));
95
    }
96
}
97