-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
152 lines (122 loc) · 3 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* This file is used for methods testing. Run this file from console or from web browser
* using the commands below:
*
* Command Line
* php run.php %method%,
*
* %method% - define the method you need to run (for example, cart.create, product.list, etc.).
*
* Browser
* http://localhost/code_sample_php/run.php?method=%method%
*
* %method% - define the method you need to run (for example, cart.create, product.list, etc.).
*
* Using test data of lib/Api2cart/TestData.php
*
* For more details visit http://docs.api2cart.com/
*
*/
include_once 'Api.php';
class Run
{
/**
* @var Api $_api
*/
protected $_api;
public function __construct()
{
$this->_api = new Api('PASTE_YOUR_API_KEY', 'PASTE_YOUR_STORE_KEY');
}
/**
* Validation input method name
*
* @return array
*
* @throws Exception
*/
protected function _validateMethodName()
{
if(isset($_SERVER['argc']) && isset($_SERVER['argc'])) {
if($_SERVER['argc'] == 1) {
throw new Exception('Please specify method name', 1);
}
$method = explode('.', $_SERVER['argv'][1]);
} else {
if(!isset($_GET['method'])) {
throw new Exception('Please specify method name', 1);
}
$method = explode('.', $_GET['method']);
}
if(!isset($method[0]) || empty($method[0]) ||
!isset($method[1]) || empty($method[1])) {
throw new Exception('Method ' . implode('.', $method) . ' not found', 1);
}
if(!method_exists($this->_api, $method[0])) {
throw new Exception('Method ' . implode('.', $method) . ' not found', 1);
}
$section = $method[0];
unset($method[0]);
$methodName = $this->_buildMethodName($method);
$object = $this->_api->$section();
if(!method_exists($object, $methodName)) {
throw new Exception('Method ' . $section .'.' . implode('.', $method) . ' not found', 1);
}
return array(
'section' => $section,
'method' => $methodName
);
}
/**
* Generates function's name from method's name
*
* Example
* product.option.value.add => apiOptionValueAdd
*
* @param array $method
*
* @return string
*/
protected function _buildMethodName($method)
{
$methodName = 'api';
foreach($method as $name) {
$methodName .= ucfirst($name);
}
return $methodName;
}
/**
* Run test method
*/
public function run()
{
try {
$method = $this->_validateMethodName();
/**
* Example
*
* $this->_api->product()->apiList()
* $this->_api->order()->apiAdd()
*/
$result = call_user_func(
array(
call_user_func(
array(
$this->_api,
$method['section']
)
),
$method['method']
),
TestData::data($method)
);
//View result
var_dump($result);
} catch (Exception $e) {
echo "#" . $e->getCode() . " " . $e->getMessage() . "\n";
}
}
}
$run = new Run();
$run->run();