1 | <?php |
||
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) |
||
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 = []) |
||
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 = []) |
||
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) |
||
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() |
||
139 | |||
140 | public function validateLoaded() |
||
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() |
||
187 | |||
188 | /** |
||
189 | * @return Account |
||
190 | */ |
||
191 | public function getAccount() |
||
195 | |||
196 | /** |
||
197 | * @param Account $account |
||
198 | */ |
||
199 | public function setAccount($account = null) |
||
203 | |||
204 | /** |
||
205 | * @return array |
||
206 | */ |
||
207 | public function getProperties() |
||
211 | } |
||
212 |