ParsesFormats::loadFormats()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace Propaganistas\LaravelPhone\Traits;
2
3
use Illuminate\Support\Arr;
4
use libphonenumber\PhoneNumberFormat;
5
use ReflectionClass;
6
7
trait ParsesFormats
8
{
9
    /**
10
     * Array of available phone formats.
11
     *
12
     * @var array
13
     */
14
    protected static $formats;
15
16
    /**
17
     * Determine whether the given format is valid.
18
     *
19
     * @param string $format
20
     * @return bool
21
     */
22 3
    public static function isValidFormat($format)
23
    {
24 3
        return ! is_null(static::parseFormat($format));
25
    }
26
27
    /**
28
     * Parse a phone format.
29
     *
30
     * @param string $format
31
     * @return string
32
     */
33 63
    protected static function parseFormat($format)
34
    {
35 63
        static::loadFormats();
36
37
        // If the format equals a constant's value, just return it.
38 63
        if (in_array($format, static::$formats, true)) {
39 60
            return $format;
40
        }
41
42
        // Otherwise we'll assume the format is the constant's name.
43 9
        return Arr::get(static::$formats, strtoupper($format));
44
    }
45
46
    /**
47
     * Load all available formats once.
48
     */
49 63
    private static function loadFormats()
50
    {
51 63
        if (! static::$formats) {
52 3
            static::$formats = with(new ReflectionClass(PhoneNumberFormat::class))->getConstants();
53
        }
54
    }
55
}