Completed
Push — master ( 77ccc6...f13dc8 )
by Propa
05:09
created

Phone::__toString()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 2
nop 0
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 5
rs 8.8571
c 0
b 0
f 0
1
<?php namespace Propaganistas\LaravelPhone\Rules;
2
3
use libphonenumber\PhoneNumberType;
4
use Propaganistas\LaravelPhone\Traits\ParsesCountries;
5
use Propaganistas\LaravelPhone\Traits\ParsesTypes;
6
7
class Phone
8
{
9
    /**
10
     * The provided phone countries.
11
     *
12
     * @var array
13
     */
14
    protected $countries = [];
15
16
    /**
17
     * The input field name to check for a country value.
18
     *
19
     * @var string
20
     */
21
    protected $countryField;
22
23
    /**
24
     * The provided phone types.
25
     *
26
     * @var array
27
     */
28
    protected $types = [];
29
30
    /**
31
     * Whether the number's country should be auto-detected.
32
     *
33
     * @var bool
34
     */
35
    protected $detect = false;
36
37
    /**
38
     * Whether to allow lenient checks (i.e. landline numbers without area codes).
39
     *
40
     * @var bool
41
     */
42
    protected $lenient = false;
43
44
    /**
45
     * Set the phone countries.
46
     *
47
     * @param string|array $country
48
     * @return $this
49
     */
50 3
    public function country($country)
51
    {
52 3
        $countries = is_array($country) ? $country : func_get_args();
53
54 3
        $this->countries = array_merge($this->countries, $countries);
55
56 3
        return $this;
57
    }
58
59
    /**
60
     * Set the country input field.
61
     *
62
     * @param string $name
63
     * @return $this
64
     */
65 3
    public function countryField($name)
66
    {
67 3
        $this->countryField = $name;
68
69 3
        return $this;
70
    }
71
72
    /**
73
     * Set the phone types.
74
     *
75
     * @param string|array $type
76
     * @return $this
77
     */
78 3
    public function type($type)
79
    {
80 3
        $types = is_array($type) ? $type : func_get_args();
81
82 3
        $this->types = array_merge($this->types, $types);
83
84 3
        return $this;
85
    }
86
87
    /**
88
     * Shortcut method for mobile type restriction.
89
     *
90
     * @return $this
91
     */
92 3
    public function mobile()
93
    {
94 3
        $this->type(PhoneNumberType::MOBILE);
95
96 3
        return $this;
97
    }
98
99
    /**
100
     * Shortcut method for fixed line type restriction.
101
     *
102
     * @return $this
103
     */
104 3
    public function fixedLine()
105
    {
106 3
        $this->type(PhoneNumberType::FIXED_LINE);
107
108 3
        return $this;
109
    }
110
111
    /**
112
     * Enable automatic country detection.
113
     *
114
     * @return $this
115
     */
116 3
    public function detect()
117
    {
118 3
        $this->detect = true;
119
120 3
        return $this;
121
    }
122
123
    /**
124
     * Enable lenient number checking.
125
     *
126
     * @return $this
127
     */
128 3
    public function lenient()
129
    {
130 3
        $this->lenient = true;
131
132 3
        return $this;
133
    }
134
135
    /**
136
     * Convert the rule to a validation string.
137
     *
138
     * @return string
139
     */
140 3
    public function __toString()
141
    {
142 3
        $parameters = implode(',', array_merge(
143 3
            $this->countries,
144 3
            $this->types,
145 3
            ($this->countryField ? [$this->countryField]: []),
146 3
            ($this->detect ? ['AUTO'] : []),
147 3
            ($this->lenient ? ['LENIENT'] : [])
148 1
        ));
149
150 3
        return 'phone' . (! empty($parameters) ? ":$parameters" : '');
151
    }
152
}