|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Huntie\JsonApi\Serializers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
|
|
8
|
|
|
class RelationshipSerializer extends JsonApiSerializer |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The loaded relation to transform. |
|
12
|
|
|
* |
|
13
|
|
|
* @var Collection|Model|null |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $relation; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The subset of attributes to return on each included record type. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $fields; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create a new JSON API relationship serializer. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Model $record The primary record |
|
28
|
|
|
* @param string $relation The named relation to serialize |
|
29
|
|
|
* @param array|null $fields Subset of fields to return by record type |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($record, $relation, array $fields = []) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
|
|
35
|
|
|
$this->relation = $record->{$relation}; |
|
36
|
|
|
$this->fields = array_unique($fields); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Return a JSON API resource linkage representation, composed of a |
|
41
|
|
|
* resource identifier for each related record. |
|
42
|
|
|
* |
|
43
|
|
|
* @return Collection|array|null |
|
44
|
|
|
*/ |
|
45
|
|
|
public function toResourceLinkage() |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->map(function($record) { |
|
48
|
|
|
return (new ResourceSerializer($record))->toResourceIdentifier(); |
|
49
|
|
|
}); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Return a single, or collection of, JSON API resource objects for each |
|
54
|
|
|
* record in the relationship. |
|
55
|
|
|
* |
|
56
|
|
|
* @return Collection|array|null |
|
57
|
|
|
*/ |
|
58
|
|
|
public function toResourceCollection() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->map(function($record) { |
|
61
|
|
|
return (new ResourceSerializer($record, $this->fields))->toBaseResourceObject(); |
|
62
|
|
|
}); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Return primary data for the JSON API document. |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function getPrimaryData() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->toResourceLinkage(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Run a map over each item in the relationship. |
|
77
|
|
|
* |
|
78
|
|
|
* @param \Closure $callback |
|
79
|
|
|
* |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function map($callback) |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->relation instanceof Collection) { |
|
85
|
|
|
return $this->relation->map($callback); |
|
86
|
|
|
} else if ($this->relation instanceof Model) { |
|
87
|
|
|
return $callback($this->relation); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|