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
|
|
|
$instance = $instance ?: TwitterAds::instance(); |
47
|
|
|
if (!$instance) { |
48
|
|
|
throw new \InvalidArgumentException( |
49
|
|
|
'An Api instance must be provided as argument or '. |
50
|
|
|
'set as instance in the \TwitterAds\Api'); |
51
|
|
|
} |
52
|
|
|
return $instance; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns a Cursor instance for a given resource. |
57
|
|
|
* |
58
|
|
|
* @param $params |
59
|
|
|
* |
60
|
|
|
* @return Cursor |
61
|
|
|
*/ |
62
|
|
|
public function all($params = []) |
63
|
|
|
{ |
64
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE_COLLECTION); |
65
|
|
|
$response = $this->getTwitterAds()->get($resource, $params); |
66
|
|
|
|
67
|
|
|
return new Cursor($this, $this->getTwitterAds(), $response->getBody(), $params); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $params |
72
|
|
|
* @return Resource |
73
|
|
|
*/ |
74
|
|
|
public function read($params = []) |
75
|
|
|
{ |
76
|
|
|
return $this->load($this->getId(), $params); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns an object instance for a given resource. |
81
|
|
|
* |
82
|
|
|
* @param string $id |
83
|
|
|
* @param $params |
84
|
|
|
* |
85
|
|
|
* @return Resource |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function load($id, $params = []) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE); |
90
|
|
|
$resource = str_replace(static::RESOURCE_ID_REPLACE, $id, $resource); |
91
|
|
|
$response = $this->getTwitterAds()->get($resource, $params); |
92
|
|
|
|
93
|
|
|
return $this->fromResponse($response->getBody()->data); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Reloads all attributes for the current object instance from the API. |
98
|
|
|
* |
99
|
|
|
* @param $params |
100
|
|
|
* @return Resource |
101
|
|
|
* @throws ServerError |
102
|
|
|
*/ |
103
|
|
|
public function reload($params = []) |
104
|
|
|
{ |
105
|
|
|
if (!$this->getId()) { |
106
|
|
|
throw new ServerError(TwitterAdsException::SERVER_ERROR, "Error loading entity", null, null); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE); |
110
|
|
|
$resource = str_replace(static::RESOURCE_ID_REPLACE, $this->getId(), $resource); |
111
|
|
|
$response = $this->getTwitterAds()->get($resource, $params); |
112
|
|
|
|
113
|
|
|
return $this->fromResponse($response->getBody()->data); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Populates a given objects attributes from a parsed JSON API response. |
118
|
|
|
* This helper handles all necessary type coercions as it assigns attribute values. |
119
|
|
|
* |
120
|
|
|
* @param $response |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function fromResponse($response) |
125
|
|
|
{ |
126
|
|
|
foreach (get_object_vars($response) as $key => $value) { |
127
|
|
|
if (($key == 'created_at' || $key == 'updated_at' || $key == 'start_time' || $key == 'end_time' || $key == 'timezone_switch_at') && !is_null($value)) { |
128
|
|
|
$this->$key = $this->toDateTimeImmutable($value); |
129
|
|
|
} else { |
130
|
|
|
$this->$key = $value; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Generates a Hash of property values for the current object. This helper |
139
|
|
|
* handles all necessary type coercions as it generates its output. |
140
|
|
|
*/ |
141
|
|
|
public function toParams() |
142
|
|
|
{ |
143
|
|
|
$params = []; |
144
|
|
|
foreach ($this->getProperties() as $property) { |
145
|
|
|
if (is_null($this->$property)) { |
146
|
|
|
continue; |
147
|
|
|
} |
148
|
|
|
if ($this->$property instanceof \DateTimeInterface) { |
149
|
|
|
$params[$property] = $this->$property->format('c'); |
150
|
|
|
} elseif (is_array($this->$property)) { |
151
|
|
|
$params[$property] = implode(',', $this->$property); |
152
|
|
|
} elseif (is_bool($this->$property)) { |
153
|
|
|
$params[$property] = $this->$property ? 'true' : 'false'; |
154
|
|
|
} else { |
155
|
|
|
$params[$property] = strval($this->$property); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $params; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function validateLoaded() |
163
|
|
|
{ |
164
|
|
|
if (!$this->getId()) { |
165
|
|
|
throw new ServerError(TwitterAdsException::SERVER_ERROR, "Error loading entity", null, null); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function toArray() |
170
|
|
|
{ |
171
|
|
|
$data = []; |
172
|
|
|
$vars = get_object_vars($this); |
173
|
|
|
|
174
|
|
|
foreach ($vars as $key => $var) { |
175
|
|
|
if ($var instanceof $this) { |
176
|
|
|
continue; |
177
|
|
|
} |
178
|
|
|
$data[$key] = $var; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $data; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function loadResource($id = '', $params = []) |
185
|
|
|
{ |
186
|
|
|
if ($id != '') { |
187
|
|
|
return $this->load($id, $params); |
188
|
|
|
} else { |
189
|
|
|
return $this->all($params); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Saves or updates the current object instance depending on the |
195
|
|
|
* presence of `object->getId()`. |
196
|
|
|
*/ |
197
|
|
|
public function save() |
198
|
|
|
{ |
199
|
|
|
if ($this->getId()) { |
200
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE); |
201
|
|
|
$resource = str_replace(static::RESOURCE_ID_REPLACE, $this->getId(), $resource); |
202
|
|
|
$response = $this->getTwitterAds()->put($resource, $this->toParams()); |
203
|
|
|
} else { |
204
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE_COLLECTION); |
205
|
|
|
$response = $this->getTwitterAds()->post($resource, $this->toParams()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $this->fromResponse($response->getBody()->data); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Deletes the current object instance depending on the |
213
|
|
|
* presence of `object->getId()`. |
214
|
|
|
*/ |
215
|
|
View Code Duplication |
public function delete() |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE); |
218
|
|
|
$resource = str_replace(static::RESOURCE_ID_REPLACE, $this->getId(), $resource); |
219
|
|
|
$response = $this->getTwitterAds()->delete($resource); |
220
|
|
|
$this->fromResponse($response->getBody()->data); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
public function getProperties() |
227
|
|
|
{ |
228
|
|
|
return $this->properties; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return TwitterAds |
234
|
|
|
*/ |
235
|
|
|
public function getTwitterAds() |
236
|
|
|
{ |
237
|
|
|
return $this->twitterAds; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
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: