Passed
Push — master ( 5f3c03...861b1d )
by Hashem
03:37 queued 10s
created

Helper::persianSlug()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 63
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 59
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 63
rs 8.8945

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    /**
11
     * Generate random password.
12
     *
13
     * @param int $length
14
     * @param string $availableSets
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
        {
37
            $password .= $set[array_rand(str_split($set))];
38
            $all .= $set;
39
        }
40
41
        $allArray = str_split($all);
42
        for($i = 0; $i < $length - count($sets); $i++) {
43
            $password .= $allArray[array_rand($allArray)];
44
        }
45
        $password = str_shuffle($password);
46
47
        return $password;
48
    }
49
50
    /**
51
     * Generate persian slug.
52
     *
53
     * @param string $string
54
     * @param string $separator
55
     * @return false|mixed|string|string[]|null
56
     */
57
    public function persianSlug(string $string, string $separator = '-')
58
    {
59
        $_transliteration = array(
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 = array(
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
        return mb_strtolower(preg_replace(array_keys($map), array_values($map), $string));
120
    }
121
122
    /**
123
     * Convert all Persian(Farsi) numbers to English.
124
     *
125
     * @param string $number
126
     * @return mixed
127
     */
128
    public function faToEnNums(string $number)
129
    {
130
        $persianDigits1 = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
131
        $persianDigits2 = ['٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١', '٠'];
132
        $allPersianDigits = array_merge($persianDigits1, $persianDigits2);
133
        $replaces = ['0','1','2','3','4','5','6','7','8','9','9','8','7','6','5','4','3','2','1','0'];
134
135
        return str_replace($allPersianDigits, $replaces , $number);
136
    }
137
138
    /**
139
     * Remove comma's from value.
140
     *
141
     * @param string $value
142
     *
143
     * @return string
144
     */
145
    public function removeComma(string $value) : string
146
    {
147
        return str_replace(',', '', $value);
148
    }
149
150
    /**
151
     * Convert jalali date to gregorian date.
152
     * 
153
     * @param  string $jDate
154
     * @return null|string
155
     */
156
    public function toGregorian(string $jDate)
157
    {
158
        $output = null;
159
        $pattern = '#^(\\d{4})/(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])$#';
160
161
        if (preg_match($pattern, $jDate)) {
162
            $jDateArray = explode('/', $jDate);
163
            $dateArray = Verta::getGregorian(
164
                $jDateArray[0],
165
                $jDateArray[1],
166
                $jDateArray[2]
167
            );
168
            $output = implode('/', $dateArray);
169
        }
170
171
        return $output;
172
    }
173
}
174