1 | <?php |
||
9 | class RelationshipSerializer extends JsonApiSerializer |
||
10 | { |
||
11 | /** |
||
12 | * The resolved relation to transform. |
||
13 | * |
||
14 | * @var Collection|Model|null |
||
15 | */ |
||
16 | protected $relation; |
||
17 | |||
18 | /** |
||
19 | * The subset of attributes to return on each resource type. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $fields; |
||
24 | |||
25 | /** |
||
26 | * The named relationships to list on the resolved resource(s). |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $relationships; |
||
31 | |||
32 | /** |
||
33 | * Create a new JSON API relationship serializer. |
||
34 | * |
||
35 | * @param Model $record The primary record |
||
36 | * @param string $relation The name of the relation to serialize |
||
37 | * @param array|null $fields The subset of fields to return on each resource type |
||
38 | * @param array|null $relationships The named relationships to list on the resolved resource(s) |
||
39 | * |
||
40 | * @throws InvalidRelationPathException |
||
41 | */ |
||
42 | public function __construct($record, $relation, array $fields = [], array $relationships = []) |
||
54 | |||
55 | /** |
||
56 | * Return a JSON API resource linkage representation, composed of a |
||
57 | * resource identifier for each related record. |
||
58 | * |
||
59 | * @return Collection|array|null |
||
60 | */ |
||
61 | public function toResourceLinkage() |
||
67 | |||
68 | /** |
||
69 | * Return a single, or collection of, JSON API resource objects for each |
||
70 | * record in the relationship. |
||
71 | * |
||
72 | * @return Collection|array|null |
||
73 | */ |
||
74 | public function toResourceCollection() |
||
81 | |||
82 | /** |
||
83 | * Return primary data for the JSON API document. |
||
84 | * |
||
85 | * @return mixed |
||
86 | */ |
||
87 | protected function getPrimaryData() |
||
91 | |||
92 | /** |
||
93 | * Run a map over each item in the relationship. |
||
94 | * |
||
95 | * @param callable $callback |
||
96 | * |
||
97 | * @return mixed |
||
98 | */ |
||
99 | protected function map(callable $callback) |
||
109 | } |
||
110 |