Passed
Push — master ( 699a02...c753a9 )
by Nícollas
01:12
created

Functions   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 61
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A validatePhone() 0 3 1
A validatePassword() 0 3 1
A hashFormat() 0 3 1
A urlFormat() 0 8 2
A aleatoryToken() 0 5 1
A validateEmail() 0 3 1
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("0123456789abcdefghijklmnopqrstuvwxyz+-&%#@()ABCDEFGHIJKLMNOPQRSTUVWXYZ");
68
        $final = substr($random, 0, $size);
69
        return $final;
70
    }
71
}