ArrayHelper::ksplit()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 15
loc 15
ccs 0
cts 14
cp 0
rs 9.7666
cc 4
nc 4
nop 2
crap 20
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\helpers;
12
13
class ArrayHelper extends \yii\helpers\ArrayHelper
14
{
15
    public static function make_sub($array, $v_key, $k_key = null)
16
    {
17
        $res = [];
18
        foreach ($array as $k => $v) {
19
            $res[$k][$v_key] = $v;
20
        }
21
        if ($k_key) {
22
            foreach ($array as $k => $v) {
23
                $res[$k][$k_key] = $k;
24
            }
25
        }
26
27
        return $res;
28
    }
29
30
    /**
31
     * Parses data, exploding the string by comma, trying to create array.
32
     *
33
     * @param $string
34
     * @param string $delimiter
35
     * @return array
36
     */
37 View Code Duplication
    public static function csplit($string, $delimiter = ',')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        if (is_array($string)) {
40
            return $string;
41
        }
42
        $res = [];
43
        foreach (explode($delimiter, $string) as $k => $v) {
44
            $v = trim($v);
45
            if (strlen($v)) {
46
                array_push($res, $v);
47
            }
48
        }
49
50
        return $res;
51
    }
52
53 View Code Duplication
    public static function ksplit($string, $delimiter = ',')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        if (is_array($string)) {
56
            return $string;
57
        }
58
        $res = [];
59
        foreach (explode($delimiter, $string) as $k => $v) {
60
            $v = trim($v);
61
            if (strlen($v)) {
62
                $res[$v] = $v;
63
            }
64
        }
65
66
        return $res;
67
    }
68
69
    /**
70
     * Retrieves the values of an array elements or object properties with the given key or property name.
71
     * Uses [[getValue]] method.
72
     *
73
     * @param array|object $array array or object to extract value from
74
     * @param array [string] $keys keys names of the array elements, or properties of the object
75
     * @return array
76
     * @see getValue()
77
     */
78
    public static function getValues($array, $keys = [])
79
    {
80
        $result = [];
81
        foreach ($keys as $key) {
82
            $result[$key] = static::getValue($array, $key);
83
        }
84
85
        return $result;
86
    }
87
}
88