Unsc::addToRemarks()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 2
crap 2
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\DateOfBirth;
8
use ADiaz\AML\OpenList\models\Entity;
9
use ADiaz\AML\OpenList\models\Id;
10
use ADiaz\AML\OpenList\models\Nationality;
11
use ADiaz\AML\OpenList\models\PlaceOfBirth;
12
use ADiaz\AML\OpenList\models\Program;
13
14
/**
15
 * This file is part of the OpenList Parser utility.
16
 *
17
 * @category PHP
18
 *
19
 * @author    Alberto Diaz <[email protected]>
20
 * @copyright 2016 Alberto Diaz <[email protected]>
21
 * @license   This source file is subject to the MIT license that is bundled
22
 *
23
 * @version Release: @package_version@
24
 *
25
 * @link http://tytem.com
26
 */
27
class Unsc implements ListInterface
28
{
29
    protected $path_source;
30
    protected $sanction_file_content;
31
    protected $entities;
32
    protected $date;
33
34
    /**
35
     * Process the list (read, process, set the date and verify entities).
36
     *
37
     * Example
38
     *
39
     * if ($this->readEntities()) {
40
     *     $this->processEntities();
41
     *     $this->setDate();
42
     *     return $this->verifyEntities();
43
     * }
44
     */
45 6
    public function run()
46
    {
47 6
        if ($this->readEntities()) {
48 6
            $this->processEntities();
49 6
            $this->setDate();
50
51 6
            return $this->verifyEntities();
52
        }
53
    }
54
55
    /**
56
     * Set source path, the file path of the list.
57
     * this method will be called by the Processor.
58
     *
59
     * Suggestion: add a property to the class
60
     *
61
     * @param string $path
62
     */
63 6
    public function setSourcePath($path)
64
    {
65 6
        $this->path_source = $path;
66 6
    }
67
68
    /**
69
     * Read the content from the file list.
70
     *
71
     * Suggestion: add a property to the class to save the content
72
     *
73
     * @TODO change the name to setFileContent;
74
     */
75 6
    public function readEntities()
76
    {
77 6
        $this->sanction_file_content = simplexml_load_string(file_get_contents($this->path_source));
78
79 6
        return $this->sanction_file_content !== false;
80
    }
81
82
    /**
83
     * Process entities. It analyses the entities and it parses to classes.
84
     */
85 6
    public function processEntities()
86
    {
87 6
        $individuals = $this->sanction_file_content->xpath('//INDIVIDUAL');
88
89 6
        foreach ($individuals as $individual) {
90 6
            $this->entities[] = $this->parseIndividuals($individual);
91
        }
92
93 6
        $entities = $this->sanction_file_content->xpath('//ENTITY');
94
95 6
        foreach ($entities as $entity) {
96 6
            $this->entities[] = $this->parseEntitiesAndOtherGroups($entity);
97
        }
98 6
    }
99
100
    /**
101
     * Parse the individuals.
102
     *
103
     * @param \SimpleXMLElement $node
104
     *
105
     * @return Entity
106
     */
107 6
    protected function parseIndividuals($node)
108
    {
109 6
        $entity = new Entity($this->g($node->xpath('DATAID')));
110 6
        $entity->type = $entity::TYPE_INDIVIDUAL;
111 6
        $entity->first_name = $this->g($node->xpath('FIRST_NAME'));
112 6
        $entity->last_name = $this->combine([$this->g($node->xpath('SECOND_NAME')), $this->g($node->xpath('THIRD_NAME')), $this->g($node->xpath('FOURTH_NAME'))]);
113 6
        $entity->name_original_script = $this->g($node->xpath('NAME_ORIGINAL_SCRIPT'));
114 6
        $entity->remarks = $this->g($node->xpath('COMMENTS1'));
115 6
        $entity->position = $this->parseDesignations($node->xpath('DESIGNATION'));
116
117 6
        $entity->program_list = $this->parsePrograms($node->xpath('UN_LIST_TYPE'));
118 6
        $entity->aka_list = $this->parseAkas($node->xpath('INDIVIDUAL_ALIAS'));
119 6
        $entity->address_list = $this->parseAddresses($node->xpath('INDIVIDUAL_ADDRESS'));
0 ignored issues
show
Documentation introduced by
$node->xpath('INDIVIDUAL_ADDRESS') is of type array, but the function expects a object<SimpleXMLElement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120 6
        $entity->date_of_birth_list = $this->parseDateOfBirth($node->xpath('INDIVIDUAL_DATE_OF_BIRTH'));
0 ignored issues
show
Documentation introduced by
$node->xpath('INDIVIDUAL_DATE_OF_BIRTH') is of type array, but the function expects a object<SimpleXMLElement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121 6
        $entity->place_of_birth_list = $this->parsePlaceOfBirth($node->xpath('INDIVIDUAL_PLACE_OF_BIRTH'));
0 ignored issues
show
Documentation introduced by
$node->xpath('INDIVIDUAL_PLACE_OF_BIRTH') is of type array, but the function expects a object<SimpleXMLElement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
122 6
        $entity->date_of_birth_list = $this->parseIds($node->xpath('INDIVIDUAL_DOCUMENT'));
0 ignored issues
show
Documentation introduced by
$node->xpath('INDIVIDUAL_DOCUMENT') is of type array, but the function expects a object<SimpleXMLElement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
124 6
        return $entity;
125
    }
126
127
    /**
128
     * Parse the entities.
129
     *
130
     * @param \SimpleXMLElement $node
131
     *
132
     * @return Entity Entity
133
     */
134 6
    protected function parseEntitiesAndOtherGroups($node)
135
    {
136 6
        $entity = new Entity($this->g($node->xpath('DATAID')));
137 6
        $entity->type = $entity::TYPE_ENTITY;
138 6
        $entity->first_name = $this->g($node->xpath('FIRST_NAME'));
139
        //@Todo <xs:element ref="REFERENCE_NUMBER"/>
140
        //@Todo <xs:element ref="LISTED_ON"/>
141
        //@Todo <xs:element minOccurs="0" ref="SUBMITTED_ON"/>
142 6
        $entity->name_original_script = $this->g($node->xpath('NAME_ORIGINAL_SCRIPT'));
143 6
        $entity->remarks = $this->g($node->xpath('COMMENTS1'));
144 6
        $entity->program_list = $this->parsePrograms($node->xpath('UN_LIST_TYPE'));
145
        //@Todo <xs:element minOccurs="0" ref="LAST_DAY_UPDATED"/>
146
147 6
        $entity->aka_list = $this->parseAkas($node->xpath('ENTITY_ALIAS'));
148 6
        $entity->address_list = $this->parseAddresses($node->xpath('ENTITY_ADDRESS'));
0 ignored issues
show
Documentation introduced by
$node->xpath('ENTITY_ADDRESS') is of type array, but the function expects a object<SimpleXMLElement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
149
        //@Todo <xs:element ref="SORT_KEY"/>
150
        //@Todo <xs:element ref="SORT_KEY_LAST_MOD"/>
151
        //@Todo <xs:element minOccurs="0" ref="DELISTED_ON"/>
152
153 6
        return $entity;
154
    }
155
156
    /**
157
     * Parse the alias of a node.
158
     *
159
     * @param array $alias
160
     *
161
     * @return array list of akas
162
     */
163 6
    protected function parseAkas($alias)
164
    {
165 6
        $akas = [];
166 6
        foreach ($alias as $alia) {
167
            //it can be a name, or an string with multiple names separated by ;
168 6
            $names = explode(';', $this->g($alia->xpath('ALIAS_NAME')));
169 6
            foreach ($names as $name) {
170 6
                $aka = new Aka();
171
                //@Todo it should be full_name
172 6
                $aka->first_name = trim($name);
173
                //@TODO it should be save in quality
174 6
                $aka->category = $this->g($alia->xpath('QUALITY'));
175
                //@TODO Missing to parse DATE_OF_BIRTH CITY_OF_BIRTH COUNTRY_OF_BIRTH NOTE
176
177 6
                $akas[] = $aka;
178
            }
179
        }
180
181 6
        return $akas;
182
    }
183
184
    /**
185
     * Parse the programs.
186
     *
187
     * @param $listTypes
188
     *
189
     * @return array
190
     */
191 6
    protected function parsePrograms($listTypes)
192
    {
193 6
        $programs = [];
194 6
        foreach ($listTypes as $listType) {
195 6
            if ($listType->count()) {
196
                $program = new Program();
197
                $program->program = $listType;
198 6
                $programs[] = $program;
199
            }
200
        }
201
202 6
        return $programs;
203
    }
204
205
    /**
206
     * Parses the addresses of a node.
207
     *
208
     * @param \SimpleXMLElement $addresses array
209
     *
210
     * @return array list of addresses
211
     */
212 6
    protected function parseAddresses($addresses)
213
    {
214 6
        $locations = [];
215 6
        foreach ($addresses as $address) {
216
            // check if empty the SimpleXmlElement
217 6
            if ($address->count()) {
218 6
                $location = new Address();
219 6
                $location->country = $this->g($address->xpath('COUNTRY'));
220 6
                $location->address1 = $this->g($address->xpath('STREET'));
221 6
                $location->city = $this->g($address->xpath('CITY'));
222 6
                $location->postal_code = $this->g($address->xpath('ZIP_CODE'));
223 6
                $location->state_or_province = $this->g($address->xpath('STATE_PROVINCE'));
224
                //@Todo it should be saved the note in Remarks or better in address
225
                //$location->note = $this->g($address->xpath('NOTE'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
226
227 6
                $locations[] = $location;
228
            }
229
        }
230
231 6
        return $locations;
232
    }
233
234
    /**
235
     * Parse the date of births of a node.
236
     *
237
     * @param \SimpleXMLElement $dobs
238
     *
239
     * @return array list of date of birth
240
     */
241 6
    protected function parseDateOfBirth($dobs)
242
    {
243 6
        $dateOfBirths = [];
244
245 6
        if ($this->areElements($dobs)) {
246 6
            foreach ($dobs as $dob) {
247 6
                $date = $this->g($dob->xpath('DATE'));
248
249 6
                if ($date !== null) {
250 6
                    $dateOfBirth = new DateOfBirth();
251 6
                    $dateOfBirth->date_of_birth = $date;
252 6
                    $dateOfBirths[] = $dateOfBirth;
253
                }
254
255 6
                $year = $this->g($dob->xpath('YEAR'));
256 6
                if ($year !== null) {
257 6
                    $dateOfBirth = new DateOfBirth();
258 6
                    $dateOfBirth->date_of_birth = $year;
259 6
                    $dateOfBirths[] = $dateOfBirth;
260
                }
261
262 6
                $between = $this->g($dob->xpath('FROM_YEAR')).'-'.$this->g($dob->xpath('TO_YEAR'));
263 6
                if ($between !== '-') {
264 6
                    $dateOfBirth = new DateOfBirth();
265 6
                    $dateOfBirth->date_of_birth = $between;
266 6
                    $dateOfBirths[] = $dateOfBirth;
267
                }
268
269
                //@Todo it should be saved the note in Remarks or better in address
270
            }
271
        }
272
273 6
        return $dateOfBirths;
274
    }
275
276
    /**
277
     * Parse place of birth.
278
     *
279
     * @param \SimpleXMLElement $places_of_birth
280
     *
281
     * @return array list of place of birth
282
     */
283 6
    protected function parsePlaceOfBirth($places_of_birth)
284
    {
285 6
        $locations = [];
286 6
        foreach ($places_of_birth as $placeOfBirth) {
287 6
            if ($placeOfBirth->count()) {
288 6
                $location = new PlaceOfBirth();
289 6
                $location->place_of_birth = $this->g($placeOfBirth->xpath('CITY'));
290 6
                $location->state_province = $this->g($placeOfBirth->xpath('STATE_PROVINCE'));
291 6
                $location->country = $this->g($placeOfBirth->xpath('COUNTRY'));
292 6
                $location->remarks = $this->g($placeOfBirth->xpath('NOTE'));
293
294 6
                $locations[] = $location;
295
            }
296
        }
297
298 6
        return $locations;
299
    }
300
301
    /**
302
     * Parse individual documents.
303
     *
304
     * @param \SimpleXMLElement $documents
305
     *
306
     * @return array list of the individual documents
307
     */
308 6
    protected function parseIds($documents)
309
    {
310 6
        $ids = [];
311 6
        foreach ($documents as $document) {
312 6
            $id = new Id();
313 6
            $id->country = $this->g($document->xpath('COUNTRY_OF_ISSUE'));
314 6
            $id->date = $this->g($document->xpath('DATE_OF_ISSUE'));
315 6
            $id->number = $this->g($document->xpath('NUMBER'));
316 6
            $id->type = $this->g($document->xpath('TYPE_OF_DOCUMENT'));
317 6
            $id->remarks .= $this->addToRemarks($this->g($document->xpath('TYPE_OF_DOCUMENT2')), 'TYPE_OF_DOCUMENT2');
318 6
            $id->remarks .= $this->addToRemarks($this->g($document->xpath('CITY_OF_ISSUE')), 'CITY_OF_ISSUE');
319 6
            $id->remarks .= $this->addToRemarks($this->g($document->xpath('ISSUING_COUNTRY')), 'ISSUING_COUNTRY');
320 6
            $id->remarks .= $this->addToRemarks($this->g($document->xpath('COUNTRY_OF_ISSUE')), 'COUNTRY_OF_ISSUE');
321 6
            $id->remarks .= $this->addToRemarks($this->g($document->xpath('NOTE')), 'NOTE');
322
323 6
            $ids[] = $id;
324
        }
325
326 6
        return $ids;
327
    }
328
329
    /**
330
     * Parse nationalities.
331
     *
332
     * @param \SimpleXMLElement $node
333
     *
334
     * @return array list of nationalities
335
     */
336
    protected function parseNationalities($node)
337
    {
338
        $nationalities = [];
339
        $subNodes = $node->xpath('NATIONALITY');
340
        foreach ($subNodes as $subNode) {
341
            $nationality = new Nationality();
342
            $nationality->country = $this->g($subNode->xpath('VALUE'));
343
            $nationalities[] = $nationality;
344
        }
345
346
        return $nationalities;
347
    }
348
349
    /**
350
     * Parse designations.
351
     *
352
     * @param $subNodes array
353
     * @param string $delimeter
354
     *
355
     * @return string with all designations
356
     */
357 6
    protected function parseDesignations($subNodes, $delimeter = ';')
358
    {
359 6
        $designations = [];
360 6
        foreach ($subNodes as $subNode) {
361 6
            $designations[] = $this->g($subNode->xpath('VALUE'));
362
        }
363
364 6
        return implode($delimeter, $designations);
365
    }
366
367
    /**
368
     * just a simple function to add to a string.
369
     *
370
     * @param string|null $str
371
     * @param string      $label
372
     *
373
     * @return string
374
     */
375 6
    protected function addToRemarks($str, $label)
376
    {
377 6
        return ($str !== null) ? $label.' '.$str : '';
378
    }
379
380
    /**
381
     * @param \SimpleXMLElement $array
382
     *
383
     * @return bool
384
     * @TODO improve it
385
     */
386 6
    protected function areElements($array)
387
    {
388 6
        return !empty($array) && (json_encode($array) !== '[{}]');
389
    }
390
391
    /**
392
     * Combine an array in a string seperate by an space
393
     * At the end it replaces multiple spaces with a single space.
394
     *
395
     * @param $array array
396
     *
397
     * @return string
398
     */
399 6
    protected function combine($array)
400
    {
401 6
        return trim(preg_replace('!\s+!', ' ', implode(' ', $array)));
402
    }
403
404
    /**
405
     * It is just a helper to extract the first element from
406
     * an array and transform it to string.
407
     *
408
     * @param $array
409
     *
410
     * @return null|string
411
     */
412 6
    protected function g($array)
413
    {
414 6
        return isset($array[0]) ? (string) $array[0] : null;
415
    }
416
417
    /**
418
     * Verify the entities, for example the content or
419
     * if the amount of entities match with amount from the file if it is provided.
420
     */
421 6
    public function verifyEntities()
422
    {
423 6
        return true;
424
    }
425
426
    /**
427
     * Get entities
428
     * return the entities created.
429
     */
430 6
    public function getEntities()
431
    {
432 6
        return $this->entities;
433
    }
434
435
    /**
436
     * Set the date when the list was updated, it takes the info from the content of the file if exists.
437
     *
438
     * @TODO add an attribute to send the format of the date
439
     */
440 6
    public function setDate()
441
    {
442
        /* Extract from dateGenerated
443
        <CONSOLIDATED_LIST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
444
        xsi:noNamespaceSchemaLocation="http://www.un.org/sc/committees/resources/xsd/sc-sanctions.xsd"
445
        dateGenerated="2016-03-07T13:06:25.399-05:00">
446
         **/
447 6
    }
448
449
    /**
450
     * Get the date of the list. When it was updated.
451
     * It must be given in Y-m-d format.
452
     */
453
    public function getDate()
454
    {
455
        return $this->date;
456
    }
457
}
458