Completed
Push — main ( eedfca...d08b40 )
by Tan
21s queued 17s
created

MetaBoxCustomResource::toArray()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 7
eloc 14
c 3
b 1
f 1
nc 36
nop 1
dl 0
loc 21
rs 8.8333
1
<?php
2
3
namespace CSlant\Blog\Api\Http\Resources\MetaBox;
4
5
use CSlant\Blog\Core\Facades\Base\Media\RvMedia;
6
use CSlant\Blog\Core\Models\MetaBox;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Resources\Json\JsonResource;
9
10
/**
11
 * @mixin MetaBox
12
 */
13
class MetaBoxCustomResource extends JsonResource
14
{
15
    /**
16
     * @param  Request  $request
17
     *
18
     * @return array<string, mixed>
19
     */
20
    public function toArray(Request $request): array
21
    {
22
        /** @var MetaBox $this */
23
24
        $metaValueCustom = is_array($this->meta_value) && count($this->meta_value) == 1
25
            ? $this->meta_value[0]
26
            : $this->meta_value;
27
        $reference = $this->reference;
28
29
        if (is_array($metaValueCustom)) {
30
            $metaValueCustom['seo_title'] = $metaValueCustom['seo_title']
31
                ?? (property_exists($reference, 'name') ? substr($reference->name, 0, 65) : null);
32
            $metaValueCustom['seo_description'] = $metaValueCustom['seo_description']
33
                ?? (property_exists($reference, 'description') ? substr($reference->description, 0, 300) : null);
34
            $metaValueCustom['seo_image'] = !empty($metaValueCustom['seo_image']) ? (RvMedia::getImageUrl($metaValueCustom['seo_image'], '', false, RvMedia::getDefaultImage()) ?? '') : null;
35
        }
36
37
        return [
38
            'meta_key' => $this->meta_key,
39
            'meta_value' => $this->meta_value,
40
            'meta_value_custom' => $metaValueCustom,
41
            //'reference_id' => $this->reference_id,
42
            //'reference_type' => $this->reference_type,
43
        ];
44
    }
45
}
46