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