|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hborras\TwitterAdsSDK\TwitterAds; |
|
4
|
|
|
|
|
5
|
|
|
use Hborras\TwitterAdsSDK\DateTime\DateTimeFormatter; |
|
6
|
|
|
use Hborras\TwitterAdsSDK\TwitterAds; |
|
7
|
|
|
use Hborras\TwitterAdsSDK\TwitterAds\Errors\ServerError; |
|
8
|
|
|
use Hborras\TwitterAdsSDK\TwitterAdsException; |
|
9
|
|
|
use Hborras\TwitterAdsSDK\Arrayable; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Created by PhpStorm. |
|
13
|
|
|
* User: hborras |
|
14
|
|
|
* Date: 2/04/16 |
|
15
|
|
|
* Time: 12:17. |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class Resource implements Arrayable |
|
18
|
|
|
{ |
|
19
|
|
|
use DateTimeFormatter; |
|
20
|
|
|
|
|
21
|
|
|
const RESOURCE = ''; |
|
22
|
|
|
const RESOURCE_COLLECTION = ''; |
|
23
|
|
|
const RESOURCE_ID_REPLACE = '{id}'; |
|
24
|
|
|
const RESOURCE_REPLACE = '{account_id}'; |
|
25
|
|
|
|
|
26
|
|
|
private $properties = []; |
|
27
|
|
|
|
|
28
|
|
|
/** @var TwitterAds $twitterAds */ |
|
29
|
|
|
private $twitterAds; |
|
30
|
|
|
|
|
31
|
|
|
abstract public function getId(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Automatically set the account if this class is not an account |
|
35
|
|
|
* Resource constructor. |
|
36
|
|
|
* @param null $id |
|
37
|
|
|
* @param TwitterAds $twitterAds |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct($id = null, TwitterAds $twitterAds = null) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->id = $id; |
|
|
|
|
|
|
42
|
|
|
$this->twitterAds = static::assureApi($twitterAds); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected static function assureApi(TwitterAds $instance = null) |
|
46
|
|
|
{ |
|
47
|
|
|
$instance = $instance ?: TwitterAds::instance(); |
|
48
|
|
|
if (!$instance) { |
|
49
|
|
|
throw new \InvalidArgumentException( |
|
50
|
|
|
'An Api instance must be provided as argument or ' . |
|
51
|
|
|
'set as instance in the \TwitterAds\Api'); |
|
52
|
|
|
} |
|
53
|
|
|
return $instance; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns a Cursor instance for a given resource. |
|
58
|
|
|
* |
|
59
|
|
|
* @param $params |
|
60
|
|
|
* |
|
61
|
|
|
* @return Cursor |
|
62
|
|
|
*/ |
|
63
|
|
View Code Duplication |
public function all($params = []) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE_COLLECTION); |
|
66
|
|
|
$response = $this->getTwitterAds()->get($resource, $params); |
|
67
|
|
|
|
|
68
|
|
|
return new Cursor($this, $this->getTwitterAds(), $response->getBody(), $params); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param $params |
|
73
|
|
|
* @return Resource |
|
74
|
|
|
*/ |
|
75
|
|
|
public function read($params = []) |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->load($this->getId(), $params); |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns an object instance for a given resource. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $id |
|
85
|
|
|
* @param $params |
|
86
|
|
|
* |
|
87
|
|
|
* @return Resource |
|
88
|
|
|
*/ |
|
89
|
|
View Code Duplication |
public function load($id, $params = []) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE); |
|
92
|
|
|
$resource = str_replace(static::RESOURCE_ID_REPLACE, $id, $resource); |
|
93
|
|
|
$response = $this->getTwitterAds()->get($resource, $params); |
|
94
|
|
|
|
|
95
|
|
|
return $this->fromResponse($response->getBody()->data); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Reloads all attributes for the current object instance from the API. |
|
100
|
|
|
* |
|
101
|
|
|
* @param $params |
|
102
|
|
|
* @return Resource |
|
103
|
|
|
* @throws ServerError |
|
104
|
|
|
*/ |
|
105
|
|
|
public function reload($params = []) |
|
106
|
|
|
{ |
|
107
|
|
|
if (!$this->getId()) { |
|
108
|
|
|
throw new ServerError(TwitterAdsException::SERVER_ERROR, "Error loading entity", null, null); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE); |
|
112
|
|
|
$resource = str_replace(static::RESOURCE_ID_REPLACE, $this->getId(), $resource); |
|
113
|
|
|
$response = $this->getTwitterAds()->get($resource, $params); |
|
114
|
|
|
|
|
115
|
|
|
return $this->fromResponse($response->getBody()->data); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Populates a given objects attributes from a parsed JSON API response. |
|
120
|
|
|
* This helper handles all necessary type coercions as it assigns attribute values. |
|
121
|
|
|
* |
|
122
|
|
|
* @param $response |
|
123
|
|
|
* |
|
124
|
|
|
* @return $this |
|
125
|
|
|
*/ |
|
126
|
|
|
public function fromResponse($response) |
|
127
|
|
|
{ |
|
128
|
|
|
foreach (get_object_vars($response) as $key => $value) { |
|
129
|
|
|
if (($key == 'created_at' || $key == 'updated_at' || $key == 'start_time' || $key == 'end_time' || $key == 'timezone_switch_at') && !is_null($value)) { |
|
130
|
|
|
$this->$key = $this->toDateTimeImmutable($value); |
|
131
|
|
|
} else { |
|
132
|
|
|
$this->$key = $value; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Generates a Hash of property values for the current object. This helper |
|
141
|
|
|
* handles all necessary type coercions as it generates its output. |
|
142
|
|
|
*/ |
|
143
|
|
|
public function toParams() |
|
144
|
|
|
{ |
|
145
|
|
|
$params = []; |
|
146
|
|
|
foreach ($this->getProperties() as $property) { |
|
147
|
|
|
if (is_null($this->$property)) { |
|
148
|
|
|
continue; |
|
149
|
|
|
} |
|
150
|
|
|
if ($this->$property instanceof \DateTimeInterface) { |
|
151
|
|
|
$params[$property] = $this->$property->format('c'); |
|
152
|
|
|
} elseif (is_array($this->$property)) { |
|
153
|
|
|
$params[$property] = implode(',', $this->$property); |
|
154
|
|
|
} elseif (is_bool($this->$property)) { |
|
155
|
|
|
$params[$property] = $this->$property ? 'true' : 'false'; |
|
156
|
|
|
} else { |
|
157
|
|
|
$params[$property] = strval($this->$property); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $params; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function validateLoaded() |
|
165
|
|
|
{ |
|
166
|
|
|
if (!$this->getId()) { |
|
167
|
|
|
throw new ServerError(TwitterAdsException::SERVER_ERROR, "Error loading entity", null, null); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function toArray() |
|
172
|
|
|
{ |
|
173
|
|
|
$data = []; |
|
174
|
|
|
$vars = get_object_vars($this); |
|
175
|
|
|
foreach ($vars as $key => $var) { |
|
176
|
|
|
if ($var instanceof $this) { |
|
177
|
|
|
continue; |
|
178
|
|
|
} |
|
179
|
|
|
$data[$key] = $var; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $data; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function loadResource($id = '', $params = []) |
|
186
|
|
|
{ |
|
187
|
|
|
if ($id != '') { |
|
188
|
|
|
return $this->load($id, $params); |
|
189
|
|
|
} else { |
|
190
|
|
|
return $this->all($params); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Saves or updates the current object instance depending on the |
|
196
|
|
|
* presence of `object->getId()`. |
|
197
|
|
|
*/ |
|
198
|
|
|
public function save() |
|
199
|
|
|
{ |
|
200
|
|
|
if ($this->getId()) { |
|
201
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE); |
|
202
|
|
|
$resource = str_replace(static::RESOURCE_ID_REPLACE, $this->getId(), $resource); |
|
203
|
|
|
$response = $this->getTwitterAds()->put($resource, $this->toParams()); |
|
204
|
|
|
} else { |
|
205
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE_COLLECTION); |
|
206
|
|
|
$response = $this->getTwitterAds()->post($resource, $this->toParams()); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $this->fromResponse($response->getBody()->data); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Deletes the current object instance depending on the |
|
214
|
|
|
* presence of `object->getId()`. |
|
215
|
|
|
*/ |
|
216
|
|
View Code Duplication |
public function delete() |
|
|
|
|
|
|
217
|
|
|
{ |
|
218
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE); |
|
219
|
|
|
$resource = str_replace(static::RESOURCE_ID_REPLACE, $this->getId(), $resource); |
|
220
|
|
|
$response = $this->getTwitterAds()->delete($resource); |
|
221
|
|
|
$this->fromResponse($response->getBody()->data); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @return array |
|
226
|
|
|
*/ |
|
227
|
|
|
public function getProperties() |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->properties; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return TwitterAds |
|
235
|
|
|
*/ |
|
236
|
|
|
public function getTwitterAds() |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->twitterAds; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: