1
|
|
|
<?php |
2
|
|
|
namespace Thunder\SimilarWebApi\Parser; |
3
|
|
|
|
4
|
|
|
use Thunder\SimilarWebApi\AbstractParser; |
5
|
|
|
use Thunder\SimilarWebApi\RawResponse; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
final class JsonParser extends AbstractParser |
11
|
|
|
{ |
12
|
33 |
View Code Duplication |
private function parseValues(array $json) |
|
|
|
|
13
|
|
|
{ |
14
|
33 |
|
$values = array(); |
15
|
33 |
|
if(array_key_exists('values', $this->mapping)) |
16
|
33 |
|
{ |
17
|
21 |
|
foreach($this->mapping['values'] as $key => $item) |
18
|
|
|
{ |
19
|
21 |
|
$values[$key] = $json[$item['json']['field']]; |
20
|
21 |
|
} |
21
|
21 |
|
} |
22
|
|
|
|
23
|
33 |
|
return $values; |
24
|
|
|
} |
25
|
|
|
|
26
|
33 |
|
private function parseArrays(array $json) |
27
|
|
|
{ |
28
|
33 |
|
$arrays = array(); |
29
|
33 |
|
if(array_key_exists('arrays', $this->mapping)) |
30
|
33 |
|
{ |
31
|
3 |
|
foreach($this->mapping['arrays'] as $key => $item) |
32
|
|
|
{ |
33
|
3 |
|
$array = array(); |
34
|
3 |
|
foreach($json[$item['json']['field']] as $element) |
35
|
|
|
{ |
36
|
3 |
|
$array[] = $element; |
37
|
3 |
|
} |
38
|
3 |
|
$arrays[$key] = $array; |
39
|
3 |
|
} |
40
|
3 |
|
} |
41
|
|
|
|
42
|
33 |
|
return $arrays; |
43
|
|
|
} |
44
|
|
|
|
45
|
33 |
|
private function parseMaps(array $json) |
46
|
|
|
{ |
47
|
33 |
|
$maps = array(); |
48
|
33 |
|
if(array_key_exists('maps', $this->mapping)) |
49
|
33 |
|
{ |
50
|
15 |
View Code Duplication |
foreach($this->mapping['maps'] as $key => $item) |
|
|
|
|
51
|
|
|
{ |
52
|
15 |
|
$map = array(); |
53
|
15 |
|
foreach($json[$item['json']['field']] as $element) |
54
|
|
|
{ |
55
|
15 |
|
$map[$element[$item['json']['key']]] = $element[$item['json']['value']]; |
56
|
15 |
|
} |
57
|
15 |
|
$maps[$key] = $map; |
58
|
15 |
|
} |
59
|
15 |
|
} |
60
|
|
|
|
61
|
33 |
|
return $maps; |
62
|
|
|
} |
63
|
|
|
|
64
|
33 |
|
private function parseTuples(array $json) |
65
|
|
|
{ |
66
|
33 |
|
$tuples = array(); |
67
|
33 |
|
if(array_key_exists('tuples', $this->mapping)) |
68
|
33 |
|
{ |
69
|
3 |
|
foreach($this->mapping['tuples'] as $key => $item) |
70
|
|
|
{ |
71
|
3 |
|
$tuple = array(); |
72
|
3 |
View Code Duplication |
foreach($json[$item['json']['field']] as $element) |
|
|
|
|
73
|
|
|
{ |
74
|
3 |
|
$array = array(); |
75
|
3 |
|
foreach($item['json']['items'] as $itemData) |
76
|
|
|
{ |
77
|
3 |
|
$array[$itemData['value']] = $element[$itemData['key']]; |
78
|
3 |
|
} |
79
|
3 |
|
$tuple[$element[$item['json']['index']]] = $array; |
80
|
3 |
|
} |
81
|
3 |
|
$tuples[$key] = $tuple; |
82
|
3 |
|
} |
83
|
3 |
|
} |
84
|
|
|
|
85
|
33 |
|
return $tuples; |
86
|
|
|
} |
87
|
|
|
|
88
|
36 |
|
protected function parse($content) |
89
|
|
|
{ |
90
|
36 |
|
$json = json_decode($content, true); |
91
|
36 |
|
if(!$json) |
92
|
36 |
|
{ |
93
|
3 |
|
throw new \RuntimeException(sprintf('Failed to parse JSON data: "%s(...)"!', substr($content, 0, 100))); |
94
|
|
|
} |
95
|
|
|
|
96
|
33 |
|
return new RawResponse($content, |
97
|
33 |
|
$this->parseValues($json), |
98
|
33 |
|
$this->parseArrays($json), |
99
|
33 |
|
$this->parseMaps($json), |
100
|
33 |
|
$this->parseTuples($json)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.