Passed
Push — main ( c218ce...778008 )
by Tan
13:41 queued 09:57
created

MetaBoxCustomResource   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 27
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 20 3
1
<?php
2
3
namespace CSlant\Blog\Api\Http\Resources\MetaBox;
4
5
use CSlant\Blog\Core\Models\MetaBox;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Resources\Json\JsonResource;
8
9
/**
10
 * @mixin MetaBox
11
 */
12
class MetaBoxCustomResource extends JsonResource
13
{
14
    /**
15
     * @param  Request  $request
16
     *
17
     * @return array<string, mixed>
18
     */
19
    public function toArray(Request $request): array
20
    {
21
        /** @var MetaBox $this */
22
23
        $metaValueCustom = count($this->meta_value) == 1
24
            ? $this->meta_value[0]
25
            : $this->meta_value;
26
        $reference = $this->reference;
27
28
        if (is_array($metaValueCustom)) {
29
            $metaValueCustom['seo_title'] = $metaValueCustom['seo_title'] ?? substr($reference->name, 0, 65) ?? null;
30
            $metaValueCustom['seo_description'] = $metaValueCustom['seo_description']
31
                ?? substr($reference->description, 0, 300)
32
                ?? null;
33
        }
34
35
        return [
36
            'meta_key' => $this->meta_key,
37
            'meta_value' => $this->meta_value,
38
            'meta_value_custom' => $metaValueCustom,
39
            //'reference_id' => $this->reference_id,
40
            //'reference_type' => $this->reference_type,
41
        ];
42
    }
43
}
44