|
1
|
|
|
<?php |
|
2
|
|
|
namespace Thunder\SimilarWebApi; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
abstract class AbstractRequest |
|
8
|
|
|
{ |
|
9
|
|
|
const API = 'http://api.similarweb.com'; |
|
10
|
|
|
|
|
11
|
|
|
protected static $appStores = array('google_play_store' => '0', 'app_store' => '1'); |
|
12
|
|
|
protected static $arguments = array('app', 'domain', 'period', 'start', 'end', 'main', 'store', 'page'); |
|
13
|
|
|
protected static $periods = array('daily', 'weekly', 'monthly'); |
|
14
|
|
|
|
|
15
|
|
|
protected $args = array(); |
|
16
|
|
|
|
|
17
|
67 |
|
public function getCallUrl($format, $token) |
|
18
|
|
|
{ |
|
19
|
67 |
|
$args = $this->args; |
|
20
|
67 |
|
$args['format'] = $format; |
|
21
|
67 |
|
$args['token'] = $token; |
|
22
|
67 |
|
$keys = array_map(function($item) { |
|
23
|
67 |
|
return '{'.$item.'}'; |
|
24
|
67 |
|
}, array_keys($args)); |
|
25
|
67 |
|
$values = array_values($args); |
|
26
|
|
|
|
|
27
|
67 |
|
return str_replace($keys, $values, $this->getUrl()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
75 |
|
protected function validateArg($arg, $value) |
|
31
|
|
|
{ |
|
32
|
75 |
|
if('domain' == $arg && !$value) |
|
33
|
75 |
|
{ |
|
34
|
1 |
|
throw new \InvalidArgumentException('Empty domain!'); |
|
35
|
|
|
} |
|
36
|
74 |
|
elseif('app' == $arg && !$value) |
|
37
|
|
|
{ |
|
38
|
1 |
|
throw new \InvalidArgumentException('Empty app name!'); |
|
39
|
|
|
} |
|
40
|
74 |
|
elseif('period' == $arg && !in_array($value, static::$periods)) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$periods = implode(', ', static::$periods); |
|
43
|
1 |
|
$msg = 'Invalid period %s, expected one of %s!'; |
|
44
|
1 |
|
throw new \InvalidArgumentException(sprintf($msg, $value, $periods)); |
|
45
|
|
|
} |
|
46
|
74 |
|
elseif(in_array($arg, array('start', 'end')) && !preg_match('/[0-9]{2}-[0-9]{4}/', $value)) |
|
47
|
|
|
{ |
|
48
|
2 |
|
$msg = 'Invalid %s date %s, expected format MM-YYYY!'; |
|
49
|
2 |
|
throw new \InvalidArgumentException(sprintf($msg, $arg, $value)); |
|
50
|
|
|
} |
|
51
|
74 |
|
elseif('main' == $arg) |
|
52
|
|
|
{ |
|
53
|
19 |
|
$value = (bool)$value ? 'true' : 'false'; |
|
54
|
19 |
|
} |
|
55
|
74 |
|
elseif('page' == $arg && !ctype_digit((string)$value)) |
|
56
|
|
|
{ |
|
57
|
1 |
|
$msg = 'Invalid page number %s!'; |
|
58
|
1 |
|
throw new \InvalidArgumentException(sprintf($msg, $value)); |
|
59
|
|
|
} |
|
60
|
74 |
|
elseif('store' == $arg && !in_array($value, static::$appStores)) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$msg = 'Invalid app store ID %s!'; |
|
63
|
1 |
|
throw new \InvalidArgumentException(sprintf($msg, $value)); |
|
64
|
|
|
} |
|
65
|
73 |
|
else if(!in_array($arg, static::$arguments)) |
|
66
|
73 |
|
{ |
|
67
|
1 |
|
$msg = 'Unknown argument %s (value %s)!'; |
|
68
|
1 |
|
throw new \InvalidArgumentException(sprintf($msg, $arg, $value)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
72 |
|
return $value; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string API endpoint name as in documentation |
|
76
|
|
|
*/ |
|
77
|
|
|
abstract public function getName(); |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string API call URL with parameter placeholders |
|
81
|
|
|
*/ |
|
82
|
|
|
abstract public function getUrl(); |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return array Mapping data for response parsing |
|
86
|
|
|
*/ |
|
87
|
|
|
abstract public function getMapping(); |
|
88
|
|
|
} |
|
89
|
|
|
|