• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-helpers / 3929967872

pending completion
3929967872

Pull #52

github

GitHub
Merge e8557eed9 into 71346419b
Pull Request #52: feat: update http request service to use easy mock;

32 of 32 new or added lines in 3 files covered. (100.0%)

150 of 910 relevant lines covered (16.48%)

1.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.96
/src/helpers.php
1
<?php
2

3
use Illuminate\Support\Collection;
4
use Illuminate\Support\Arr;
5

6
/**
7
 * Round all values in list of floats.
8
 *
9
 * @param array $array
10
 * @return array
11
 */
12
function array_round(array $array): array
13
{
14
    $keys = array_keys($array);
1✔
15

16
    $values = array_map(function ($value) {
1✔
17
        if (is_numeric($value)) {
1✔
18
            return round($value);
1✔
19
        }
20

21
        return $value;
1✔
22
    }, $array);
1✔
23

24
    return array_combine($keys, $values);
1✔
25
}
26

27
/**
28
 * Get list of element which placed in $path in $array
29
 *
30
 * @param array|string $array
31
 * @param string $path
32
 *
33
 * @return mixed
34
 */
35
function array_get_list($array, $path)
36
{
37
    if (is_string($path)) {
7✔
38
        $path = explode('.', $path);
7✔
39
    }
40

41
    $key = array_shift($path);
7✔
42

43
    if (empty($path)) {
7✔
44
        return ($key === '*') ? $array : Arr::get($array, $key);
7✔
45
    }
46

47
    if ($key === '*') {
5✔
48
        if (empty($array)) {
5✔
49
            return [];
1✔
50
        }
51

52
        $values = array_map(function ($item) use ($path) {
5✔
53
            $value = array_get_list($item, $path);
5✔
54

55
            if (!is_array($value) || is_associative($value)) {
5✔
56
                return [$value];
4✔
57
            }
58

59
            return $value;
4✔
60
        }, $array);
5✔
61

62
        return Arr::collapse($values);
5✔
63
    } else {
64
        $value = Arr::get($array, $key);
5✔
65

66
        return array_get_list($value, $path);
5✔
67
    }
68
}
69

70
/**
71
 * Verifies whether input is associative array or a list
72
 *
73
 * @param array $array
74
 *
75
 * @return boolean
76
 */
77
function is_associative($array)
78
{
79
    return $array !== array_values($array);
8✔
80
}
81

82
/**
83
 * Verifies whether input is array or arrays or not
84
 *
85
 * @param array $array
86
 *
87
 * @return boolean
88
 */
89
function is_multidimensional(array $array): bool
90
{
91
    return is_array(Arr::first($array));
3✔
92
}
93

94
/**
95
 * Create directory recursively. The native mkdir() function recursively create directory incorrectly.
96
 * This is solution.
97
 *
98
 * @param string $path
99
 */
100
function mkdir_recursively($path)
101
{
102
    $explodedPath = explode('/', $path);
×
103

104
    $currentPath = $explodedPath[0];
×
105

106
    array_walk($explodedPath, function ($dir) use (&$currentPath) {
×
107
        if ($currentPath != '/') {
×
108
            $currentPath .= '/' . $dir;
×
109
        } else {
110
            $currentPath .= $dir;
×
111
        }
112

113
        if (!file_exists($currentPath)) {
×
114
            mkdir($currentPath);
×
115
        }
116
    });
×
117
}
118

119
/**
120
 * Check equivalency of two arrays
121
 *
122
 * @param array $array1
123
 * @param array $array2
124
 *
125
 * @return boolean
126
 */
127
function array_equals($array1, $array2)
128
{
129
    if (is_associative($array1)) {
4✔
130
        return array_equals_assoc($array1, $array2);
3✔
131
    }
132

133
    $array1 = (new Collection($array1))->sort()->values()->toArray();
1✔
134
    $array2 = (new Collection($array2))->sort()->values()->toArray();
1✔
135

136
    return $array1 === $array2;
1✔
137
}
138

139
/**
140
 * Check equivalency of two associative arrays
141
 *
142
 * @param array $array1
143
 * @param array $array2
144
 *
145
 * @return boolean
146
 */
147
function array_equals_assoc($array1, $array2)
148
{
149
    $array1 = (new Collection($array1))->sortKeys()->toArray();
3✔
150
    $array2 = (new Collection($array2))->sortKeys()->toArray();
3✔
151

152
    return $array1 === $array2;
3✔
153
}
154

155
/**
156
 * Return subtraction of two arrays
157
 *
158
 * @param array $array1
159
 * @param array $array2
160
 *
161
 * @return array
162
 */
163
function array_subtraction($array1, $array2)
164
{
165
    $intersection = array_intersect($array1, $array2);
×
166

167
    return array_diff($array1, $intersection);
×
168
}
169

170
/**
171
 * Generate GUID
172
 *
173
 * @return string
174
 *
175
 * @codeCoverageIgnore
176
 */
177
function getGUID()
178
{
179
    mt_srand((double)microtime() * 10000);//optional for php 4.2.0 and up.
180
    $charId = strtoupper(md5(uniqid(rand(), true)));
181
    $hyphen = chr(45);// "-"
182
    return chr(123)// "{"
183
        . substr($charId, 0, 8) . $hyphen
184
        . substr($charId, 8, 4) . $hyphen
185
        . substr($charId, 12, 4) . $hyphen
186
        . substr($charId, 16, 4) . $hyphen
187
        . substr($charId, 20, 12)
188
        . chr(125);// "}"
189
}
190

191
function array_concat($array, $callback)
192
{
193
    $content = '';
2✔
194

195
    foreach ($array as $key => $value) {
2✔
196
        $content .= $callback($value, $key);
2✔
197
    }
198

199
    return $content;
2✔
200
}
201

202
function rmdir_recursively($dir)
203
{
204
    if ($objs = glob($dir . "/*")) {
×
205
        foreach ($objs as $obj) {
×
206
            is_dir($obj) ? rmdir_recursively($obj) : unlink($obj);
×
207
        }
208
    }
209
    rmdir($dir);
×
210
}
211

212
function fPutQuotedCsv($handle, $row, $fd = ',', $quot = '"')
213
{
214
    $cells = array_map(function ($cell) use ($quot) {
×
215
        if (preg_match("/[;.\",\n]/", $cell)) {
×
216
            $cell = $quot . str_replace($quot, "{$quot}{$quot}", $cell) . $quot;
×
217
        }
218

219
        return $cell;
×
220
    }, $row);
×
221

222
    $str = implode($fd, $cells);
×
223

224
    fputs($handle, $str . "\n");
×
225

226
    return strlen($str);
×
227
}
228

229
function clear_folder($path)
230
{
231
    $files = glob("$path/*");
×
232

233
    foreach ($files as $file) {
×
234
        if (is_file($file)) {
×
235
            unlink($file);
×
236
        }
237

238
        if (is_dir($file)) {
×
239
            clear_folder($file);
×
240
        }
241
    }
242
}
243

244
/**
245
 * Builds an associative array by gotten keys and values
246
 *
247
 * @param array $array
248
 * @param callable $callback - should return associate array with "key" and "value" keys
249
 *
250
 * @example $callback
251
 *  function ($value, $key) {
252
 *      return [
253
 *        'key' => $key,
254
 *        'value' => $value,
255
 *      ];
256
 *  }
257
 *
258
 * @return array
259
 */
260
function array_associate(array $array, callable $callback): array
261
{
262
    $result = [];
7✔
263

264
    foreach ($array as $key => $value) {
7✔
265
        $callbackResult = $callback($value, $key);
7✔
266

267
        if (!empty($callbackResult)) {
7✔
268
            $result[$callbackResult['key']] = $callbackResult['value'];
7✔
269
        }
270
    }
271

272
    return $result;
7✔
273
}
274

275
/**
276
 * Get duplicate values of array
277
 *
278
 * @param array $array
279
 *
280
 * @return array
281
 *
282
 * @deprecated Use array_get_duplicates
283
 */
284
function array_duplicate($array)
285
{
286
    return array_get_duplicates($array);
3✔
287
}
288

289
/**
290
 * Get duplicate values of array
291
 *
292
 * @param array $array
293
 *
294
 * @return array
295
 */
296
function array_get_duplicates(array $array): array
297
{
298
    return array_diff_key($array, array_unique($array));
6✔
299
}
300

301
/**
302
 * Get only unique objects from array by key (array of keys) or by closure
303
 *
304
 * @param array $objectsList
305
 * @param string|callable|array $filter
306
 *
307
 * @return array
308
 */
309
function array_unique_objects($objectsList, $filter = 'id')
310
{
311
    $uniqueKeys = [];
3✔
312

313
    $uniqueObjects = array_map(function ($object) use (&$uniqueKeys, $filter) {
3✔
314
        if (is_string($filter)) {
3✔
315
            $value = $object[$filter];
1✔
316
        }
317

318
        if (is_callable($filter)) {
3✔
319
            $value = $filter($object);
1✔
320
        }
321

322
        if (is_array($filter)) {
3✔
323
            $value = Arr::only($object, $filter);
1✔
324
        }
325

326
        if (in_array($value, $uniqueKeys)) {
3✔
327
            return null;
3✔
328
        }
329
        $uniqueKeys[] = $value;
3✔
330

331
        return $object;
3✔
332
    }, $objectsList);
3✔
333

334
    return array_filter($uniqueObjects, function ($item) {
3✔
335
        return !is_null($item);
3✔
336
    });
3✔
337
}
338

339
function array_trim(array $array): array
340
{
341
    return array_map(
1✔
342
        function ($item) {
1✔
343
            return (is_string($item)) ? trim($item) : $item;
1✔
344
        },
1✔
345
        $array
1✔
346
    );
1✔
347
}
348

349
function array_remove_by_field($array, $fieldName, $fieldValue)
350
{
351
    $array = array_values($array);
2✔
352
    $key = array_search($fieldValue, array_column($array, $fieldName));
2✔
353
    if ($key !== false) {
2✔
354
        unset($array[$key]);
2✔
355
    }
356

357
    return array_values($array);
2✔
358
}
359

360
function array_remove_elements($array, $elements)
361
{
362
    return array_diff($array, $elements);
×
363
}
364

365
function prepend_symbols($string, $expectedLength, $symbol)
366
{
367
    while (strlen($string) < $expectedLength) {
×
368
        $string = "{$symbol}{$string}";
×
369
    }
370

371
    return $string;
×
372
}
373

374
function array_default(&$array, $key, $default)
375
{
376
    $array[$key] = Arr::get($array, $key, $default);
×
377
}
378

379
/**
380
 * inverse transformation from array_dot
381
 * @param $array
382
 * @return array
383
 */
384
function array_undot(array $array): array
385
{
386
    $result = [];
1✔
387

388
    foreach ($array as $key => $value) {
1✔
389
        Arr::set($result, $key, $value);
1✔
390
    }
391

392
    return $result;
1✔
393
}
394

395
function extract_last_part(string $string, string $separator = '.'): array
396
{
397
    $entities = explode($separator, $string);
×
398

399
    $fieldName = array_pop($entities);
×
400

401
    $relation = implode($separator, $entities);
×
402

403
    return [$fieldName, $relation];
×
404
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc