Passed
Pull Request — main (#62)
by Tan
03:17
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\ListPostResource;
6
use CSlant\Blog\Api\Http\Resources\Post\ListPostResourceCollection;
7
use CSlant\Blog\Core\Models\User;
8
use Illuminate\Http\Resources\Json\JsonResource;
9
10
/**
11
 * @mixin User
12
 */
13
class AuthorWithPostResource extends JsonResource
14
{
15
    /**
16
     * @param $request
17
     *
18
     * @return array<string, mixed>
19
     */
20
    public function toArray($request): array
21
    {
22
        /** @var User $this */
23
        $posts = $this->posts()
24
            ->orderBy($request->get('order_by', 'created_at'), $request->get('order', 'DESC'))
25
            ->paginate($request->get('per_page', 10));
26
27
        return [
28
            'id' => $this->id,
29
            'email' => $this->email,
30
            'first_name' => $this->first_name,
31
            'last_name' => $this->last_name,
32
            'username' => $this->username,
33
            'image' => $this->avatar_url,
34
            'posts' => ListPostResourceCollection::make($posts),
35
        ];
36
    }
37
}
38