1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupBundle\Value\PhoneNumber; |
20
|
|
|
|
21
|
|
|
use Surfnet\StepupBundle\Exception\InvalidArgumentException; |
22
|
|
|
use Surfnet\StepupBundle\Value\Exception\InvalidPhoneNumberFormatException; |
23
|
|
|
|
24
|
|
|
class InternationalPhoneNumber |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var CountryCode |
28
|
|
|
*/ |
29
|
|
|
private $countryCode; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var PhoneNumber |
33
|
|
|
*/ |
34
|
|
|
private $phoneNumber; |
35
|
|
|
|
36
|
|
|
public function __construct(CountryCode $countryCode, PhoneNumber $number) |
37
|
|
|
{ |
38
|
|
|
$this->countryCode = $countryCode; |
39
|
|
|
$this->phoneNumber = $number; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $string a well formatted "+{CountryCode} (0) {PhoneNumber}" international phone number |
44
|
|
|
* @return InternationalPhoneNumber |
45
|
|
|
*/ |
46
|
|
|
public static function fromStringFormat($string) |
47
|
|
|
{ |
48
|
|
|
if (!is_string($string)) { |
49
|
|
|
throw InvalidArgumentException::invalidType('string', 'string', $string); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$matches = []; |
53
|
|
|
if (!preg_match('~^\+([^\(]+)\(0\)\s{1}([\d]+)$~', $string, $matches)) { |
54
|
|
|
throw new InvalidPhoneNumberFormatException( |
55
|
|
|
'In order to create the phone number from string, it must have the format "+31 (0) 614696076", formal: ' |
56
|
|
|
. '"+{CountryCode} (0) {PhoneNumber}"' |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$countryCode = str_replace(' ', '', $matches[1]); |
61
|
|
|
$phoneNumber = $matches[2]; |
62
|
|
|
|
63
|
|
|
return new self(new CountryCode($countryCode), new PhoneNumber($phoneNumber)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @see http://en.wikipedia.org/wiki/MSISDN#MSISDN_Format |
68
|
|
|
* @see https://www.messagebird.com/developers#messaging-send |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function toMSISDN() |
73
|
|
|
{ |
74
|
|
|
return $this->countryCode->getCountryCode() . $this->phoneNumber->formatAsMsisdnPart(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param InternationalPhoneNumber $other |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function equals(InternationalPhoneNumber $other) |
82
|
|
|
{ |
83
|
|
|
return $this->countryCode->equals($other->countryCode) |
84
|
|
|
&& $this->phoneNumber->equals($other->phoneNumber); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function __toString() |
88
|
|
|
{ |
89
|
|
|
return $this->countryCode . ' ' . $this->phoneNumber; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|