Jsonable::jsonSerialize()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 5
nop 0
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Glorand\Drip\Models\Traits;
4
5
trait Jsonable
6
{
7 6
    public function jsonSerialize(): array
8
    {
9 6
        $response = [];
10 6
        $attributes = get_object_vars($this);
11 6
        foreach ($attributes as $attribute => $value) {
12 6
            if (is_array($value) && count($value)) {
13 3
                $response[$attribute] = $value;
14 6
            } elseif ($value instanceof \DateTime) {
15 2
                $response[$attribute] = $value->format(DATE_ISO8601);
16 6
            } elseif (!empty($value)) {
17 6
                $response[$attribute] = $value;
18
            }
19
        }
20
21 6
        return $response;
22
    }
23
24
    abstract public function toDrip(): array;
25
}
26