Passed
Push — master ( b1742c...4b0d05 )
by Arthur
101:17 queued 94:42
created

Transformer::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 29.10.18
6
 * Time: 09:35.
7
 */
8
9
namespace Foundation\Abstracts\Transformers;
10
11
use Foundation\Contracts\Transformable;
12
use Illuminate\Database\Eloquent\Collection;
13
use Illuminate\Database\Eloquent\Model;
14
use Illuminate\Http\Resources\Json\JsonResource;
15
16
abstract class Transformer extends JsonResource implements Transformable
17
{
18
    use IncludesRelations;
19
20
    public $include = [];
21
22
    public $availableIncludes = [];
23
24
    public function __construct($resource)
25
    {
26 10
        parent::__construct(self::loadRelations($resource));
27
    }
28 10
29
    public static function resource($model): self
30 10
    {
31
        return new static($model);
32
    }
33 10
34
    private static function loadRelations($resource)
35 10
    {
36 1
        if ($resource instanceof Model || $resource instanceof Collection) {
37
            $relations = call_class_function(static::class, 'compileRelations');
38
            $resource->load($relations);
39 10
        }
40
        return $resource;
41
    }
42 10
43
    public static function collection($resource)
44 10
    {
45
        return new AnonymousTransformerCollection(self::loadRelations($resource), static::class);
46
    }
47 10
48
    public function serialize()
49 10
    {
50 10
        return json_decode(json_encode($this->jsonSerialize()), true);
51 5
    }
52
}
53