1 | <?php namespace Propaganistas\LaravelPhone\Traits; |
||
8 | trait ParsesTypes |
||
9 | { |
||
10 | /** |
||
11 | * Array of available phone types. |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | protected static $types; |
||
16 | |||
17 | /** |
||
18 | * Determine whether the given type is valid. |
||
19 | * |
||
20 | * @param string $type |
||
21 | * @return bool |
||
22 | */ |
||
23 | 26 | public static function isValidType($type) |
|
27 | |||
28 | /** |
||
29 | * Parse a phone type into constant's value. |
||
30 | * |
||
31 | * @param string|array $types |
||
32 | * @return array |
||
33 | */ |
||
34 | 47 | protected static function parseTypes($types) |
|
35 | { |
||
36 | 47 | static::loadTypes(); |
|
37 | |||
38 | 47 | return Collection::make(is_array($types) ? $types : func_get_args()) |
|
39 | 47 | ->map(function ($type) { |
|
40 | // If the type equals a constant's value, just return it. |
||
41 | 47 | if (is_numeric($type) && in_array($type, static::$types)) { |
|
42 | 20 | return (int) $type; |
|
43 | } |
||
44 | |||
45 | // Otherwise we'll assume the type is the constant's name. |
||
46 | 44 | return Arr::get(static::$types, strtoupper($type)); |
|
47 | 47 | }) |
|
48 | 47 | ->reject(function ($value) { |
|
49 | 47 | return is_null($value) || $value === false; |
|
50 | 47 | })->toArray(); |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * Parse a phone type into its string representation. |
||
55 | * |
||
56 | * @param string|array $types |
||
57 | * @return array |
||
58 | */ |
||
59 | 3 | protected static function parseTypesAsStrings($types) |
|
70 | |||
71 | /** |
||
72 | * Load all available formats once. |
||
73 | */ |
||
74 | 47 | private static function loadTypes() |
|
80 | } |