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\Postcode; |
11
|
|
|
|
12
|
|
|
class Parser implements ParserInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Regular expression pattern for Outward code |
16
|
|
|
*/ |
17
|
|
|
const OUTWARD = "/\d[A-Za-z]{1,2}$/i"; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Regular expression pattern for Inward code |
22
|
|
|
*/ |
23
|
|
|
const INWARD = "/\d[A-Za-z]{2}$/i"; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Regular expression pattern for Area code |
28
|
|
|
*/ |
29
|
|
|
const AREA = "/^[A-Za-z]{1,2}/i"; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Regular expression pattern for Sector code |
34
|
|
|
*/ |
35
|
|
|
const SECTOR = "/^[A-Za-z]{1,2}\d[A-Za-z\d]?\s*\d/i"; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Regular expression pattern for Unit code |
40
|
|
|
*/ |
41
|
|
|
const UNIT = "/[A-Za-z]{2}$/i"; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Regular expression pattern for District code |
46
|
|
|
*/ |
47
|
|
|
const DISTRICT = "/^([A-Za-z]{1,2}\d)([A-Za-z])$/i"; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Regular expression pattern for Subdistrict code |
52
|
|
|
*/ |
53
|
|
|
const SUBDISTRICT = "/^([A-Za-z]{1,2}\d)([A-Za-z])$/i"; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Postcode $postcode |
58
|
|
|
*/ |
59
|
|
|
private $postcode; |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Constructor |
64
|
|
|
* |
65
|
|
|
* @param Postcode $postcode |
66
|
|
|
*/ |
67
|
|
|
public function __construct(Postcode $postcode) |
68
|
|
|
{ |
69
|
|
|
$this->postcode = (string)$postcode; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string $outward |
74
|
|
|
*/ |
75
|
|
|
public function outward() |
76
|
|
|
{ |
77
|
|
|
return \trim(\preg_replace(self::OUTWARD, "", $this->postcode)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string $inward |
82
|
|
|
*/ |
83
|
|
|
public function inward() |
84
|
|
|
{ |
85
|
|
|
return (\preg_match(self::INWARD, $this->postcode, $matches)) ? $matches[0] : ""; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string $area |
91
|
|
|
*/ |
92
|
|
|
public function area() |
93
|
|
|
{ |
94
|
|
|
return (\preg_match(self::AREA, $this->postcode, $matches)) ? $matches[0] : ""; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string $district |
99
|
|
|
*/ |
100
|
|
|
public function district() |
101
|
|
|
{ |
102
|
|
|
return (\preg_match(self::DISTRICT, $this->outward(), $matches)) ? $matches[1] : ""; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Subdistrict code |
107
|
|
|
* |
108
|
|
|
* @return string Example: "AA9A" |
109
|
|
|
*/ |
110
|
|
|
public function subdistrict() |
111
|
|
|
{ |
112
|
|
|
return (\preg_match(self::REGEXP_SUBDISTRICT, $this->outward(), $matches)) ? $matches[0] : ""; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string $sector |
117
|
|
|
*/ |
118
|
|
|
public function sector() |
119
|
|
|
{ |
120
|
|
|
return (\preg_match(self::SECTOR, $this->postcode, $matches)) ? $matches[0] : ""; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string $unit |
126
|
|
|
*/ |
127
|
|
|
public function unit() |
128
|
|
|
{ |
129
|
|
|
return (\preg_match(self::UNIT, $this->postcode, $matches)) ? $matches[0] : ""; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function parse() |
136
|
|
|
{ |
137
|
|
|
return []; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|