|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Shared Kernel library. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016-present LIN3S <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace LIN3S\SharedKernel\Domain\Model\Phone; |
|
15
|
|
|
|
|
16
|
|
|
use libphonenumber\NumberParseException; |
|
17
|
|
|
use libphonenumber\PhoneNumber; |
|
18
|
|
|
use libphonenumber\PhoneNumberFormat; |
|
19
|
|
|
use libphonenumber\PhoneNumberUtil; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Beñat Espiña <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class Phone |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
private $phone; |
|
27
|
|
|
|
|
28
|
|
|
public static function fromInternational($phone) : self |
|
29
|
|
|
{ |
|
30
|
|
|
return new self($phone); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function fromRegion($region, $phone) : self |
|
34
|
|
|
{ |
|
35
|
|
|
return new self($phone, $region); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public static function fromSpain($phone) : self |
|
39
|
|
|
{ |
|
40
|
|
|
return new self($phone, 'ES'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function __construct($phone, string $region = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->setPhone($phone, $region); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function equals(self $phone) : bool |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->phone() === $phone->phone(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function phone() : string |
|
54
|
|
|
{ |
|
55
|
|
|
return PhoneNumberUtil::getInstance()->format($this->phone, PhoneNumberFormat::E164); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function nationalPhone() : string |
|
59
|
|
|
{ |
|
60
|
|
|
return PhoneNumberUtil::getInstance()->getNationalSignificantNumber($this->phone); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function phoneCallingFrom($region) : string |
|
64
|
|
|
{ |
|
65
|
|
|
return PhoneNumberUtil::getInstance()->formatOutOfCountryCallingNumber($this->phone, $region); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function __toString() : string |
|
69
|
|
|
{ |
|
70
|
|
|
return (string) $this->phone(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function setPhone($phone, ?string $region) : void |
|
74
|
|
|
{ |
|
75
|
|
|
try { |
|
76
|
|
|
$phone = PhoneNumberUtil::getInstance()->parse($phone, $region); |
|
77
|
|
|
$this->checkIsValidNumber($phone); |
|
78
|
|
|
$this->phone = $phone; |
|
79
|
|
|
} catch (NumberParseException $exception) { |
|
80
|
|
|
throw new PhoneInvalidFormatException( |
|
81
|
|
|
$exception->getMessage() |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function checkIsValidNumber(PhoneNumber $phone) : void |
|
87
|
|
|
{ |
|
88
|
|
|
if (!PhoneNumberUtil::getInstance()->isValidNumber($phone)) { |
|
89
|
|
|
throw new PhoneInvalidFormatException(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|