Completed
Push — main ( c5c3a9...be9d9d )
by Tan
18s queued 14s
created

AuthorWithPostResource::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 15
rs 9.9
1
<?php
2
3
namespace CSlant\Blog\Api\Http\Resources\Author;
4
5
use CSlant\Blog\Api\Http\Resources\Post\ListPostResourceCollection;
6
use CSlant\Blog\Core\Models\User;
7
use Illuminate\Http\Resources\Json\JsonResource;
8
9
/**
10
 * @mixin User
11
 */
12
class AuthorWithPostResource extends JsonResource
13
{
14
    /**
15
     * @param $request
16
     *
17
     * @return array<string, mixed>
18
     */
19
    public function toArray($request): array
20
    {
21
        /** @var User $this */
22
        $posts = $this->posts()
23
            ->orderBy($request->get('order_by', 'created_at'), $request->get('order', 'DESC'))
24
            ->paginate($request->get('per_page', 10));
25
26
        return [
27
            'id' => $this->id,
28
            'email' => $this->email,
29
            'first_name' => $this->first_name,
30
            'last_name' => $this->last_name,
31
            'username' => $this->username,
32
            'image' => $this->avatar_url,
33
            'posts' => ListPostResourceCollection::make($posts),
34
        ];
35
    }
36
}
37