1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2019 - present |
4
|
|
|
* updown - CrudObject.php |
5
|
|
|
* author: Roberto Belotti - [email protected] |
6
|
|
|
* web : robertobelotti.com, github.com/biscolab |
7
|
|
|
* Initial version created on: 15/2/2019 |
8
|
|
|
* MIT license: https://github.com/biscolab/updown-php/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Biscolab\UpDown\Objects; |
12
|
|
|
|
13
|
|
|
use Biscolab\UpDown\Abstracts\AbstractCollection; |
14
|
|
|
use Biscolab\UpDown\Abstracts\AbstractObject; |
15
|
|
|
use Biscolab\UpDown\UpDown; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class CrudObject |
19
|
|
|
* @package Biscolab\UpDown\Abstracts |
20
|
|
|
*/ |
21
|
|
|
class CrudObject extends AbstractObject |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected static $endpoint = ''; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected static $collection_type = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $key = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* CrudObject constructor. |
41
|
|
|
* |
42
|
|
|
* @param mixed $args |
43
|
|
|
*/ |
44
|
|
|
public function __construct($args = []) |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
if (!is_array($args)) { |
48
|
|
|
$args_[self::getPrimaryKey()] = $args; |
|
|
|
|
49
|
|
|
} else { |
50
|
|
|
$args_ = $args; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
parent::__construct($args_); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public static function getPrimaryKey(): string |
60
|
|
|
{ |
61
|
|
|
|
62
|
|
|
return 'token'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return null|AbstractCollection |
67
|
|
|
*/ |
68
|
|
|
public function all(): AbstractCollection |
69
|
|
|
{ |
70
|
|
|
|
71
|
|
|
$path = static::getEndpoint(); |
72
|
|
|
|
73
|
|
|
$response = $this->updown->get($path); |
|
|
|
|
74
|
|
|
|
75
|
|
|
if (static::$collection_type) { |
76
|
|
|
$collection_type = static::$collection_type; |
77
|
|
|
|
78
|
|
|
return new $collection_type($response->getResult()->getData()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return null; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected static function getEndpoint(): string |
88
|
|
|
{ |
89
|
|
|
|
90
|
|
|
return UpDown::API_URL . static::$endpoint; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array|null $params |
95
|
|
|
* |
96
|
|
|
* @return AbstractObject |
97
|
|
|
*/ |
98
|
|
|
public function create(?array $params = []): AbstractObject |
99
|
|
|
{ |
100
|
|
|
|
101
|
|
|
if (empty($params)) { |
102
|
|
|
$params = $this->attributes; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$path = $this->prepareApiPath(false); |
106
|
|
|
|
107
|
|
|
$response = $this->updown->post($path, $params); |
108
|
|
|
|
109
|
|
|
$this->setArgs($response->getResult()->getData()); |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param bool|null $with_id |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function prepareApiPath(?bool $with_id = true): string |
120
|
|
|
{ |
121
|
|
|
|
122
|
|
|
$path = static::getEndpoint(); |
123
|
|
|
|
124
|
|
|
if ($with_id) { |
125
|
|
|
$path .= '/' . $this->getId(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $path; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public function getId() |
135
|
|
|
{ |
136
|
|
|
|
137
|
|
|
return $this->{static::getPrimaryKey()}; |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return AbstractObject |
143
|
|
|
*/ |
144
|
|
|
public function read(): AbstractObject |
145
|
|
|
{ |
146
|
|
|
|
147
|
|
|
$path = $this->prepareApiPath(); |
148
|
|
|
|
149
|
|
|
$response = $this->updown->get($path); |
150
|
|
|
|
151
|
|
|
$this->setArgs($response->getResult()->getData()); |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $params |
158
|
|
|
* |
159
|
|
|
* @return AbstractObject |
160
|
|
|
*/ |
161
|
|
|
public function update(?array $params = []): AbstractObject |
162
|
|
|
{ |
163
|
|
|
|
164
|
|
|
if (empty($params)) { |
165
|
|
|
$params = $this->attributes; |
166
|
|
|
$this->setArgs($params); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$path = $this->prepareApiPath(); |
170
|
|
|
|
171
|
|
|
$response = $this->updown->put($path, $params); |
172
|
|
|
|
173
|
|
|
$this->setArgs($response->getResult()->getData()); |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function delete(): bool |
182
|
|
|
{ |
183
|
|
|
|
184
|
|
|
$path = $this->prepareApiPath(); |
185
|
|
|
|
186
|
|
|
$response = $this->updown->delete($path); |
187
|
|
|
|
188
|
|
|
$result = $response->getResult()->getData(); |
189
|
|
|
|
190
|
|
|
if ($result['deleted']) { |
191
|
|
|
return $result['deleted']; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function getStaticEndpoint(): string |
201
|
|
|
{ |
202
|
|
|
|
203
|
|
|
return static::getEndpoint(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
} |