Passed
Push — master ( 25efb9...c70e91 )
by Caen
04:09 queued 12s
created

Serializable::arraySerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support\Concerns;
6
7
use function json_encode;
8
use function collect;
9
10
/**
11
 * Automatically serializes an Arrayable implementation when JSON is requested.
12
 *
13
 * @see \Hyde\Support\Contracts\SerializableContract
14
 * @see \Hyde\Framework\Testing\Unit\SerializableTest
15
 */
16
trait Serializable
17
{
18
    /** @inheritDoc */
19
    abstract public function toArray(): array;
20
21
    /** Recursively serialize Arrayables */
22
    public function arraySerialize(): array
23
    {
24
        return collect($this->toArray())->toArray();
0 ignored issues
show
Bug introduced by
$this->toArray() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
        return collect(/** @scrutinizer ignore-type */ $this->toArray())->toArray();
Loading history...
25
    }
26
27
    /** @inheritDoc */
28
    public function jsonSerialize(): array
29
    {
30
        return $this->arraySerialize();
31
    }
32
33
    /** @param  int  $options */
34
    public function toJson($options = 0): string
35
    {
36
        return json_encode($this->jsonSerialize(), $options);
37
    }
38
}
39