Functions::fixArray()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 1
nc 6
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SimplePHP\Root;
4
5
/**
6
 * Trait Functions
7
 * @package NicollasSilva/SimplePHP
8
 */
9
trait Functions {
10
    /**
11
     * @param string $hash
12
     * @param array $options
13
     * @return null|string
14
     */
15
    public function hashFormat(String $hash, Array $options = ['cost' => 15]): ?String
16
    {
17
        return password_hash($hash, PASSWORD_BCRYPT, $options);
18
    }
19
20
    /**
21
     * @param string $text
22
     * @return null|string
23
     */
24
    public function urlFormat(String $text): ?String
25
    {
26
        $text = preg_replace('~[^\pL\d]+~u', '-', $text);
27
        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
28
        $text = preg_replace('~[^-\w]+~', '', trim($text, '-'));
29
        $text = preg_replace('~-+~', '-', strtolower($text));
30
        if(empty($text)) { return null; }
31
        return $text;
32
    }
33
34
    /**
35
     * @param string $email
36
     * @return bool
37
     */
38
    public function validateEmail(String $email): bool
39
    {
40
        return !!preg_match("/\S{4,}@\w{3,}\.\w{2,6}(\.\w{2})?/", $email);
41
    }
42
43
    /**
44
     * @param string $password
45
     * @return bool
46
     */
47
    public function validatePassword(String $pass): bool
48
    {
49
        return !!preg_match("/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@#$%!^&*]).{6,20}$/", $pass);
50
    }
51
52
    /**
53
     * @param string $phone
54
     * @return bool
55
     */
56
    public function validatePhone(String $phone): bool
57
    {
58
        return !!preg_match("/(\(\d{2}\)\s?)?\d{4,5}-?\d{4}/", $phone);
59
    }
60
61
    /**
62
     * @param int $size
63
     * @return string
64
     */
65
    public function aleatoryToken(int $size): String
66
    {
67
        $random = str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
68
        $final = substr($random, 0, $size);
69
        return $final;
70
    }
71
72
    /**
73
     * @param $array
74
     * @return bool|null|array
75
     */
76
    public function fixArray($array, Bool $always = true)
77
    {
78
        return is_array($array) && !isset($array[0]) && $always ? [$array] : $array;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getAdressIP() {
85
        if(array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
86
            if(strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') > 0) {
87
                $addr = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']);
88
                return trim($addr[0]);
89
            } else {
90
                return $_SERVER['HTTP_X_FORWARDED_FOR'];
91
            }
92
        }
93
        else {
94
            return $_SERVER['REMOTE_ADDR'];
95
        }
96
    }
97
}