1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* UK Postcode Parser |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Vasil Dakov <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
7
|
|
|
*/ |
8
|
|
|
namespace VasilDakov\Postcode; |
9
|
|
|
|
10
|
|
|
use VasilDakov\Postcode\Validator; |
11
|
|
|
use VasilDakov\Postcode\Exception; |
12
|
|
|
|
13
|
|
|
class Parser implements ParserInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Regular expression pattern for Outward code |
17
|
|
|
*/ |
18
|
|
|
const OUTWARD = "/\d[A-Za-z]{1,2}$/i"; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Regular expression pattern for Inward code |
23
|
|
|
*/ |
24
|
|
|
const INWARD = "/\d[A-Za-z]{2}$/i"; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Regular expression pattern for Area code |
29
|
|
|
*/ |
30
|
|
|
const AREA = "/^[A-Za-z]{1,2}/i"; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Regular expression pattern for Sector code |
35
|
|
|
*/ |
36
|
|
|
const SECTOR = "/^[A-Za-z]{1,2}\d[A-Za-z\d]?\s*\d/i"; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Regular expression pattern for Unit code |
41
|
|
|
*/ |
42
|
|
|
const UNIT = "/[A-Za-z]{2}$/i"; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Regular expression pattern for District code |
47
|
|
|
*/ |
48
|
|
|
const DISTRICT = "/^([A-Za-z]{1,2}\d)([A-Za-z])$/i"; |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Regular expression pattern for Subdistrict code |
53
|
|
|
*/ |
54
|
|
|
const SUBDISTRICT = "/^([A-Za-z]{1,2}\d)([A-Za-z])$/i"; |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* string $postcode |
59
|
|
|
*/ |
60
|
|
|
private $postcode; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Validator $validator |
64
|
|
|
*/ |
65
|
|
|
private $validator; |
66
|
|
|
|
67
|
1 |
|
/** |
68
|
|
|
* Constructor |
69
|
1 |
|
* |
70
|
1 |
|
* @param string $postcode |
71
|
|
|
* @param Validator $validator |
72
|
|
|
*/ |
73
|
|
|
public function __construct($postcode, Validator $validator = null) |
74
|
|
|
{ |
75
|
1 |
|
if (null === $validator) { |
76
|
|
|
$validator = new Validator; |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
$this->validator = $validator; |
80
|
|
|
|
81
|
|
|
if (!$this->validator->isValid($postcode)) { |
82
|
|
|
throw new Exception\InvalidPostcodeException; |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
$this->postcode = $postcode; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string $outward |
90
|
|
|
*/ |
91
|
|
|
public function outward() |
92
|
1 |
|
{ |
93
|
|
|
return \trim(\preg_replace(self::OUTWARD, "", $this->postcode)); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string $inward |
98
|
|
|
*/ |
99
|
|
|
public function inward() |
100
|
1 |
|
{ |
101
|
|
|
return (\preg_match(self::INWARD, $this->postcode, $matches)) ? $matches[0] : ""; |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string $area |
107
|
|
|
*/ |
108
|
|
|
public function area() |
109
|
|
|
{ |
110
|
1 |
|
return (\preg_match(self::AREA, $this->postcode, $matches)) ? $matches[0] : ""; |
111
|
|
|
} |
112
|
1 |
|
|
113
|
|
|
/** |
114
|
|
|
* @return string $district |
115
|
|
|
*/ |
116
|
|
|
public function district() |
117
|
|
|
{ |
118
|
1 |
|
return (\preg_match(self::DISTRICT, $this->outward(), $matches)) ? $matches[1] : ""; |
119
|
|
|
} |
120
|
1 |
|
|
121
|
|
|
/** |
122
|
|
|
* Subdistrict code |
123
|
|
|
* |
124
|
|
|
* @return string Example: "AA9A" |
125
|
|
|
*/ |
126
|
|
|
public function subdistrict() |
127
|
1 |
|
{ |
128
|
|
|
return (\preg_match(self::SUBDISTRICT, $this->outward(), $matches)) ? $matches[0] : ""; |
129
|
1 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string $sector |
133
|
|
|
*/ |
134
|
|
|
public function sector() |
135
|
1 |
|
{ |
136
|
|
|
return (\preg_match(self::SECTOR, $this->postcode, $matches)) ? $matches[0] : ""; |
137
|
|
|
} |
138
|
1 |
|
|
139
|
1 |
|
|
140
|
1 |
|
/** |
141
|
1 |
|
* @return string $unit |
142
|
1 |
|
*/ |
143
|
1 |
|
public function unit() |
144
|
1 |
|
{ |
145
|
1 |
|
return (\preg_match(self::UNIT, $this->postcode, $matches)) ? $matches[0] : ""; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function parse() |
152
|
|
|
{ |
153
|
|
|
return [ |
154
|
|
|
'outward' => $this->outward(), |
155
|
|
|
'inward' => $this->inward(), |
156
|
|
|
'area' => $this->area(), |
157
|
|
|
'district' => $this->district(), |
158
|
|
|
'subdistrict' => $this->subdistrict(), |
159
|
|
|
'sector' => $this->sector(), |
160
|
|
|
'unit' => $this->unit() |
161
|
|
|
]; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|