|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magium\Extractors; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
abstract class AbstractAddressExtractor extends AbstractExtractor |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
protected $name; |
|
10
|
|
|
protected $business; |
|
11
|
|
|
protected $street1; |
|
12
|
|
|
protected $street2; |
|
13
|
|
|
protected $city; |
|
14
|
|
|
protected $region; |
|
15
|
|
|
protected $postCode; |
|
16
|
|
|
protected $country; |
|
17
|
|
|
protected $phone; |
|
18
|
|
|
protected $fax; |
|
19
|
|
|
|
|
20
|
|
|
protected $remainingRows; |
|
21
|
|
|
|
|
22
|
|
|
abstract public function getBaseXpath(); |
|
23
|
|
|
|
|
24
|
|
|
public function extract() |
|
25
|
|
|
{ |
|
26
|
|
|
$addressElement = $this->webDriver->byXpath($this->getBaseXpath()); |
|
27
|
|
|
$text = $addressElement->getText(); |
|
28
|
|
|
$rows = explode("\n", $text); |
|
29
|
|
|
|
|
30
|
|
|
$rows = array_reverse($rows); |
|
31
|
|
|
$this->name = trim(array_pop($rows)); |
|
32
|
|
|
while (count($rows) > 0) { |
|
33
|
|
|
$row = array_shift($rows); |
|
34
|
|
|
if (strpos($row, 'T: ') ===0) { |
|
35
|
|
|
$this->phone = trim(substr($row, 3)); |
|
36
|
|
|
continue; |
|
37
|
|
|
} else if (strpos($row, 'F: ') ===0) { |
|
38
|
|
|
$this->fax = trim(substr($row, 3)); |
|
39
|
|
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
if ($this->country === null) { |
|
42
|
|
|
$this->country = trim($row); |
|
43
|
|
|
continue; |
|
44
|
|
|
} |
|
45
|
|
|
if ($this->postCode === null) { |
|
46
|
|
|
$parts = explode(',', $row); |
|
47
|
|
|
$this->postCode = trim(array_pop($parts)); |
|
48
|
|
|
$this->city = trim(array_shift($parts)); |
|
49
|
|
|
if (count($parts) > 0) { |
|
50
|
|
|
$this->region = trim(array_shift($parts)); |
|
51
|
|
|
} |
|
52
|
|
|
break; // After this point we have to figure out business, st1 and st2; two of which are not required |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
// Easy |
|
57
|
|
|
if (count($rows) == 3) { |
|
58
|
|
|
$this->street2 = trim(array_shift($rows)); |
|
59
|
|
|
$this->street1 = trim(array_shift($rows)); |
|
60
|
|
|
$this->business = trim(array_shift($rows)); |
|
61
|
|
|
//Easy |
|
62
|
|
|
}else if (count($rows) == 1) { |
|
63
|
|
|
$this->street1 = trim(array_shift($rows)); |
|
64
|
|
|
|
|
65
|
|
|
// Not so easy |
|
66
|
|
|
} else { |
|
67
|
|
|
$stOrBus = trim(array_shift($rows)); |
|
68
|
|
|
if (preg_match('/^\d+ /', $stOrBus)) { // Good chance of street 1 (could be a business, though) |
|
69
|
|
|
|
|
70
|
|
|
$this->street1 = $stOrBus; |
|
71
|
|
|
$this->business = trim(array_shift($rows)); |
|
72
|
|
|
} else { |
|
73
|
|
|
$this->street1 =trim(array_shift($rows)); |
|
74
|
|
|
$this->street2 = $stOrBus; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
$this->remainingRows = $rows; |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getRemainingRows() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->remainingRows; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getName() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->name; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return mixed |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getBusiness() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->business; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return mixed |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getStreet1() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->street1; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return mixed |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getStreet2() |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->street2; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return mixed |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getCity() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->city; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return mixed |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getRegion() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->region; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return mixed |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getPostCode() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->postCode; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return mixed |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getCountry() |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->country; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return mixed |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getPhone() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->phone; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return mixed |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getFax() |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->fax; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
} |