Completed
Push — master ( 1ce8a3...77bf97 )
by Avtandil
02:04
created

Str::convertSpacesToDashes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\Support;
6
7
use ForceUTF8\Encoding;
8
use Illuminate\Support\Str as BaseStr;
9
use RuntimeException;
10
11
use function class_exists;
12
use function html_entity_decode;
13
use function is_array;
14
use function is_null;
15
use function is_object;
16
use function max;
17
use function mb_substr;
18
use function number_format;
19
use function preg_replace;
20
use function serialize;
21
use function sha1;
22
use function sort;
23
use function str_repeat;
24
use function str_replace;
25
use function stripslashes;
26
use function strtolower;
27
use function trim;
28
use function ucwords;
29
30
use const ENT_QUOTES;
31
32
class Str extends BaseStr
33
{
34 1
    public static function addZeros(string $value, int $finalLength = 2, string $dir = 'left'): string
35
    {
36 1
        $length = self::length($value);
37 1
        if ($length >= $finalLength) {
38
            return $value;
39
        }
40 1
        $diff = $finalLength - $length;
41 1
        $value = $dir === 'left' ? str_repeat('0', $diff) . $value : $value . str_repeat('0', $diff);
42
43 1
        return $value;
44
    }
45
46 1
    public static function formatBalance(?int $amount = 0, int $d = 2): string
47
    {
48
        //$amount = str_replace([',', ' '], ['.', ''], $amount);
49 1
        $amount = (float) $amount;
50 1
        $amount /= 100;
51 1
        $amount = number_format($amount, $d, '.', '');
52
53 1
        return $amount;
54
    }
55
56 1
    public static function snakeCaseToCamelCase(string $string): string
57
    {
58 1
        $str = str_replace('_', '', ucwords($string, '_'));
59
60 1
        return $str;
61
    }
62
63 1
    public static function camelCaseToSnakeCase(string $string): string
64
    {
65 1
        $string = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $string));
66
67 1
        return $string;
68
    }
69
70 1
    public static function convertSpacesToDashes(string $string): string
71
    {
72 1
        $string = str_replace(' ', '-', $string);
73
74 1
        return $string;
75
    }
76
77 1
    public static function substrReplace(string $string, string $replacement, int $start, ?int $length = null): string
78
    {
79 1
        $strLength = self::length($string);
80
81 1
        if ($start < 0) {
82
            $start = max(0, $strLength + $start);
83 1
        } elseif ($start > $strLength) {
84
            $start = $strLength;
85
        }
86
87 1
        if ($length < 0) {
88
            $length = max(0, $strLength - $start + $length);
89 1
        } elseif ((is_null($length) === true) || ($length > $strLength)) {
90
            $length = $strLength;
91
        }
92
93 1
        if (($start + $length) > $strLength) {
94
            $length = $strLength - $start;
95
        }
96
97 1
        return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $strLength - $start - $length);
98
    }
99
100 1
    public static function limitMiddle(string $value, int $limit = 100, string $separator = '...'): string
101
    {
102 1
        $length = self::length($value);
103
104 1
        if ($length <= $limit) {
105
            return $value;
106
        }
107
108 1
        return self::substrReplace($value, $separator, $limit / 2, $length - $limit);
109
    }
110
111 1
    public static function toDotNotation(string $value): string
112
    {
113 1
        $value = trim($value);
114 1
        $value = preg_replace('/\[(.+)\]/U', '.$1', $value);
115
116 1
        return $value;
117
    }
118
119
    /**
120
     * @param mixed $data
121
     * @return string
122
     */
123 1
    public static function hash($data): string
124
    {
125 1
        if (is_array($data)) {
126 1
            sort($data);
127 1
            $data = serialize($data);
128
        }
129
130 1
        if (is_object($data)) {
131 1
            $data = serialize($data);
132
        }
133
134 1
        if (is_null($data)) {
135 1
            $data = 'NULL';
136
        }
137
138 1
        return sha1((string) $data);
139
    }
140
141 1
    public static function convertToUtf8(string $string): string
142
    {
143 1
        if (! class_exists(Encoding::class)) {
144
            throw new RuntimeException('To use this method, package "neitanod/forceutf8" should be installed!');
145
        }
146
147
        /** @link https://github.com/neitanod/forceutf8 */
148 1
        $string = Encoding::toUTF8($string);
149
150 1
        $string = stripslashes(trim($string));
151
152 1
        $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
153
154 1
        return $string;
155
    }
156
}
157