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

h4kuna / data-type / 14657822982

25 Apr 2025 05:46AM UTC coverage: 96.618% (-0.1%) from 96.75%
14657822982

push

github

h4kuna
features

4 of 5 new or added lines in 2 files covered. (80.0%)

657 of 680 relevant lines covered (96.62%)

0.97 hits per line

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

96.49
/src/Basic/Strings.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\DataType\Basic;
4

5
use h4kuna\DataType\Exceptions\InvalidStateException;
6
use h4kuna\DataType\Exceptions\InvalidTypeException;
7
use h4kuna\DataType\Location;
8
use Nette\StaticClass;
9

10
final class Strings
11
{
12
        use StaticClass;
13

14
        public static function nullable(mixed $value): ?string
1✔
15
        {
16
                return $value === null ? null : self::from($value);
1✔
17
        }
18

19

20
        public static function strokeToPoint(string $value): string
1✔
21
        {
22
                return strtr($value, ',', '.');
1✔
23
        }
24

25

26
        public static function key(string|int ...$values): string
27
        {
NEW
28
                return self::join($values, "\x00");
×
29
        }
30

31

32
        public static function toFloat(string $value): float
1✔
33
        {
34
                return Floats::from($value);
1✔
35
        }
36

37

38
        public static function from(mixed $value): string
1✔
39
        {
40
                if (is_int($value) || is_float($value) || is_null($value)) {
1✔
41
                        return (string) $value;
1✔
42
                } elseif (is_string($value) === false) {
1✔
43
                        throw InvalidTypeException::invalidString($value);
1✔
44
                }
45

46
                return $value;
1✔
47
        }
48

49

50
        public static function toInt(string $value): int
1✔
51
        {
52
                return Integer::from($value);
1✔
53
        }
54

55

56
        /**
57
         * @return array{lat: float, long: float}
58
         */
59
        public static function toGps(string $value): array
1✔
60
        {
61
                return Location\Gps::fromString($value);
1✔
62
        }
63

64

65
        /**
66
         * @return array<string, true>
67
         */
68
        public static function toSet(string $value): array
1✔
69
        {
70
                return Set::fromString($value);
1✔
71
        }
72

73

74
        /**
75
         * foo_bar => FooBar
76
         */
77
        public static function toPascal(string $string): string
1✔
78
        {
79
                return ucfirst(self::toCamel($string));
1✔
80
        }
81

82

83
        /**
84
         * foo_bar => fooBar
85
         */
86
        public static function toCamel(string $string): string
1✔
87
        {
88
                return (string) preg_replace_callback('/_([a-z])/', static function (array $find): string {
1✔
89
                        return strtoupper($find[1]);
1✔
90
                }, $string);
1✔
91
        }
92

93

94
        /**
95
         * The original explode(',', '') return [''] right is [].
96
         *
97
         * @return array<string>
98
         */
99
        public static function split(string $value, string $delimiter = ', '): array
1✔
100
        {
101
                if ($delimiter === '') {
1✔
102
                        throw new InvalidStateException('Delimiter like empty string is not allowed.');
×
103
                } elseif ($value === '') {
1✔
104
                        return [];
1✔
105
                }
106

107
                return explode($delimiter, $value);
1✔
108
        }
109

110

111
        /**
112
         * The original implode/join(',', ['', null, false, 'A']) return ',,,A' right is 'A'.
113
         *
114
         * @param array<scalar|null> $array
115
         */
116
        public static function join(array $array, string $delimiter = ', '): string
1✔
117
        {
118
                return implode(
1✔
119
                        $delimiter,
1✔
120
                        array_filter($array, static fn (mixed $value): bool => $value !== false && $value !== '' && $value !== null)
1✔
121
                );
122
        }
123

124

125
        /**
126
         * FooBar => foo_bar
127
         */
128
        public static function toUnderscore(string $string): string
1✔
129
        {
130
                return strtolower((string) preg_replace_callback('/(.)([A-Z][a-z])|([a-z])([A-Z])/', static function (
1✔
131
                        array $find,
132
                ): string {
133
                        if ($find[1] !== '') {
1✔
134
                                return $find[1] . '_' . $find[2];
1✔
135
                        }
136

137
                        return $find[3] . '_' . $find[4];
1✔
138
                }, $string));
1✔
139
        }
140

141

142
        public static function replaceStart(
1✔
143
                string $subject,
144
                string $search,
145
                string $replacement = '',
146
        ): string
147
        {
148
                return self::strictReplace($subject, $search, $replacement, '^%s', 1);
1✔
149
        }
150

151

152
        private static function strictReplace(
1✔
153
                string $subject,
154
                string $search,
155
                string $replacement,
156
                string $pattern,
157
                int $limit = -1,
158
        ): string
159
        {
160
                return preg_replace(
1✔
161
                        sprintf(self::padIfNeed($pattern, '#', STR_PAD_BOTH), preg_quote($search, '#')),
1✔
162
                        $replacement,
163
                        $subject,
164
                        $limit,
165
                ) ?? $search;
1✔
166
        }
167

168

169
        public static function padIfNeed(string $string, string $padString = '/', int $padType = STR_PAD_LEFT): string
1✔
170
        {
171
                $length = mb_strlen($padString);
1✔
172
                $prefix = $suffix = '';
1✔
173
                if (($padType === STR_PAD_LEFT || $padType === STR_PAD_BOTH) && mb_substr($string, 0, $length) !== $padString) {
1✔
174
                        $prefix = $padString;
1✔
175
                }
176

177
                if (($padType === STR_PAD_RIGHT || $padType === STR_PAD_BOTH) && mb_substr($string, -$length) !== $padString) {
1✔
178
                        $suffix = $padString;
1✔
179
                }
180

181
                return "$prefix$string$suffix";
1✔
182
        }
183

184

185
        public static function replaceEnd(
1✔
186
                string $subject,
187
                string $search,
188
                string $replacement = '',
189
        ): string
190
        {
191
                return self::strictReplace($subject, $search, $replacement, '%s$', 1);
1✔
192
        }
193

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