Completed
Push — master ( 899eaf...7feae9 )
by Dave
29s queued 14s
created

ArrayUtils   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 136
rs 10
wmc 19

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getIntValue() 0 7 2
A getStringValue() 0 7 2
A getIntOrNullValue() 0 10 3
A getIntAsStringValue() 0 12 3
A getArrayValue() 0 7 2
A assertArrayKeyExists() 0 4 2
A assertArray() 0 4 2
A getOptionalStringValue() 0 10 3
1
<?php
2
3
/**
4
 * Static Analysis Results Baseliner (sarb).
5
 *
6
 * (c) Dave Liddament
7
 *
8
 * For the full copyright and licence information please view the LICENSE file distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils;
14
15
class ArrayUtils
16
{
17
    /**
18
     * Gets string value for given key in the array.
19
     *
20
     * @throws ArrayParseException
21
     * @psalm-param array<mixed> $array
22
     */
23
    public static function getStringValue(array $array, string $key): string
24
    {
25
        self::assertArrayKeyExists($array, $key);
26
        if (is_string($array[$key])) {
27
            return $array[$key];
28
        }
29
        throw ArrayParseException::invalidType($key, 'string');
30
    }
31
32
    /**
33
     * Gets int value for given key in the array.
34
     *
35
     * @throws ArrayParseException
36
     * @psalm-param array<mixed> $array
37
     */
38
    public static function getIntValue(array $array, string $key): int
39
    {
40
        self::assertArrayKeyExists($array, $key);
41
        if (is_int($array[$key])) {
42
            return $array[$key];
43
        }
44
        throw ArrayParseException::invalidType($key, 'int');
45
    }
46
47
    /**
48
     * Gets int value for given key in the array.
49
     *
50
     * @throws ArrayParseException
51
     *
52
     * @psalm-param array<mixed> $array
53
     */
54
    public static function getIntOrNullValue(array $array, string $key): ?int
55
    {
56
        self::assertArrayKeyExists($array, $key);
57
        if (null === $array[$key]) {
58
            return null;
59
        }
60
        if (is_int($array[$key])) {
61
            return $array[$key];
62
        }
63
        throw ArrayParseException::invalidType($key, 'int');
64
    }
65
66
    /**
67
     * Gets optional value. Note: if key exists, then the value must be a string.
68
     *
69
     * @throws ArrayParseException
70
     *
71
     * @psalm-param array<mixed> $array
72
     */
73
    public static function getOptionalStringValue(array $array, string $key): ?string
74
    {
75
        if (!array_key_exists($key, $array)) {
76
            return null;
77
        }
78
79
        if (is_string($array[$key])) {
80
            return $array[$key];
81
        }
82
        throw ArrayParseException::invalidType($key, 'string');
83
    }
84
85
    /**
86
     * Gets array value for given key in the array.
87
     *
88
     * @throws ArrayParseException
89
     * @psalm-param array<mixed> $array
90
     * @psalm-return array<mixed> $array
91
     */
92
    public static function getArrayValue(array $array, string $key): array
93
    {
94
        self::assertArrayKeyExists($array, $key);
95
        if (is_array($array[$key])) {
96
            return $array[$key];
97
        }
98
        throw ArrayParseException::invalidType($key, 'array');
99
    }
100
101
    /**
102
     * @throws ArrayParseException
103
     * @psalm-param array<mixed> $array
104
     */
105
    private static function assertArrayKeyExists(array $array, string $key): void
106
    {
107
        if (!array_key_exists($key, $array)) {
108
            throw ArrayParseException::missingKey($key);
109
        }
110
    }
111
112
    /**
113
     * @param mixed $entity
114
     * @psalm-assert array $entity
115
     *
116
     * @throws ArrayParseException
117
     */
118
    public static function assertArray($entity): void
119
    {
120
        if (!is_array($entity)) {
121
            throw ArrayParseException::invalidType('base level', 'array');
122
        }
123
    }
124
125
    /**
126
     * Extracts a integer value from an array, however the integer is in string format.
127
     *
128
     * e.g. Assume the following code:
129
     *
130
     *   $array = ['age' => '21'];
131
     *   $age = ArrayUtils::getIntAsStringValue($array, 'age');
132
     *
133
     * $age would be the integer value 21.
134
     *
135
     * @psalm-param array<mixed> $array
136
     *
137
     * @throws ArrayParseException
138
     */
139
    public static function getIntAsStringValue(array $array, string $key): int
140
    {
141
        self::assertArrayKeyExists($array, $key);
142
        if (is_string($array[$key])) {
143
            $valueAsString = $array[$key];
144
            $valueAsInt = (int) $valueAsString;
145
            $intValueAsString = (string) $valueAsInt;
146
            if ($intValueAsString === $valueAsString) {
147
                return $valueAsInt;
148
            }
149
        }
150
        throw ArrayParseException::invalidType($key, 'int as string');
151
    }
152
}
153