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

h4kuna / data-type / 6788287166

07 Nov 2023 05:41PM UTC coverage: 97.01% (-0.07%) from 97.084%
6788287166

push

github

h4kuna
Iterators: add ReverseArray

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

1 existing line in 1 file now uncovered.

584 of 602 relevant lines covered (97.01%)

0.97 hits per line

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

97.83
/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

9
final class Strings
10
{
11
        public static function from(mixed $value): string
1✔
12
        {
13
                if (is_int($value) || is_float($value) || is_null($value)) {
1✔
14
                        return (string) $value;
1✔
15
                } elseif (is_string($value) === false) {
1✔
16
                        throw InvalidTypeException::invalidString($value);
1✔
17
                }
18

19
                return $value;
1✔
20
        }
21

22

23
        public static function strokeToPoint(string $value): string
1✔
24
        {
25
                return strtr($value, [',' => '.']);
1✔
26
        }
27

28

29
        public static function toFloat(string $value): float
1✔
30
        {
31
                return Floats::from($value);
1✔
32
        }
33

34

35
        public static function toInt(string $value): int
1✔
36
        {
37
                return Integer::from($value);
1✔
38
        }
39

40

41
        /**
42
         * @return array{lat: float, long: float}
43
         */
44
        public static function toGps(string $value): array
1✔
45
        {
46
                return Location\Gps::fromString($value);
1✔
47
        }
48

49

50
        /**
51
         * @return array<string, true>
52
         */
53
        public static function toSet(string $value): array
1✔
54
        {
55
                return Set::fromString($value);
1✔
56
        }
57

58

59
        /**
60
         * foo_bar => fooBar
61
         */
62
        public static function toCamel(string $string): string
1✔
63
        {
64
                return (string) preg_replace_callback('/_([a-z])/', static function (array $find): string {
1✔
65
                        return strtoupper($find[1]);
1✔
66
                }, $string);
1✔
67
        }
68

69

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

78

79
        /**
80
         * The original explode(',', '') return [''] right is [].
81
         *
82
         * @return array<string>
83
         */
84
        public static function split(string $value, string $delimiter = ', '): array
1✔
85
        {
86
                if ($delimiter === '') {
1✔
UNCOV
87
                        throw new InvalidStateException('Delimiter like empty string is not allowed.');
×
88
                } elseif ($value === '') {
1✔
89
                        return [];
1✔
90
                }
91

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

95

96
        /**
97
         * The original implode/join(',', ['', null, false, 'A']) return ',,,A' right is 'A'.
98
         * @param array<scalar|null> $array
99
         */
100
        public static function join(array $array, string $delimiter = ', '): string
1✔
101
        {
102
                return implode(
1✔
103
                        $delimiter,
1✔
104
                        array_filter($array, static fn (mixed $value): bool => $value !== false && $value !== '' && $value !== null)
1✔
105
                );
106
        }
107

108

109
        public static function padIfNeed(string $string, string $padString = '/', int $padType = STR_PAD_LEFT): string
1✔
110
        {
111
                $length = mb_strlen($padString);
1✔
112
                $prefix = $suffix = '';
1✔
113
                if (($padType === STR_PAD_LEFT || $padType === STR_PAD_BOTH) && mb_substr($string, 0, $length) !== $padString) {
1✔
114
                        $prefix = $padString;
1✔
115
                }
116

117
                if (($padType === STR_PAD_RIGHT || $padType === STR_PAD_BOTH) && mb_substr($string, -$length) !== $padString) {
1✔
118
                        $suffix = $padString;
1✔
119
                }
120

121
                return "$prefix$string$suffix";
1✔
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 (isset($find[1]) && $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
}
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