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

h4kuna / data-type / 10057590774

23 Jul 2024 10:53AM UTC coverage: 96.646% (-0.6%) from 97.239%
10057590774

push

github

h4kuna
add nullable

0 of 4 new or added lines in 4 files covered. (0.0%)

634 of 656 relevant lines covered (96.65%)

0.97 hits per line

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

96.36
/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
15
        {
NEW
16
                return $value === null ? null : self::from($value);
×
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 toFloat(string $value): float
1✔
27
        {
28
                return Floats::from($value);
1✔
29
        }
30

31

32
        public static function from(mixed $value): string
1✔
33
        {
34
                if (is_int($value) || is_float($value) || is_null($value)) {
1✔
35
                        return (string) $value;
1✔
36
                } elseif (is_string($value) === false) {
1✔
37
                        throw InvalidTypeException::invalidString($value);
1✔
38
                }
39

40
                return $value;
1✔
41
        }
42

43

44
        public static function toInt(string $value): int
1✔
45
        {
46
                return Integer::from($value);
1✔
47
        }
48

49

50
        /**
51
         * @return array{lat: float, long: float}
52
         */
53
        public static function toGps(string $value): array
1✔
54
        {
55
                return Location\Gps::fromString($value);
1✔
56
        }
57

58

59
        /**
60
         * @return array<string, true>
61
         */
62
        public static function toSet(string $value): array
1✔
63
        {
64
                return Set::fromString($value);
1✔
65
        }
66

67

68
        /**
69
         * foo_bar => FooBar
70
         */
71
        public static function toPascal(string $string): string
1✔
72
        {
73
                return ucfirst(self::toCamel($string));
1✔
74
        }
75

76

77
        /**
78
         * foo_bar => fooBar
79
         */
80
        public static function toCamel(string $string): string
1✔
81
        {
82
                return (string) preg_replace_callback('/_([a-z])/', static function (array $find): string {
1✔
83
                        return strtoupper($find[1]);
1✔
84
                }, $string);
1✔
85
        }
86

87

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

101
                return explode($delimiter, $value);
1✔
102
        }
103

104

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

118

119
        /**
120
         * FooBar => foo_bar
121
         */
122
        public static function toUnderscore(string $string): string
1✔
123
        {
124
                return strtolower((string) preg_replace_callback('/(.)([A-Z][a-z])|([a-z])([A-Z])/', static function (
1✔
125
                        array $find,
126
                ): string {
127
                        if (isset($find[1]) && $find[1] !== '') {
1✔
128
                                return $find[1] . '_' . $find[2];
1✔
129
                        }
130

131
                        return $find[3] . '_' . $find[4];
1✔
132
                }, $string));
1✔
133
        }
134

135

136
        public static function replaceStart(
1✔
137
                string $subject,
138
                string $search,
139
                string $replacement = '',
140
        ): string
141
        {
142
                return self::strictReplace($subject, $search, $replacement, '^%s', 1);
1✔
143
        }
144

145

146
        private static function strictReplace(
1✔
147
                string $subject,
148
                string $search,
149
                string $replacement,
150
                string $pattern,
151
                int $limit = -1,
152
        ): string
153
        {
154
                return preg_replace(
1✔
155
                        sprintf(self::padIfNeed($pattern, '#', STR_PAD_BOTH), preg_quote($search, '#')),
1✔
156
                        $replacement,
157
                        $subject,
158
                        $limit,
159
                ) ?? $search;
1✔
160
        }
161

162

163
        public static function padIfNeed(string $string, string $padString = '/', int $padType = STR_PAD_LEFT): string
1✔
164
        {
165
                $length = mb_strlen($padString);
1✔
166
                $prefix = $suffix = '';
1✔
167
                if (($padType === STR_PAD_LEFT || $padType === STR_PAD_BOTH) && mb_substr($string, 0, $length) !== $padString) {
1✔
168
                        $prefix = $padString;
1✔
169
                }
170

171
                if (($padType === STR_PAD_RIGHT || $padType === STR_PAD_BOTH) && mb_substr($string, -$length) !== $padString) {
1✔
172
                        $suffix = $padString;
1✔
173
                }
174

175
                return "$prefix$string$suffix";
1✔
176
        }
177

178

179
        public static function replaceEnd(
1✔
180
                string $subject,
181
                string $search,
182
                string $replacement = '',
183
        ): string
184
        {
185
                return self::strictReplace($subject, $search, $replacement, '%s$', 1);
1✔
186
        }
187

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

© 2026 Coveralls, Inc