Passed
Push — relationships ( ba3a3d...460653 )
by Alex
02:33
created

JsonApiTransforms   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 90
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B transformRecord() 0 41 5
A transformCollection() 0 8 1
A transformCollectionIds() 0 11 1
1
<?php
2
3
namespace Huntie\JsonApi\Support;
4
5
/**
6
 * Transform Eloquent models and collections into JSON API objects.
7
 */
8
trait JsonApiTransforms
9
{
10
    /**
11
     * Transform a model instance into a JSON API object.
12
     *
13
     * @param \Illuminate\Database\Eloquent\Model $record
14
     * @param array|null                          $fields  Field subset to return
15
     * @param array|null                          $include Relations to include
16
     *
17
     * @return array
18
     */
19
    protected function transformRecord($record, array $fields = [], array $include = [])
20
    {
21
        $relations = array_unique(array_merge($record->getRelations(), $include));
22
        $attributes = $record->load($relations)->toArray();
23
        $relationships = [];
24
        $included = [];
25
26
        foreach ($relations as $relation) {
27
            $relationships[$relation] = [
28
                'data' => []
29
            ];
30
31
            foreach (array_pull($attributes, $relation) as $relatedRecord) {
32
                $relationships[$relation]['data'][] = [
33
                    'type' => $relation,
34
                    'id' => $relatedRecord['id'],
35
                ];
36
37
                if (in_array($relation, $include)) {
38
                    $included[] = [
39
                        'type' => $relation,
40
                        'id' => $relatedRecord['id'],
41
                        'attributes' => array_except($relatedRecord, ['id', 'pivot']),
42
                    ];
43
                }
44
            }
45
        }
46
47
        if (!empty($fields)) {
48
            $attributes = array_only($attributes, $fields);
49
        }
50
51
        $data = array_filter([
52
            'type' => $record->getTable(),
53
            'id' => $record->id,
54
            'attributes' => array_except($attributes, ['id']),
55
            'relationships' => $relationships,
56
        ]);
57
58
        return array_filter(compact('data', 'included'));
59
    }
60
61
    /**
62
     * Transform a set of models into a JSON API collection.
63
     *
64
     * @param \Illuminate\Support\Collection $records
65
     * @param array                          $fields
66
     *
67
     * @return array
68
     */
69
    protected function transformCollection($records, array $fields = [])
70
    {
71
        $data = $records->map(function ($record) use ($fields) {
72
            return $this->transformRecord($record, $fields)['data'];
73
        });
74
75
        return compact('data');
76
    }
77
78
    /**
79
     * Transform a set of models into a collection of JSON API resource
80
     * identifier objects.
81
     *
82
     * @param \Illuminate\Support\Collection $records
83
     *
84
     * @return array
85
     */
86
    protected function transformCollectionIds($records)
87
    {
88
        $data = $records->map(function ($record) {
89
            return [
90
                'type' => $record->getTable(),
91
                'id' => $record->id,
92
            ];
93
        });
94
95
        return compact('data');
96
    }
97
}
98