ParsesCountries   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidCountryCode() 0 12 2
A parseCountries() 0 10 2
1
<?php namespace Propaganistas\LaravelPhone\Traits;
2
3
use Illuminate\Support\Collection;
4
use League\ISO3166\ISO3166;
5
6
trait ParsesCountries
7
{
8
    /**
9
     * Determine whether the given country code is valid.
10
     *
11
     * @param string $country
12
     * @return bool
13
     */
14 126
    public static function isValidCountryCode($country)
15
    {
16 126
    	$iso3166 = new ISO3166;
17
18
    	try {
19 126
    		$iso3166->alpha2($country);
20
21 114
    		return true;
22 51
    	} catch (\Exception $e) {
23 51
    		return false;
24
    	}
25
    }
26
27
    /**
28
     * Parse the provided phone countries to a valid array.
29
     *
30
     * @param string|array $countries
31
     * @return array
32
     */
33 120
    protected function parseCountries($countries)
34
    {
35 120
        return Collection::make(is_array($countries) ? $countries : func_get_args())
36
                         ->map(function ($country) {
37 117
                             return strtoupper($country);
38 120
                         })
39
                         ->filter(function ($value) {
40 117
                             return static::isValidCountryCode($value);
41 120
                         })->toArray();
42
    }
43
}