1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SchulzeFelix\Stat\Api; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use SchulzeFelix\Stat\Objects\StatKeyword; |
7
|
|
|
use SchulzeFelix\Stat\Objects\StatKeywordEngineRanking; |
8
|
|
|
use SchulzeFelix\Stat\Objects\StatKeywordRanking; |
9
|
|
|
use SchulzeFelix\Stat\Objects\StatKeywordStats; |
10
|
|
|
use SchulzeFelix\Stat\Objects\StatLocalSearchTrend; |
11
|
|
|
|
12
|
|
|
class StatKeywords extends BaseStat |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param $siteID |
16
|
|
|
* @return Collection |
17
|
|
|
*/ |
18
|
|
|
public function list($siteID): Collection |
19
|
|
|
{ |
20
|
|
|
$start = 0; |
21
|
|
|
$keywords = collect(); |
22
|
|
|
|
23
|
|
|
do { |
24
|
|
|
$response = $this->performQuery('keywords/list', ['site_id' => $siteID, 'start' => $start, 'results' => 5000]); |
25
|
|
|
$start += 5000; |
26
|
|
|
|
27
|
|
|
if ($response['totalresults'] == 0) { |
28
|
|
|
break; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (isset($response['Result']['Id'])) { |
32
|
|
|
$keywords->push($response['Result']); |
33
|
|
|
break; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$keywords = $keywords->merge($response['Result']); |
37
|
|
|
|
38
|
|
|
if (! isset($response['nextpage'])) { |
39
|
|
|
break; |
40
|
|
|
} |
41
|
|
|
} while ($response['resultsreturned'] < $response['totalresults']); |
42
|
|
|
|
43
|
|
|
$keywords = $keywords->transform(function ($keyword) { |
44
|
|
|
return $this->transformListedKeyword($keyword); |
45
|
|
|
}); |
46
|
|
|
|
47
|
|
|
return $keywords; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $siteID |
52
|
|
|
* @param $market |
53
|
|
|
* @param array $keywords |
54
|
|
|
* @param array|null $tags |
55
|
|
|
* @param null $location |
56
|
|
|
* @param string $device |
57
|
|
|
* @return Collection |
58
|
|
|
*/ |
59
|
|
|
public function create($siteID, $market, array $keywords, array $tags = null, $location = null, $device = 'Desktop'): Collection |
60
|
|
|
{ |
61
|
|
|
$arguments['site_id'] = $siteID; |
|
|
|
|
62
|
|
|
$arguments['market'] = $market; |
63
|
|
|
$arguments['device'] = $device; |
64
|
|
|
$arguments['type'] = 'regular'; |
65
|
|
|
|
66
|
|
|
$keywords = array_map(function ($keyword) { |
67
|
|
|
$keyword = str_replace(',', '\,', $keyword); |
68
|
|
|
$keyword = rawurlencode($keyword); |
69
|
|
|
|
70
|
|
|
return $keyword; |
71
|
|
|
}, $keywords); |
72
|
|
|
$arguments['keyword'] = implode(',', $keywords); |
73
|
|
|
|
74
|
|
|
if (! is_null($tags) && count($tags) > 0) { |
75
|
|
|
$tags = array_map(function ($tag) { |
76
|
|
|
$tag = str_replace(',', '-', $tag); |
77
|
|
|
$tag = rawurlencode($tag); |
78
|
|
|
|
79
|
|
|
return $tag; |
80
|
|
|
}, $tags); |
81
|
|
|
|
82
|
|
|
$arguments['tag'] = implode(',', $tags); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (! is_null($location) && $location != '') { |
86
|
|
|
$arguments['location'] = $location; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$response = $this->performQuery('keywords/create', $arguments); |
90
|
|
|
|
91
|
|
|
$keywords = collect(); |
92
|
|
|
|
93
|
|
|
if ($response['resultsreturned'] == 0) { |
94
|
|
|
return $keywords; |
95
|
|
|
} |
96
|
|
|
if ($response['resultsreturned'] == 1) { |
97
|
|
|
$keywords->push($response['Result']); |
98
|
|
|
} else { |
99
|
|
|
$keywords = collect($response['Result']); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $keywords->transform(function ($keyword) { |
103
|
|
|
return $this->transformCreatedKeyword($keyword); |
104
|
|
|
}); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param int|array $id |
109
|
|
|
* @return Collection |
110
|
|
|
*/ |
111
|
|
|
public function delete($id) |
112
|
|
|
{ |
113
|
|
|
if (! is_array($id)) { |
114
|
|
|
$id = [$id]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (! array_filter($id)) { |
118
|
|
|
return new Collection(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$ids = implode(',', $id); |
122
|
|
|
|
123
|
|
|
$response = $this->performQuery('keywords/delete', ['id' => $ids]); |
124
|
|
|
|
125
|
|
|
if (isset($response['Result']['Id'])) { |
126
|
|
|
return collect($response['Result']['Id']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return collect($response['Result'])->transform(function ($keywordID) { |
130
|
|
|
return $keywordID['Id']; |
131
|
|
|
}); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $keyword |
136
|
|
|
* @return StatKeyword |
137
|
|
|
*/ |
138
|
|
|
protected function transformCreatedKeyword($keyword) |
139
|
|
|
{ |
140
|
|
|
return new StatKeyword([ |
141
|
|
|
'id' => $keyword['Id'], |
142
|
|
|
'keyword' => $keyword['Keyword'], |
143
|
|
|
'keyword_market' => $keyword['KeywordMarket'], |
144
|
|
|
'keyword_location' => $keyword['KeywordLocation'], |
145
|
|
|
'keyword_device' => $keyword['KeywordDevice'], |
146
|
|
|
'created_at' => $keyword['CreatedAt'], |
147
|
|
|
]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $keyword |
152
|
|
|
* @return StatKeyword |
153
|
|
|
*/ |
154
|
|
|
protected function transformListedKeyword($keyword) |
155
|
|
|
{ |
156
|
|
|
$modifiedKeyword = new StatKeyword(); |
157
|
|
|
$modifiedKeyword->id = $keyword['Id']; |
158
|
|
|
$modifiedKeyword->keyword = $keyword['Keyword']; |
159
|
|
|
$modifiedKeyword->keyword_market = $keyword['KeywordMarket']; |
160
|
|
|
$modifiedKeyword->keyword_location = $keyword['KeywordLocation']; |
161
|
|
|
$modifiedKeyword->keyword_device = $keyword['KeywordDevice']; |
162
|
|
|
|
163
|
|
View Code Duplication |
if ($keyword['KeywordTags'] == 'none') { |
|
|
|
|
164
|
|
|
$modifiedKeyword->keyword_tags = collect(); |
165
|
|
|
} else { |
166
|
|
|
$modifiedKeyword->keyword_tags = collect(explode(',', $keyword['KeywordTags'])); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
View Code Duplication |
if (is_null($keyword['KeywordStats'])) { |
|
|
|
|
170
|
|
|
$modifiedKeyword->keyword_stats = null; |
171
|
|
|
} else { |
172
|
|
|
$localTrends = collect($keyword['KeywordStats']['LocalSearchTrendsByMonth'])->map(function ($searchVolume, $month) { |
173
|
|
|
return new StatLocalSearchTrend([ |
174
|
|
|
'month' => strtolower($month), |
175
|
|
|
'search_volume' => ($searchVolume == '-') ? null : $searchVolume, |
176
|
|
|
]); |
177
|
|
|
}); |
178
|
|
|
|
179
|
|
|
$modifiedKeyword->keyword_stats = new StatKeywordStats([ |
180
|
|
|
'advertiser_competition' => $keyword['KeywordStats']['AdvertiserCompetition'], |
181
|
|
|
'global_search_volume' => $keyword['KeywordStats']['GlobalSearchVolume'], |
182
|
|
|
'regional_search_volume' => $keyword['KeywordStats']['RegionalSearchVolume'], |
183
|
|
|
'cpc' => $keyword['KeywordStats']['CPC'], |
184
|
|
|
'local_search_trends_by_month' => $localTrends->values(), |
185
|
|
|
]); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if (is_null($keyword['KeywordRanking'])) { |
189
|
|
|
$modifiedKeyword->keyword_ranking = null; |
190
|
|
|
} else { |
191
|
|
|
$modifiedKeyword->keyword_ranking = new StatKeywordRanking([ |
192
|
|
|
'date' => $keyword['KeywordRanking']['date'], |
193
|
|
|
'google' => new StatKeywordEngineRanking([ |
194
|
|
|
'rank' => $keyword['KeywordRanking']['Google']['Rank'], |
195
|
|
|
'base_rank' => $keyword['KeywordRanking']['Google']['BaseRank'], |
196
|
|
|
'url' => $keyword['KeywordRanking']['Google']['Url'], |
197
|
|
|
]), |
198
|
|
|
'bing' => new StatKeywordEngineRanking([ |
199
|
|
|
'rank' => $keyword['KeywordRanking']['Bing']['Rank'], |
200
|
|
|
'url' => $keyword['KeywordRanking']['Bing']['Url'], |
201
|
|
|
]), |
202
|
|
|
]); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$modifiedKeyword->created_at = $keyword['CreatedAt']; |
206
|
|
|
|
207
|
|
|
return $modifiedKeyword; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.