Passed
Pull Request — master (#64)
by Dave
02:05
created

ArrayUtils::getIntOrNullValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
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
     * @phpstan-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
     * @phpstan-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
     * @phpstan-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 array value for given key in the array.
68
     *
69
     * @throws ArrayParseException
70
     * @phpstan-param array<mixed> $array
71
     * @phpstan-return array<mixed> $array
72
     */
73
    public static function getArrayValue(array $array, string $key): array
74
    {
75
        self::assertArrayKeyExists($array, $key);
76
        if (is_array($array[$key])) {
77
            return $array[$key];
78
        }
79
        throw ArrayParseException::invalidType($key, 'array');
80
    }
81
82
    /**
83
     * @throws ArrayParseException
84
     * @phpstan-param array<mixed> $array
85
     */
86
    private static function assertArrayKeyExists(array $array, string $key): void
87
    {
88
        if (!array_key_exists($key, $array)) {
89
            throw ArrayParseException::missingKey($key);
90
        }
91
    }
92
93
    /**
94
     * @param mixed $entity
95
     *
96
     * @throws ArrayParseException
97
     */
98
    public static function assertArray($entity): void
99
    {
100
        if (!is_array($entity)) {
101
            throw ArrayParseException::invalidType('base level', 'array');
102
        }
103
    }
104
105
    /**
106
     * Extracts a integer value from an array, however the integer is in string format.
107
     *
108
     * e.g. Assume the following code:
109
     *
110
     *   $array = ['age' => '21'];
111
     *   $age = ArrayUtils::getIntAsStringValue($array, 'age');
112
     *
113
     * $age would be the integer value 21.
114
     *
115
     * @phpstan-param array<mixed> $array
116
     *
117
     * @throws ArrayParseException
118
     */
119
    public static function getIntAsStringValue(array $array, string $key): int
120
    {
121
        self::assertArrayKeyExists($array, $key);
122
        if (is_string($array[$key])) {
123
            $valueAsString = $array[$key];
124
            $valueAsInt = (int) $valueAsString;
125
            $intValueAsString = (string) $valueAsInt;
126
            if ($intValueAsString === $valueAsString) {
127
                return $valueAsInt;
128
            }
129
        }
130
        throw ArrayParseException::invalidType($key, 'int as string');
131
    }
132
}
133