1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FacebookAds; |
4
|
|
|
|
5
|
|
|
use FacebookAds\Cursor; |
6
|
|
|
use FacebookAds\AbstractObject; |
7
|
|
|
|
8
|
|
|
class ApiRequest { |
9
|
|
|
protected $api; |
10
|
|
|
protected $id; |
11
|
|
|
protected $method; |
12
|
|
|
protected $endpoint; |
13
|
|
|
protected $return_prototype; |
|
|
|
|
14
|
|
|
protected $accepted_fields; |
|
|
|
|
15
|
|
|
protected $param_checker; |
|
|
|
|
16
|
|
|
protected $api_type; |
|
|
|
|
17
|
|
|
protected $use_graph_video_endpoint; |
|
|
|
|
18
|
|
|
private $fields; |
19
|
|
|
private $params; |
20
|
|
|
private $file_params; |
|
|
|
|
21
|
|
|
private $allow_file_upload; |
|
|
|
|
22
|
|
|
private $file_counter; |
|
|
|
|
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
Api $api, |
26
|
|
|
$id, |
27
|
|
|
$method, |
28
|
|
|
$endpoint, |
29
|
|
|
$return_prototype = null, |
30
|
|
|
$api_type = null, |
31
|
|
|
$accepted_fields = array(), |
32
|
|
|
TypeChecker $param_checker = null, |
33
|
|
|
$allow_file_upload = false, |
34
|
|
|
$use_graph_video_endpoint = false) { |
35
|
|
|
$this->fields = []; |
36
|
|
|
$this->params = []; |
37
|
|
|
$this->file_params = []; |
38
|
|
|
$this->file_counter = 0; |
39
|
|
|
$this->api = $api; |
40
|
|
|
$this->id = $id; |
41
|
|
|
$this->method = $method; |
42
|
|
|
$this->endpoint = $endpoint; |
43
|
|
|
$this->return_prototype = $return_prototype; |
44
|
|
|
$this->api_type = $api_type; |
45
|
|
|
$this->accepted_fields = $accepted_fields; |
46
|
|
|
$this->param_checker = $param_checker; |
47
|
|
|
$this->allow_file_upload = $allow_file_upload; |
48
|
|
|
$this->use_graph_video_endpoint = $use_graph_video_endpoint; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function addParam($param, $value) { |
52
|
|
|
$extracted_value = $this->extractValue($value); |
53
|
|
|
if (!ApiConfig::TYPE_CHECKER_STRICT_MODE |
54
|
|
|
|| !$this->param_checker->isValidParam($param) |
55
|
|
|
) { |
56
|
|
|
$this->params[$param] = $extracted_value; |
57
|
|
|
} else { |
58
|
|
|
if ($this->param_checker->isValidParamPair($param, $value)) { |
59
|
|
|
if ($this->param_checker->isFileParam($param)) { |
60
|
|
|
$this->file_params[$param] = $extracted_value; |
61
|
|
|
} else { |
62
|
|
|
$this->params[$param] = $extracted_value; |
63
|
|
|
} |
64
|
|
|
} elseif ($this->param_checker->isPrimitiveType($param)) { |
65
|
|
|
$param_type = $this->param_checker->getType($param); |
66
|
|
|
$this->params[$param] = $this->param_checker->convertStringToPrimType( |
67
|
|
|
$param_type, $value); |
68
|
|
|
} else { |
69
|
|
|
throw new \LogicException('The value for '.$param.' is not compatible'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function addParams($params) { |
76
|
|
|
foreach ($params as $key => $value) { |
77
|
|
|
$this->addParam($key, $value); |
78
|
|
|
} |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function removeParam($param) { |
83
|
|
|
if (array_key_exists($param, $this->params)) { |
84
|
|
|
unset($this->params[$param]); |
85
|
|
|
} elseif (array_key_exists($param, $this->params)) { |
86
|
|
|
unset($this->file_params[$param]); |
87
|
|
|
} |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function clearParams() { |
92
|
|
|
$this->params = []; |
93
|
|
|
$this->file_params = []; |
94
|
|
|
return this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getParams() { |
98
|
|
|
$all_params = array_merge($this->params, $this->file_params); |
99
|
|
|
return $all_params; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function addField($field) { |
103
|
|
|
if (ApiConfig::TYPE_CHECKER_STRICT_MODE |
104
|
|
|
&& !in_array($field, $this->accepted_fields) |
105
|
|
|
) { |
106
|
|
|
throw new \LogicException('Field '.$field.' is not supported'); |
107
|
|
|
} |
108
|
|
|
if (!(in_array($field, $this->fields))) { |
109
|
|
|
$this->fields[] = $field; |
110
|
|
|
} |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function addFields($fields) { |
115
|
|
|
foreach ($fields as $field) { |
116
|
|
|
$this->addField($field); |
117
|
|
|
} |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function removeField($field) { |
122
|
|
|
if (in_array($field, $this->fields)) { |
123
|
|
|
$index_to_remove = array_search($field, $this->fields); |
124
|
|
|
unset($this->fields[$index_to_remove]); |
125
|
|
|
} |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function clearFields() { |
130
|
|
|
$this->fields = []; |
131
|
|
|
return this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getFields() { |
135
|
|
|
return $this->fields; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function addFile($filename) { |
139
|
|
|
if (ApiConfig::TYPE_CHECKER_STRICT_MODE && !$this->allow_file_upload) { |
140
|
|
|
throw new \LogicException("This api cannot upload files"); |
141
|
|
|
} |
142
|
|
|
$file_key = 'source'.$this->file_counter; |
143
|
|
|
if (file_exists($filename)) { |
144
|
|
|
$this->file_params[$file_key] = $filename; |
145
|
|
|
$this->file_counter++; |
146
|
|
|
} |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Execute the request |
152
|
|
|
*/ |
153
|
|
|
public function execute() { |
154
|
|
|
$url_path = '/'.$this->id.$this->endpoint; |
155
|
|
|
$updated_params = $this->params; |
156
|
|
|
if (!empty($this->fields)) { |
157
|
|
|
$fields = implode(',', $this->fields); |
158
|
|
|
$updated_params['fields'] = $fields; |
159
|
|
|
} |
160
|
|
|
$response = $this->api->call( |
161
|
|
|
$url_path, $this->method, $updated_params, $this->file_params); |
|
|
|
|
162
|
|
|
if ($this->api_type === "EDGE" && $this->method === "GET") { |
163
|
|
|
return new Cursor($response, $this->return_prototype, $this->api); |
164
|
|
|
} else if ($this->method === "DELETE") { |
165
|
|
|
return $response; |
166
|
|
|
} else { |
167
|
|
|
return $this->createObject($response->getContent()); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function extractValue($value) { |
172
|
|
|
if ($this->param_checker->isAbstractObject($value)) { |
173
|
|
|
return $value->exportAllData(); |
174
|
|
|
} else if (is_array($value)) { |
175
|
|
|
$extracted_value = []; |
176
|
|
|
foreach ($value as $key => $sub_value) { |
177
|
|
|
$extracted_value[$key] = $this->extractValue($sub_value); |
178
|
|
|
} |
179
|
|
|
return $extracted_value; |
180
|
|
|
} else { |
181
|
|
|
return $value; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
protected function createObject(array $object_data) { |
186
|
|
|
$object = clone $this->return_prototype; |
187
|
|
|
$object->setDataWithoutValidation($object_data); |
188
|
|
|
if ($object instanceof AbstractCrudObject) { |
|
|
|
|
189
|
|
|
$object->setApi($this->api); |
190
|
|
|
} |
191
|
|
|
return $object; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.