-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAbstractRepository.php
More file actions
282 lines (242 loc) · 6.45 KB
/
AbstractRepository.php
File metadata and controls
282 lines (242 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
namespace OWC\OpenPub\Base\Repositories;
use Closure;
use OWC\OpenPub\Base\Exceptions\PropertyNotExistsException;
use OWC\OpenPub\Base\Support\CreatesFields;
use WP_Post;
use WP_Query;
abstract class AbstractRepository
{
protected string $posttype;
/**
* Instance of the WP_Query object.
*
* @var null|WP_Query
*/
protected $query = null;
/**
* Arguments for the WP_Query.
*/
protected array $queryArgs = [];
/**
* Fields that need to be hidden.
*/
protected array $hidden = [];
/**
* Dynamically added fields.
*/
protected array $fields = [];
/**
* Additional fields that needs to be added to an item.
*
* @var CreatesFields[]
*/
protected static array $globalFields = [];
/**
* Construct a new Model class.
*
* @throws PropertyNotExistsException
* @throws \ReflectionException
*/
public function __construct()
{
/**
* In order to register globalFields for a specific model the static property
* "globalFields" must be present on the derived class to prevent adding fields to
* the abstract Model class.
*/
$reflect = new \ReflectionClass(static::class);
if (__CLASS__ == $reflect->getProperty('globalFields')->class) {
throw new PropertyNotExistsException(sprintf(
'Property $globalFields must be present on derived class %s.',
static::class
));
}
}
/**
* Get all the items from the database.
*
* @return array
*/
public function all(): array
{
$args = array_merge($this->queryArgs, [
'post_type' => [$this->posttype],
]);
$this->query = new WP_Query($args);
return array_map([$this, 'transform'], $this->getQuery()->posts);
}
/**
* Find a particular openpub item by ID.
*
* @param int $id
*
* @return array|null
*/
public function find(int $id)
{
$args = array_merge($this->queryArgs, [
'p' => $id,
'post_type' => [$this->posttype],
]);
$this->query = new WP_Query($args);
if (empty($this->getQuery()->posts)) {
return null;
}
return $this->transform(reset($this->getQuery()->posts));
}
/**
* Find a particular pdc item by slug.
*
* @param string $slug
*
* @return array|null
*/
public function findBySlug(string $slug)
{
$args = array_merge($this->queryArgs, [
'name' => $slug,
'post_type' => [$this->posttype],
]);
$this->query = new WP_Query($args);
if (empty($this->getQuery()->posts)) {
return null;
}
return $this->transform(reset($this->getQuery()->posts));
}
/**
* Get the WP_Query object.
*
* @return null|WP_Query
*/
public function getQuery()
{
return $this->query;
}
/**
* Add additional query arguments.
*/
public function query(array $args): AbstractRepository
{
$this->queryArgs = array_merge_recursive($this->queryArgs, $args);
return $this;
}
/**
* Returns the query args.
*
* @return array
*/
public function getQueryArgs()
{
return $this->queryArgs;
}
/**
* Hide a particular key from the request.
*
* @param array $keys
*
* @return $this
*/
public function hide(array $keys)
{
$this->hidden = array_merge($this->hidden, $keys);
return $this;
}
/**
* Dynamically add additional fields to the model.
*
* @param string $key
* @param CreatesFields $creator
*
* @return $this
*/
public function addField(string $key, CreatesFields $creator)
{
$this->fields[$key] = $creator;
return $this;
}
/**
* Adds a new field to the output.
*
* @param string $key
* @param CreatesFields $creator
* @param Closure|null $conditional
*
* @return void
*/
public static function addGlobalField(string $key, CreatesFields $creator, Closure $conditional = null): void
{
static::$globalFields[] = [
'key' => $key,
'creator' => $creator,
'conditional' => $conditional,
];
}
/**
* Get all defined global fields of the Model.
*
* @return array
*/
public static function getGlobalFields(): array
{
uasort(static::$globalFields, function ($a, $b) {
return ($a['key'] < $b['key']) ? -1 : 1;
});
return static::$globalFields;
}
/**
* Transform a single WP_Post item.
*
* @param WP_Post $post
*
* @return array
*/
public function transform(WP_Post $post)
{
$GLOBALS['post'] = $post;
setup_postdata($post);
$data = [
'id' => $post->ID,
'title' => $post->post_title,
'content' => apply_filters('the_content', $post->post_content),
'excerpt' => $post->post_excerpt,
'date' => $post->post_date,
'slug' => $post->post_name,
'post_status' => $post->post_status
];
$data = $this->assignFields($data, $post);
wp_reset_postdata();
return $data;
}
/**
* Assign fields to the data array.
*
* @param array $data
* @param WP_Post $post
*
* @return array
*/
protected function assignFields(array $data, WP_Post $post)
{
// Assign global fields.
foreach (static::getGlobalFields() as $field) {
if (in_array($field['key'], $this->hidden)) {
continue;
}
if (is_null($field['conditional'])) {
// If the field has no conditional set we will add it
$data[$field['key']] = $field['creator']->create($post);
} else {
// Check if the conditional matches.
if ($field['conditional']($post)) {
$data[$field['key']] = $field['creator']->create($post);
}
}
}
// Assign dynamic fields.
foreach ($this->fields as $key => $creator) {
$data[$key] = $creator->create($post);
}
return $data;
}
}