Completed
Push — master ( b139e2...07b7da )
by Daniel
03:29
created

Enumerator::isIndexed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace Narrowspark\Arr;
3
4
use Narrowspark\Arr\Traits\ValueTrait;
5
6
class Enumerator
7
{
8
    use ValueTrait;
9
10
    /**
11
     * Get a random element from the array supplied.
12
     *
13
     * @param array $array the source array
14
     *
15
     * @return mixed
16
     */
17
    public function random(array $array)
18
    {
19
        if (!count($array)) {
20
            return;
21
        }
22
23
        $keys = array_rand($array, 1);
24
25
        return $this->value($array[$keys]);
26
    }
27
28
    /**
29
     * Get a subset of the items from the given array.
30
     *
31
     * @param string[] $array
32
     * @param string[] $keys
33
     *
34
     * @return string[]
35
     */
36
    public function only(array $array, $keys)
37
    {
38
        return array_intersect_key($array, array_flip((array) $keys));
39
    }
40
41
    /**
42
     * Determines if an array is associative.
43
     *
44
     * @param array $array
45
     *
46
     * @return bool
47
     */
48
    public function isAssoc(array $array)
49
    {
50
        if ($array === []) {
51
            return true;
52
        }
53
54
        return array_keys($array) !== range(0, count($array) - 1);
55
    }
56
57
    /**
58
     * Split an array in the given amount of pieces.
59
     *
60
     * @param array $array
61
     * @param int   $numberOfPieces
62
     * @param bool  $preserveKeys
63
     *
64
     * @return array
65
     */
66
    public function split(array $array, $numberOfPieces = 2, $preserveKeys = false)
67
    {
68
        if (count($array) === 0) {
69
            return [];
70
        }
71
72
        $splitSize = ceil(count($array) / $numberOfPieces);
73
74
        return array_chunk($array, $splitSize, $preserveKeys);
75
    }
76
77
    /**
78
     * Check if an array has a numeric index.
79
     *
80
     * @param array $array
81
     *
82
     * @return bool
83
     */
84
    public function isIndexed(array $array)
85
    {
86
        if ($array == []) {
87
            return true;
88
        }
89
90
        return !$this->isAssoc($array);
91
    }
92
93
    /**
94
     * Push an item onto the beginning of an array.
95
     *
96
     * @param array $array
97
     * @param mixed $value
98
     * @param mixed $key
99
     *
100
     * @return array
101
     */
102
    public function prepend(array $array, $value, $key = null)
103
    {
104
        if (is_null($key)) {
105
            array_unshift($array, $value);
106
        } else {
107
            $array = [$key => $value] + $array;
108
        }
109
110
        return $array;
111
    }
112
113
    /**
114
     * Replace a given pattern with each value in the array in sequentially.
115
     *
116
     * @param string $pattern
117
     * @param array  $replacements
118
     * @param string $subject
119
     *
120
     * @return string
121
     */
122
    public function pregReplaceSub($pattern, &$replacements, $subject)
123
    {
124
        return preg_replace_callback($pattern, function ($match) use (&$replacements) {
125
            return array_shift($replacements);
126
        }, $subject);
127
    }
128
129
    /**
130
     * A shorter way to run a match on the array's keys rather than the values.
131
     *
132
     * @param string $pattern
133
     * @param array  $input
134
     * @param int    $flags
135
     *
136
     * @return array
137
     */
138
    public function pregGrepKeys($pattern, array $input, $flags = 0)
139
    {
140
        return array_intersect_key($input, array_flip(preg_grep($pattern, array_keys($input), $flags)));
141
    }
142
143
    /**
144
     * Explode the "value" and "key" arguments passed to "pluck".
145
     *
146
     * @param string      $value
147
     * @param string|null $key
148
     *
149
     * @return array[]
150
     */
151
    protected function explodePluckParameters($value, $key)
152
    {
153
        $value = is_array($value) ? $value : explode('.', $key);
154
155
        $key = is_null($key) || is_array($key) ? $key : explode('.', $key);
156
157
        return [$value, $key];
158
    }
159
}
160