Passed
Push — master ( 4dbea6...eb47cb )
by Hashem
02:18
created

Helper::mbJsonEncode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Shetabit\Helper;
4
5
use Verta;
0 ignored issues
show
Bug introduced by
The type Verta was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
class Helper
8
{
9
    /**
10
     * Generate random password.
11
     *
12
     * @param int    $length
13
     * @param string $availableSets
14
     *
15
     * @return string
16
     */
17
    public function randomPassword(int $length = 9, string $availableSets = 'luds')
18
    {
19
        $sets = [];
20
        if (strpos($availableSets, 'l') !== false) {
21
            $sets[] = 'abcdefghjkmnpqrstuvwxyz';
22
        }
23
        if (strpos($availableSets, 'u') !== false) {
24
            $sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ';
25
        }
26
        if (strpos($availableSets, 'd') !== false) {
27
            $sets[] = '23456789';
28
        }
29
        if (strpos($availableSets, 's') !== false) {
30
            $sets[] = '!@#$%&*?';
31
        }
32
33
        $all = '';
34
        $password = '';
35
        foreach ($sets as $set) {
36
            $password .= $set[array_rand(str_split($set))];
37
            $all .= $set;
38
        }
39
40
        $allArray = str_split($all);
41
        for ($i = 0; $i < $length - count($sets); $i++) {
42
            $password .= $allArray[array_rand($allArray)];
43
        }
44
        $password = str_shuffle($password);
45
46
        return $password;
47
    }
48
49
    /**
50
     * Generate persian slug.
51
     *
52
     * @param string $string
53
     * @param string $separator
54
     *
55
     * @return false|mixed|string|string[]|null
56
     */
57
    public function persianSlug(string $string, string $separator = '-')
58
    {
59
        $_transliteration = [
60
            '/ä|æ|ǽ/'                         => 'ae',
61
            '/ö|œ/'                           => 'oe',
62
            '/ü/'                             => 'ue',
63
            '/Ä/'                             => 'Ae',
64
            '/Ü/'                             => 'Ue',
65
            '/Ö/'                             => 'Oe',
66
            '/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/'           => 'A',
67
            '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/'         => 'a',
68
            '/Ç|Ć|Ĉ|Ċ|Č/'                     => 'C',
69
            '/ç|ć|ĉ|ċ|č/'                     => 'c',
70
            '/Ð|Ď|Đ/'                         => 'D',
71
            '/ð|ď|đ/'                         => 'd',
72
            '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/'             => 'E',
73
            '/è|é|ê|ë|ē|ĕ|ė|ę|ě/'             => 'e',
74
            '/Ĝ|Ğ|Ġ|Ģ/'                       => 'G',
75
            '/ĝ|ğ|ġ|ģ/'                       => 'g',
76
            '/Ĥ|Ħ/'                           => 'H',
77
            '/ĥ|ħ/'                           => 'h',
78
            '/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ/'           => 'I',
79
            '/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı/'           => 'i',
80
            '/Ĵ/'                             => 'J',
81
            '/ĵ/'                             => 'j',
82
            '/Ķ/'                             => 'K',
83
            '/ķ/'                             => 'k',
84
            '/Ĺ|Ļ|Ľ|Ŀ|Ł/'                     => 'L',
85
            '/ĺ|ļ|ľ|ŀ|ł/'                     => 'l',
86
            '/Ñ|Ń|Ņ|Ň/'                       => 'N',
87
            '/ñ|ń|ņ|ň|ʼn/'                     => 'n',
88
            '/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/'         => 'O',
89
            '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/'       => 'o',
90
            '/Ŕ|Ŗ|Ř/'                         => 'R',
91
            '/ŕ|ŗ|ř/'                         => 'r',
92
            '/Ś|Ŝ|Ş|Ș|Š/'                     => 'S',
93
            '/ś|ŝ|ş|ș|š|ſ/'                   => 's',
94
            '/Ţ|Ț|Ť|Ŧ/'                       => 'T',
95
            '/ţ|ț|ť|ŧ/'                       => 't',
96
            '/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U',
97
            '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u',
98
            '/Ý|Ÿ|Ŷ/'                         => 'Y',
99
            '/ý|ÿ|ŷ/'                         => 'y',
100
            '/Ŵ/'                             => 'W',
101
            '/ŵ/'                             => 'w',
102
            '/Ź|Ż|Ž/'                         => 'Z',
103
            '/ź|ż|ž/'                         => 'z',
104
            '/Æ|Ǽ/'                           => 'AE',
105
            '/ß/'                             => 'ss',
106
            '/IJ/'                             => 'IJ',
107
            '/ij/'                             => 'ij',
108
            '/Œ/'                             => 'OE',
109
            '/ƒ/'                             => 'f',
110
        ];
111
        $quotedReplacement = preg_quote($separator, '/');
112
        $merge = [
113
            '/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
114
            '/[\s\p{Zs}]+/mu'                                     => $separator,
115
            sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
116
        ];
117
        $map = $_transliteration + $merge;
118
        unset($_transliteration);
119
120
        return mb_strtolower(preg_replace(array_keys($map), array_values($map), $string));
121
    }
122
123
    /**
124
     * Convert all Persian(Farsi) numbers to English.
125
     *
126
     * @param string $number
127
     *
128
     * @return mixed
129
     */
130
    public function faToEnNums(string $number)
131
    {
132
        $persianDigits1 = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
133
        $persianDigits2 = ['٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١', '٠'];
134
        $allPersianDigits = array_merge($persianDigits1, $persianDigits2);
135
        $replaces = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0'];
136
137
        return str_replace($allPersianDigits, $replaces, $number);
138
    }
139
140
    /**
141
     * Remove comma's from value.
142
     *
143
     * @param string $value
144
     *
145
     * @return string
146
     */
147
    public function removeComma(string $value) : string
148
    {
149
        return str_replace(',', '', $value);
150
    }
151
152
    /**
153
     * Convert jalali date to gregorian date.
154
     *
155
     * @param string $jDate
156
     *
157
     * @return null|string
158
     */
159
    public function toGregorian(string $jDate)
160
    {
161
        $output = null;
162
        $pattern = '#^(\\d{4})/(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])$#';
163
164
        if (preg_match($pattern, $jDate)) {
165
            $jDateArray = explode('/', $jDate);
166
            $dateArray = Verta::getGregorian(
167
                $jDateArray[0],
168
                $jDateArray[1],
169
                $jDateArray[2]
170
            );
171
            $output = implode('/', $dateArray);
172
        }
173
174
        return $output;
175
    }
176
177
    /**
178
     * json_encode() for multibyte characters
179
     *
180
     * @param string $input
181
     * @return string|string[]|null
182
     */
183
    public function mbJsonEncode(string $input)
184
    {
185
        return preg_replace_callback('/\\\\u([0-9a-zA-Z]{4})/', function ($matches) {
186
            return mb_convert_encoding(pack('H*',$matches[1]), 'UTF-8','UTF-16');
187
        },
188
            json_encode($input, JSON_UNESCAPED_UNICODE)
189
        );
190
    }
191
}
192