Repository navigation
Expand file tree
/
Copy path010.php
More file actions
48 lines (38 loc) · 1.35 KB
/
Copy path010.php
File metadata and controls
48 lines (38 loc) · 1.35 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
<?php
namespace App\Transformers;
use App\Transformers\CommentTransformer;
use App\Transformers\PostTransformer;
use CodeIgniter\API\BaseTransformer;
class UserTransformer extends BaseTransformer
{
public function toArray(mixed $resource): array
{
return [
'id' => $resource['id'],
'name' => $resource['name'],
'email' => $resource['email'],
];
}
protected function getAllowedIncludes(): ?array
{
// Only these relationships can be included
return ['posts', 'comments'];
}
protected function includePosts(): array
{
$posts = model('PostModel')->where('user_id', $this->resource['id'])->findAll();
return $this->transformRelated(PostTransformer::class, $posts);
}
protected function includeComments(): array
{
$comments = model('CommentModel')->where('user_id', $this->resource['id'])->findAll();
return $this->transformRelated(CommentTransformer::class, $comments);
}
protected function includeOrders(): array
{
// This method exists but won't be callable from the API
// because 'orders' is not in getAllowedIncludes()
$orders = model('OrderModel')->where('user_id', $this->resource['id'])->findAll();
return $this->transformRelated(OrderTransformer::class, $orders);
}
}