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\InvalidCountryCodeFormatException; |
23
|
|
|
use Surfnet\StepupBundle\Value\Exception\UnknownCountryCodeException; |
24
|
|
|
|
25
|
|
|
class CountryCode |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $countryCode; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $countyCode |
34
|
|
|
*/ |
35
|
|
|
public function __construct($countyCode) |
36
|
|
|
{ |
37
|
|
|
if (!is_string($countyCode)) { |
38
|
|
|
throw InvalidArgumentException::invalidType('string', 'countryCodeDefinition', $countyCode); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!preg_match('~^\d+$~', $countyCode)) { |
42
|
|
|
throw new InvalidCountryCodeFormatException($countyCode); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!CountryCodeListing::isValidCountryCode($countyCode)) { |
46
|
|
|
throw UnknownCountryCodeException::unknownCountryCode($countyCode); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->countryCode = $countyCode; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getCountryCode() |
56
|
|
|
{ |
57
|
|
|
return $this->countryCode; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param CountryCode $other |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function equals(CountryCode $other) |
65
|
|
|
{ |
66
|
|
|
return $this->countryCode === $other->countryCode; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function __toString() |
70
|
|
|
{ |
71
|
|
|
$countryCode = $this->getCountryCode(); |
72
|
|
|
|
73
|
|
|
// 4 digits (1234) are split after the first (1 234), same for both Kazakhstan codes after the first digit |
74
|
|
|
if (strlen($countryCode) === 4 || in_array($countryCode, ['77', '76'])) { |
75
|
|
|
$countryCode = substr($countryCode, 0, 1) . ' ' . substr($countryCode, 1); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return '+' . $countryCode; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|