Passed
Branch master (f22511)
by Abishek R
01:50
created

Helpers::validateArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Greenlyst\BaseCommerce\Core;
6
7
use Greenlyst\BaseCommerce\LogicException;
8
use function ArrayHelpers\array_has;
9
10
class Helpers
11
{
12
    /**
13
     * @param $data
14
     * @return array
15
     */
16
    public static function clearArray($data)
17
    {
18
        return array_filter($data, function ($item) {
19
            if ($item === null)
20
                return false;
21
            return true;
22
        });
23
    }
24
25
    /**
26
     * @param $array
27
     * @param $keys
28
     * @throws LogicException
29
     */
30
    public static function validateArray($array, $keys)
31
    {
32
        foreach ($keys as $key) {
33
            if (array_has($array, $key) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
34
                throw LogicException::requiredFieldDoesntExist($key);
35
            }
36
        }
37
    }
38
39
40
    /**
41
     * Get all of the given array except for a specified array of items.
42
     *
43
     * @param array $array
44
     * @param array|string $keys
45
     * @return array
46
     */
47
    public static function array_except($array, $keys)
48
    {
49
        return array_diff_key($array, array_flip((array)$keys));
50
    }
51
52
    public static function replaceArrayKeys($array, $search, $replace)
53
    {
54
        $newArray = [];
55
56
        foreach ($array as $key => $item) {
57
            $newArray[str_replace($search, $replace, $key)] = $item;
58
        }
59
60
        return $newArray;
61
    }
62
63
}