1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of gpupo/pipe2 |
5
|
|
|
* |
6
|
|
|
* (c) Gilmar Pupo <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* For more information, see |
12
|
|
|
* <https://opensource.gpupo.com/pipe2/>. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Gpupo\Pipe2\Traits; |
16
|
|
|
|
17
|
|
|
trait ParserTrait |
18
|
|
|
{ |
19
|
|
|
protected function fieldReduce(Array $item) |
20
|
|
|
{ |
21
|
|
|
return $item; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function parserItems($data) |
25
|
|
|
{ |
26
|
|
|
$item = []; |
27
|
|
|
foreach ($data['item'] as $product) { |
28
|
|
|
if (array_key_exists('tag', $product) && array_key_exists('value', $product)) { |
29
|
|
|
$item[$product['tag']] = $product['value']; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $item; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function parserFromFile($filePath, $key = 'item') |
37
|
|
|
{ |
38
|
|
|
$list = []; |
39
|
|
|
|
40
|
|
|
$doc = new \DOMDocument(); |
41
|
|
|
if (@$doc->load($filePath)) { |
42
|
|
|
$xml = $doc->saveXML(); |
43
|
|
|
$values = $index = []; |
44
|
|
|
$parser = xml_parser_create(); |
45
|
|
|
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); |
46
|
|
|
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); |
47
|
|
|
xml_parse_into_struct($parser, $xml, $values, $index); |
48
|
|
|
xml_parser_free($parser); |
49
|
|
|
|
50
|
|
|
foreach ($index as $k => $v) { |
|
|
|
|
51
|
|
|
if ($k === $key) { |
52
|
|
|
for ($i = 0; $i < count($v); $i += 2) { |
|
|
|
|
53
|
|
|
$count = (empty($count) ? $v[$i] : ++$count); |
54
|
|
|
$offset = $v[$i] + 1; |
55
|
|
|
$len = $v[$i + 1] - $offset; |
56
|
|
|
$list[$count] = $values[$v[0]]; |
57
|
|
|
$item = $this->fieldReduce(array_slice($values, $offset, $len)); |
58
|
|
|
|
59
|
|
|
if (!empty($item)) { |
60
|
|
|
$list[$count][$key] = $item; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
break; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$list[] = $values[$v[0]]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $list; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.