|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ADiaz\AML\OpenList\parsers; |
|
4
|
|
|
|
|
5
|
|
|
use ADiaz\AML\OpenList\models\Address; |
|
6
|
|
|
use ADiaz\AML\OpenList\models\Aka; |
|
7
|
|
|
use ADiaz\AML\OpenList\models\Citizenship; |
|
8
|
|
|
use ADiaz\AML\OpenList\models\DateOfBirth; |
|
9
|
|
|
use ADiaz\AML\OpenList\models\Entity; |
|
10
|
|
|
use ADiaz\AML\OpenList\models\Id; |
|
11
|
|
|
use ADiaz\AML\OpenList\models\Nationality; |
|
12
|
|
|
use ADiaz\AML\OpenList\models\PlaceOfBirth; |
|
13
|
|
|
use ADiaz\AML\OpenList\models\Program; |
|
14
|
|
|
use ADiaz\AML\OpenList\models\Vessel; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* This file is part of the OpenList Parser utility. |
|
18
|
|
|
* |
|
19
|
|
|
* @category PHP |
|
20
|
|
|
* |
|
21
|
|
|
* @author Alberto Diaz <[email protected]> |
|
22
|
|
|
* @copyright 2016 Alberto Diaz <[email protected]> |
|
23
|
|
|
* @license This source file is subject to the MIT license that is bundled |
|
24
|
|
|
* |
|
25
|
|
|
* @version Release: @package_version@ |
|
26
|
|
|
* |
|
27
|
|
|
* @link http://tytem.com |
|
28
|
|
|
*/ |
|
29
|
|
|
class Usofac implements ListInterface |
|
30
|
|
|
{ |
|
31
|
|
|
const ID = 'Id'; |
|
32
|
|
|
const AKA = 'Aka'; |
|
33
|
|
|
const NATIONALITY = 'Nationality'; |
|
34
|
|
|
const ADDRESS = 'Address'; |
|
35
|
|
|
const CITIZENSHIP = 'Citizenship'; |
|
36
|
|
|
const DATE_OF_BIRTH = 'DateOfBirth'; |
|
37
|
|
|
const PLACE_OF_BIRTH = 'PlaceOfBirth'; |
|
38
|
|
|
const VESSEL = 'Vessel'; |
|
39
|
|
|
const PROGRAM = 'Program'; |
|
40
|
|
|
|
|
41
|
|
|
const ERROR_FILE = 'Error. There was problem loading the file'; |
|
42
|
|
|
const ERROR_TOTAL_ENTITIES = 'Error. The number of entities doesn\'t math'; |
|
43
|
|
|
|
|
44
|
|
|
public $path_source; |
|
45
|
|
|
public $sanction_file_content; |
|
46
|
|
|
public $sanctions; |
|
47
|
|
|
public $entities; |
|
48
|
|
|
public $date; |
|
49
|
|
|
|
|
50
|
3 |
|
public function setSourcePath($pathSource) |
|
51
|
|
|
{ |
|
52
|
3 |
|
$this->path_source = $pathSource; |
|
53
|
3 |
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
public function run() |
|
56
|
|
|
{ |
|
57
|
3 |
|
if ($this->readEntities()) { |
|
58
|
3 |
|
$this->processEntities(); |
|
59
|
3 |
|
$this->setDate(); |
|
60
|
|
|
|
|
61
|
3 |
|
return $this->verifyEntities(); |
|
62
|
|
|
} |
|
63
|
|
|
// Log self:ERROR_FILE |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
public function readEntities() |
|
67
|
|
|
{ |
|
68
|
3 |
|
$xml = simplexml_load_string(file_get_contents($this->path_source)); |
|
69
|
|
|
|
|
70
|
3 |
|
$this->sanction_file_content = $this->xmlToArray($xml); |
|
71
|
|
|
|
|
72
|
3 |
|
return !empty($this->sanction_file_content); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Convert xml to array. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $xml |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
3 |
|
protected function xmlToArray($xml) |
|
83
|
|
|
{ |
|
84
|
3 |
|
$json = json_encode($xml); |
|
85
|
|
|
|
|
86
|
3 |
|
return $this->sanction_file_content = json_decode($json, true); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
public function verifyEntities() |
|
90
|
|
|
{ |
|
91
|
3 |
|
$result = (count($this->entities) === (int) $this->sanction_file_content['publshInformation']['Record_Count']); |
|
92
|
3 |
|
if (!$result) { |
|
93
|
|
|
//log |
|
94
|
|
|
echo self::ERROR_TOTAL_ENTITIES.'Detail: total entities '.count($this->entities).', records in the list: '.(int) $this->sanction_file_content['publshInformation']['Record_Count']; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
3 |
|
return $result; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
protected function getMapper() |
|
101
|
|
|
{ |
|
102
|
|
|
return [ |
|
103
|
3 |
|
self::ID => [ |
|
104
|
|
|
'id' => 'uid', |
|
105
|
|
|
'type' => 'idType', |
|
106
|
|
|
'number' => 'idNumber', |
|
107
|
|
|
'country' => 'idCountry', |
|
108
|
|
|
'date' => 'issueDate', |
|
109
|
|
|
'expirationDate' => 'expirationDate', |
|
110
|
3 |
|
], |
|
111
|
3 |
|
self::AKA => [ |
|
112
|
|
|
'id' => 'uid', |
|
113
|
|
|
'type' => 'idType', |
|
114
|
|
|
'category' => 'category', |
|
115
|
|
|
'last_name' => 'lastName', |
|
116
|
|
|
'first_name' => 'firstName', |
|
117
|
|
|
], |
|
118
|
3 |
|
self::ADDRESS => [ |
|
119
|
|
|
'id' => 'uid', |
|
120
|
|
|
'address1' => 'address1', |
|
121
|
|
|
'address2' => 'address2', |
|
122
|
|
|
'address3' => 'address3', |
|
123
|
|
|
'city' => 'city', |
|
124
|
|
|
'state_or_province' => 'stateOrProvince', |
|
125
|
|
|
'postal_code' => 'postalCode', |
|
126
|
|
|
'country' => 'country', |
|
127
|
|
|
], |
|
128
|
3 |
|
self::NATIONALITY => [ |
|
129
|
|
|
'id' => 'uid', |
|
130
|
|
|
'country' => 'country', |
|
131
|
|
|
'main_entry' => 'mainEntry', |
|
132
|
|
|
], |
|
133
|
3 |
|
self::CITIZENSHIP => [ |
|
134
|
|
|
'id' => 'uid', |
|
135
|
|
|
'country' => 'country', |
|
136
|
|
|
'main_entry' => 'mainEntry', |
|
137
|
|
|
], |
|
138
|
3 |
|
self::DATE_OF_BIRTH => [ |
|
139
|
|
|
'id' => 'uid', |
|
140
|
|
|
'date_of_birth' => 'dateOfBirth', |
|
141
|
|
|
'main_entry' => 'mainEntry', |
|
142
|
|
|
], |
|
143
|
3 |
|
self::PLACE_OF_BIRTH => [ |
|
144
|
|
|
'id' => 'uid', |
|
145
|
|
|
'place_of_birth' => 'placeOfBirth', |
|
146
|
|
|
'main_entry' => 'mainEntry', |
|
147
|
|
|
], |
|
148
|
3 |
|
self::VESSEL => [ |
|
149
|
|
|
'call_sign' => 'callSign', |
|
150
|
|
|
'vessel_type' => 'vesselType', |
|
151
|
|
|
'vessel_flag' => 'vesselFlag', |
|
152
|
|
|
'vessel_owner' => 'vesselOwner', |
|
153
|
|
|
'tonnage' => 'tonnage', |
|
154
|
|
|
'gross_registered_tonnage' => 'grossRegisteredTonnage', |
|
155
|
|
|
], |
|
156
|
3 |
|
self::PROGRAM => [ |
|
157
|
|
|
'program' => 'program', |
|
158
|
|
|
], |
|
159
|
|
|
]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Create the entities and set them. |
|
164
|
|
|
*/ |
|
165
|
3 |
|
public function processEntities() |
|
166
|
|
|
{ |
|
167
|
3 |
|
$this->sanctions = $this->sanction_file_content['sdnEntry']; |
|
168
|
|
|
|
|
169
|
3 |
|
foreach ($this->sanctions as $s) { |
|
170
|
3 |
|
$entity = new Entity($s['uid']); |
|
171
|
|
|
|
|
172
|
3 |
|
$entity->first_name = isset($s['firstName']) ? $s['firstName'] : null; |
|
173
|
3 |
|
$entity->last_name = isset($s['lastName']) ? $s['lastName'] : null; |
|
174
|
3 |
|
$entity->title = isset($s['title']) ? $s['title'] : null; |
|
175
|
3 |
|
$entity->type = isset($s['sdnType']) ? $s['sdnType'] : null; |
|
176
|
3 |
|
$entity->remarks = isset($s['remarks']) ? $s['remarks'] : null; |
|
177
|
|
|
|
|
178
|
3 |
|
$entity->program_list = $this->getList($s, 'programList', self::PROGRAM); |
|
179
|
3 |
|
$entity->citizenship_list = $this->getList($s, 'citizenship', self::CITIZENSHIP, 'citizenshipList'); |
|
180
|
3 |
|
$entity->aka_list = $this->getList($s, 'aka', self::AKA, 'akaList'); |
|
181
|
3 |
|
$entity->address_list = $this->getList($s, 'address', self::ADDRESS, 'addressList'); |
|
182
|
3 |
|
$entity->id_list = $this->getList($s, 'id', self::ID, 'idList'); |
|
183
|
3 |
|
$entity->nationality_list = $this->getList($s, 'nationality', self::NATIONALITY, 'nationalityList'); |
|
184
|
3 |
|
$entity->date_of_birth_list = $this->getList($s, 'dateOfBirthItem', self::DATE_OF_BIRTH, 'dateOfBirthList'); |
|
185
|
3 |
|
$entity->place_of_birth_list = $this->getList($s, 'placeOfBirthItem', self::PLACE_OF_BIRTH, 'placeOfBirthList'); |
|
186
|
3 |
|
$entity->vessel = $this->getList($s, 'vesselInfo', self::VESSEL); |
|
187
|
|
|
|
|
188
|
3 |
|
$this->entities[] = $entity; |
|
189
|
|
|
} |
|
190
|
3 |
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* get the content of the list. |
|
194
|
|
|
* |
|
195
|
|
|
* @param array $entityNode |
|
196
|
|
|
* @param string $node |
|
197
|
|
|
* @param string $className |
|
198
|
|
|
* @param bool|string $listName |
|
199
|
|
|
* |
|
200
|
|
|
* @return array |
|
201
|
|
|
*/ |
|
202
|
3 |
|
protected function getList(array $entityNode, $node, $className, $listName = false) |
|
203
|
|
|
{ |
|
204
|
3 |
|
$mapper = $this->getMapper()[$className]; |
|
205
|
3 |
|
$list = $this->getListContent($entityNode, $listName, $node); |
|
206
|
|
|
|
|
207
|
3 |
|
if (!$this->hasSubArrays($list)) { |
|
208
|
3 |
|
return [$this->createSubsIntance($list, $className, $mapper)]; |
|
209
|
|
|
} else { |
|
210
|
3 |
|
$elements = []; |
|
211
|
3 |
|
foreach ($list as $node) { |
|
212
|
3 |
|
$elements[] = $this->createSubsIntance($node, $className, $mapper); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
3 |
|
return $elements; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Check if an array has sub arrays. |
|
221
|
|
|
* |
|
222
|
|
|
* @param array $array |
|
223
|
|
|
* |
|
224
|
|
|
* @return bool |
|
225
|
|
|
*/ |
|
226
|
3 |
|
protected function hasSubArrays(array $array) |
|
227
|
|
|
{ |
|
228
|
3 |
|
return is_array($array) && count($array, COUNT_RECURSIVE) !== count($array); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Get the content of a subArray up two levels. |
|
233
|
|
|
* |
|
234
|
|
|
* @param array|null $entityNode |
|
235
|
|
|
* @param bool|string $listName |
|
236
|
|
|
* @param string $node |
|
237
|
|
|
* |
|
238
|
|
|
* @return array |
|
239
|
|
|
*/ |
|
240
|
3 |
|
protected function getListContent($entityNode, $listName, $node) |
|
241
|
|
|
{ |
|
242
|
3 |
|
$content = false; |
|
243
|
|
|
|
|
244
|
3 |
|
if (isset($entityNode[$listName][$node])) { |
|
245
|
3 |
|
$content = $entityNode[$listName][$node]; |
|
246
|
3 |
|
} elseif (isset($entityNode[$node])) { |
|
247
|
3 |
|
$content = $entityNode[$node]; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
3 |
|
return $content ? $content : []; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Based of the subnode, creates an instance and set the attributes. |
|
255
|
|
|
* |
|
256
|
|
|
* @param array $node info to get |
|
257
|
|
|
* @param string $className class to generate |
|
258
|
|
|
* @param array $mapper |
|
259
|
|
|
* |
|
260
|
|
|
* @return mixed |
|
261
|
|
|
*/ |
|
262
|
3 |
|
protected function createSubsIntance(array $node, $className, array $mapper) |
|
263
|
|
|
{ |
|
264
|
|
|
// if the subnode exists (for example program) |
|
265
|
3 |
|
if (isset($node)) { |
|
266
|
|
|
// get a new instance |
|
267
|
3 |
|
$classWithNS = 'ADiaz\AML\OpenList\models\\'.$className; |
|
268
|
|
|
|
|
269
|
3 |
|
$instance = new $classWithNS(); |
|
270
|
|
|
|
|
271
|
|
|
// set the attributes based of the mapper |
|
272
|
3 |
|
foreach ($mapper as $key => $value) { |
|
273
|
3 |
|
$instance->$key = isset($node[$value]) ? $node[$value] : null; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
3 |
|
return $instance; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
3 |
|
public function setDate() |
|
281
|
|
|
{ |
|
282
|
3 |
|
$this->date = $this->sanction_file_content['publshInformation']['Publish_Date']; |
|
283
|
3 |
|
} |
|
284
|
|
|
|
|
285
|
3 |
|
public function getEntities() |
|
286
|
|
|
{ |
|
287
|
3 |
|
return $this->entities; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Get the date of the list. When it was updated. |
|
292
|
|
|
*/ |
|
293
|
|
|
public function getDate() |
|
294
|
|
|
{ |
|
295
|
|
|
return $this->date; |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|