|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace one; |
|
4
|
|
|
|
|
5
|
|
|
use One\Model\Article; |
|
6
|
|
|
|
|
7
|
|
|
class FormatMapping { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* map a single article to main attributes in Article Class |
|
11
|
|
|
* @param string $singleJsonArticle JSON response |
|
12
|
|
|
* @return \One\Model\Article An Article Object |
|
13
|
|
|
*/ |
|
14
|
|
|
public function mapMainArticle($singleJsonArticle) { |
|
15
|
|
|
|
|
16
|
|
|
$decoded_article = $singleJsonArticle; |
|
17
|
|
|
|
|
18
|
|
|
if (!is_null($this->jsonToArray($singleJsonArticle))) { |
|
19
|
|
|
$decoded_article = $this->jsonToArray($singleJsonArticle); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$data_article = $decoded_article['data']; |
|
23
|
|
|
|
|
24
|
|
|
$title = function () use ($data_article) { |
|
25
|
|
|
if (isset($data_article['title'])) { |
|
26
|
|
|
return $data_article['title']; |
|
27
|
|
|
} |
|
28
|
|
|
return null; |
|
29
|
|
|
}; |
|
30
|
|
|
|
|
31
|
|
|
$body = function () use ($data_article) { |
|
32
|
|
|
if (isset($data_article['body'])) { |
|
33
|
|
|
return $data_article['body']; |
|
34
|
|
|
} |
|
35
|
|
|
return null; |
|
36
|
|
|
}; |
|
37
|
|
|
|
|
38
|
|
|
$source = function () use ($data_article) { |
|
39
|
|
|
if (isset($data_article['source'])) { |
|
40
|
|
|
return $data_article['source']; |
|
41
|
|
|
} |
|
42
|
|
|
return null; |
|
43
|
|
|
}; |
|
44
|
|
|
|
|
45
|
|
|
$uniqueId = function () use ($data_article) { |
|
46
|
|
|
if (isset($data_article['uniqueId'])) { |
|
47
|
|
|
return $data_article['uniqueId']; |
|
48
|
|
|
} |
|
49
|
|
|
return null; |
|
50
|
|
|
}; |
|
51
|
|
|
|
|
52
|
|
|
$typeId = function () use ($data_article) { |
|
53
|
|
|
if (isset($data_article['type']['type_id'])) { |
|
54
|
|
|
return $data_article['type']['type_id']; |
|
55
|
|
|
} |
|
56
|
|
|
return null; |
|
57
|
|
|
}; |
|
58
|
|
|
|
|
59
|
|
|
$categoryId = function () use ($data_article) { |
|
60
|
|
|
if (isset($data_article['category']['category_id'])) { |
|
61
|
|
|
return $data_article['category']['category_id']; |
|
62
|
|
|
} |
|
63
|
|
|
return null; |
|
64
|
|
|
}; |
|
65
|
|
|
|
|
66
|
|
|
$reporter = function () use ($data_article) { |
|
67
|
|
|
if (isset($data_article['reporter'])) { |
|
68
|
|
|
return $data_article['reporter']; |
|
69
|
|
|
} |
|
70
|
|
|
return null; |
|
71
|
|
|
}; |
|
72
|
|
|
|
|
73
|
|
|
$lead = function () use ($data_article) { |
|
74
|
|
|
if (isset($data_article['lead'])) { |
|
75
|
|
|
return $data_article['lead']; |
|
76
|
|
|
} |
|
77
|
|
|
return null; |
|
78
|
|
|
}; |
|
79
|
|
|
|
|
80
|
|
|
$tags = function () use ($data_article) { |
|
81
|
|
|
if (isset($data_article['tags']['tag_name'])) { |
|
82
|
|
|
return $data_article['tags']['tag_name']; |
|
83
|
|
|
} |
|
84
|
|
|
return null; |
|
85
|
|
|
}; |
|
86
|
|
|
|
|
87
|
|
|
$publishedAt = function () use ($data_article) { |
|
88
|
|
|
if (isset($data_article['published_at'])) { |
|
89
|
|
|
return $data_article['published_at']; |
|
90
|
|
|
} |
|
91
|
|
|
return null; |
|
92
|
|
|
}; |
|
93
|
|
|
|
|
94
|
|
|
$identifier = function () use ($data_article) { |
|
95
|
|
|
if (isset($data_article['id'])) { |
|
96
|
|
|
return $data_article['id']; |
|
97
|
|
|
} |
|
98
|
|
|
return null; |
|
99
|
|
|
}; |
|
100
|
|
|
|
|
101
|
|
|
$article = array(); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
$article = new Article( |
|
104
|
|
|
|
|
105
|
|
|
$title(), |
|
106
|
|
|
|
|
107
|
|
|
$body(), |
|
108
|
|
|
|
|
109
|
|
|
$source(), |
|
110
|
|
|
|
|
111
|
|
|
$uniqueId(), |
|
112
|
|
|
|
|
113
|
|
|
$typeId(), |
|
114
|
|
|
|
|
115
|
|
|
$categoryId(), |
|
116
|
|
|
|
|
117
|
|
|
$reporter(), |
|
118
|
|
|
|
|
119
|
|
|
$lead(), |
|
120
|
|
|
|
|
121
|
|
|
$tags(), |
|
122
|
|
|
|
|
123
|
|
|
$publishedAt(), |
|
124
|
|
|
|
|
125
|
|
|
$identifier() |
|
126
|
|
|
|
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
return $article; |
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Convert JSON string to associative array |
|
135
|
|
|
* @param string $jsonResponse |
|
136
|
|
|
* @return array if it is valid json, null otherwise |
|
137
|
|
|
*/ |
|
138
|
|
|
public function jsonToArray($jsonResponse) { |
|
139
|
|
|
|
|
140
|
|
|
try { |
|
141
|
|
|
return json_decode($jsonResponse, TRUE); |
|
142
|
|
|
|
|
143
|
|
|
} catch (\Exception $e) { |
|
144
|
|
|
return null; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.