Test Failed
Push — master ( f47101...348e9e )
by Ricardo
02:03
created

Phone::toDatabase()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 1
dl 0
loc 13
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Validate;
4
5
use Validate\Traits\FakeNameTrait;
6
7
class Phone implements \Validate\Contracts\Validate
8
{
9
    use FakeNameTrait;
10
11
    public static function toDatabase(string $phone)
12
    {
13
        $phone = preg_replace('/[^0-9]/', '', $phone);
14
        if (substr((string) $phone,0,2)=='55') {
15
            return $phone;
16
        }
17
        if (empty($phone) || $phone=='55') {
18
            return null;
19
        }
20
        if (strlen($phone)>11) {
21
            return $phone;
22
        }
23
        return '55'.$phone;
24
    }
25
26
    public static function toUser($phone)
27
    {
28
        return $phone;
29
    }
30
31
    public static function validate($phoneNumber)
32
    {
33
        $phone = self::break($phoneNumber);
34
35
        if ((int) $phone['country'] === 0 ) {
36
            return false;
37
        }
38
39
        if ((int) $phone['region'] === 0 ) {
40
            return false;
41
        }
42
43
        if ((int) $phone['number'] === 0 ) {
44
            return false;
45
        }
46
47
        if (strlen(static::toDatabase($phoneNumber)) < 12 || strlen(static::toDatabase($phoneNumber)) > 13) {
48
            return false;
49
        }
50
51
        return true;
52
    }
53
54
    public static function break(string $phone)
55
    {
56
        $phone = static::toDatabase($phone);
57
        $data['country'] = '55';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
58
        $data['region'] = '61';
59
60
        if (strlen($phone)>=12) {
61
            $data['country'] = substr((string) $phone, 0, 2);
62
            $phone = substr((string) $phone, 2, strlen($phone)-2);
63
        }
64
65
        if (strlen($phone)>=10) {
66
            $data['region'] = substr((string) $phone, 0, 2);
67
            $phone = substr((string) $phone, 2, strlen($phone)-2);
68
        }
69
70
        $data['number'] = $phone;
71
        return $data;
72
    }
73
74
    public static function isSame(string $to, string $from)
75
    {
76
        return (self::toDatabase($to)===self::toDatabase($from));
77
    }
78
79
}
80