Completed
Push — master ( 0e17ed...515e0a )
by Daniel
03:08
created

Enumerator::closest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4286
cc 3
eloc 8
nc 3
nop 2
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
     * Return the closest found value from array.
145
     *
146
     * @param array $array
147
     * @param sting $value
148
     *
149
     * @return mixed
150
     */
151
    public function closest(array $array, $value)
152
    {
153
        sort($array);
154
        $closest = $array[0];
155
156
        for ($i = 1, $j = count($array), $k = 0; $i < $j; $i++, $k++) {
157
            $middleValue = ($array[$i] - $array[$k]) / 2 + $array[$k];
158
159
            if ($value >= $middleValue) {
160
                $closest = $array[$i];
161
            }
162
        }
163
164
        return $this->value($closest);
165
    }
166
}
167