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

h4kuna / data-type / 12386052846

18 Dec 2024 03:53AM UTC coverage: 97.13% (-0.1%) from 97.273%
12386052846

push

github

h4kuna
feat(Interval): support milliseconds

4 of 4 new or added lines in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

643 of 662 relevant lines covered (97.13%)

0.97 hits per line

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

97.87
/src/Basic/Arrays.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\DataType\Basic;
4

5
use h4kuna\DataType;
6
use Nette\StaticClass;
7
use Nette\Utils\Strings;
8
use Stringable;
9

10
final class Arrays
11
{
12
        use StaticClass;
13

14
        /**
15
         * Better array_combine where values array does not need same size.
16
         * @param array<string|int> $keys
17
         * @param array<mixed> $values
18
         * @return array<string|int, mixed>
19
         */
20
        public static function combine(array $keys, array $values, mixed $value = null): array
1✔
21
        {
22
                $diff = count($keys) - count($values);
1✔
23

24
                if ($diff > 0) {
1✔
25
                        $values = array_merge($values, array_fill(0, $diff, $value));
1✔
26
                } elseif ($diff < 0) {
1✔
27
                        throw new DataType\Exceptions\InvalidArgumentsException('Array of values can\'t be bigger than keys.');
1✔
28
                }
29

30
                return array_combine($keys, $values);
1✔
31
        }
32

33

34
        public static function startWith(string $haystack, string ...$needle): bool
1✔
35
        {
36
                foreach ($needle as $str) {
1✔
37
                        if (str_starts_with($haystack, $str)) {
1✔
38
                                return true;
1✔
39
                        }
40
                }
41

42
                return false;
1✔
43
        }
44

45

46
        /**
47
         * strict in_array
48
         * @param list<mixed>|array<string|int, mixed> $haystack
49
         */
50
        public static function contains(string $needle, array $haystack): bool
51
        {
UNCOV
52
                return in_array($needle, $haystack, true);
×
53
        }
54

55

56
        /**
57
         * @param array<scalar|Stringable|null> $array
58
         * @deprecated use join
59
         * Implode only values where strlen > 0 and you can define keys.
60
         */
61
        public static function concatWs(string $glue, array $array): string
1✔
62
        {
63
                return self::join($array, $glue);
1✔
64
        }
65

66

67
        /**
68
         * The original implode/join(',', ['', null, false, 'A']) return ',,,A' right is 'A'.
69
         * @param array<scalar|Stringable|null> $array
70
         */
71
        public static function join(array $array, string $delimiter = ','): string
1✔
72
        {
73
                return implode(
1✔
74
                        $delimiter,
1✔
75
                        array_filter($array, static fn (mixed $value): bool => $value !== false && $value !== '' && $value !== null)
1✔
76
                );
77
        }
78

79

80
        /**
81
         * The original explode(',', '') return [''] right is [].
82
         * @param non-empty-string $delimiter
83
         *
84
         * @return array<string>
85
         */
86
        public static function explode(string $value, string $delimiter = ','): array
1✔
87
        {
88
                if ($value === '') {
1✔
89
                        return [];
1✔
90
                }
91

92
                return explode($delimiter, $value);
1✔
93
        }
94

95

96
        /**
97
         * COALESCE similar behavior database.
98
         * @param iterable<string|int, mixed> $array
99
         */
100
        public static function coalesce(iterable $array): mixed
1✔
101
        {
102
                foreach ($array as $v) {
1✔
103
                        if ($v !== null) {
1✔
104
                                return $v;
1✔
105
                        }
106
                }
107

108
                return null;
1✔
109
        }
110

111

112
        /**
113
         * Unset keys from array.
114
         * @param array<mixed> $array
115
         * @param string|int $keys
116
         * @return array<mixed>
117
         */
118
        public static function unsetKeys(&$array, ...$keys): array
1✔
119
        {
120
                $out = [];
1✔
121
                foreach ($keys as $key) {
1✔
122
                        if (array_key_exists($key, $array)) {
1✔
123
                                $out[$key] = $array[$key];
1✔
124
                                unset($array[$key]);
1✔
125
                        }
126
                }
127

128
                return $out;
1✔
129
        }
130

131

132
        /**
133
         * @template T
134
         * @param array<string|int, T> $values
135
         * @param array<string|int> $keys
136
         * @return array<string|int, T>
137
         */
138
        public static function intersectKeys(array $values, array $keys): array
1✔
139
        {
140
                return array_intersect_key($values, array_flip($keys));
1✔
141
        }
142

143

144
        /**
145
         * @return array<int>
146
         */
147
        public static function generateNumbers(int $from, int $to): array
1✔
148
        {
149
                $values = range($from, $to, ($from < $to) ? 1 : -1);
1✔
150

151
                return array_combine($values, $values);
1✔
152
        }
153

154

155
        /**
156
         * @param array<mixed> $array1
157
         * @param array<mixed> $array2
158
         * @param array<mixed> ...$arrays
159
         * @return array<mixed>
160
         */
161
        public static function mergeUnique(array $array1, array $array2, array ...$arrays): array
1✔
162
        {
163
                return array_values(array_unique(array_merge($array1, $array2, ...$arrays)));
1✔
164
        }
165

166

167
        /**
168
         * @return array<string>
169
         */
170
        public static function text2Array(string $text): array
1✔
171
        {
172
                $existsNewMethod = method_exists(Strings::class, 'unixNewLines'); // @phpstan-ignore-line
1✔
173
                return explode("\n", $existsNewMethod
1✔
174
                        ? Strings::unixNewLines($text)
1✔
175
                        : Strings::normalizeNewLines($text)
1✔
176
                );
177
        }
178

179
}
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

© 2025 Coveralls, Inc