Completed
Push — master ( efafaa...1ce8a3 )
by Avtandil
05:26
created

Str::convertToUtf8()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 15
ccs 0
cts 7
cp 0
crap 6
rs 9.7666
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
    public static function addZeros(string $value, int $finalLength = 2, string $dir = 'left'): string
35
    {
36
        $length = self::length($value);
37
        if ($length >= $finalLength) {
38
            return $value;
39
        }
40
        $diff = $finalLength - $length;
41
        $value = $dir === 'left' ? str_repeat('0', $diff) . $value : $value . str_repeat('0', $diff);
42
43
        return $value;
44
    }
45
46
    public static function formatBalance(?int $amount = 0, int $d = 2): string
47
    {
48
        //$amount = str_replace([',', ' '], ['.', ''], $amount);
49
        $amount = (float) $amount;
50
        $amount /= 100;
51
        $amount = number_format($amount, $d, '.', '');
52
53
        return $amount;
54
    }
55
56
    public static function snakeCaseToCamelCase(string $string): string
57
    {
58
        $str = str_replace('_', '', ucwords($string, '_'));
59
60
        return $str;
61
    }
62
63
    public static function camelCaseToSnakeCase(string $string): string
64
    {
65
        $string = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $string));
66
67
        return $string;
68
    }
69
70
    public static function convertSpacesToDashes(string $string): string
71
    {
72
        $string = str_replace(' ', '-', $string);
73
74
        return $string;
75
    }
76
77
    public static function substrReplace(string $string, string $replacement, int $start, ?int $length = null): string
78
    {
79
        $strLength = self::length($string);
80
81
        if ($start < 0) {
82
            $start = max(0, $strLength + $start);
83
        } elseif ($start > $strLength) {
84
            $start = $strLength;
85
        }
86
87
        if ($length < 0) {
88
            $length = max(0, $strLength - $start + $length);
89
        } elseif ((is_null($length) === true) || ($length > $strLength)) {
90
            $length = $strLength;
91
        }
92
93
        if (($start + $length) > $strLength) {
94
            $length = $strLength - $start;
95
        }
96
97
        return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $strLength - $start - $length);
98
    }
99
100
    public static function limitMiddle(string $value, int $limit = 100, string $separator = '...'): string
101
    {
102
        $length = self::length($value);
103
104
        if ($length <= $limit) {
105
            return $value;
106
        }
107
108
        return self::substrReplace($value, $separator, $limit / 2, $length - $limit);
109
    }
110
111
    public static function toDotNotation(string $value): string
112
    {
113
        $value = trim($value);
114
        $value = preg_replace('/\[(.+)\]/U', '.$1', $value);
115
116
        return $value;
117
    }
118
119
    /**
120
     * @param mixed $data
121
     * @return string
122
     */
123
    public static function hash($data): string
124
    {
125
        if (is_array($data)) {
126
            sort($data);
127
            $data = serialize($data);
128
        }
129
130
        if (is_object($data)) {
131
            $data = serialize($data);
132
        }
133
134
        if (is_null($data)) {
135
            $data = 'NULL';
136
        }
137
138
        return sha1((string) $data);
139
    }
140
141
    public static function convertToUtf8(string $string): string
142
    {
143
        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
        $string = Encoding::toUTF8($string);
149
150
        $string = stripslashes(trim($string));
151
152
        $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
153
154
        return $string;
155
    }
156
}
157