1 | <?php |
||
18 | class FractalTransformFactory implements TransformFactory |
||
19 | { |
||
20 | /** |
||
21 | * A manager for executing transforms. |
||
22 | * |
||
23 | * @var \League\Fractal\Manager |
||
24 | */ |
||
25 | protected $manager; |
||
26 | |||
27 | /** |
||
28 | * Construct the factory class. |
||
29 | * |
||
30 | * @param \League\Fractal\Manager $manager |
||
31 | */ |
||
32 | 59 | public function __construct(Manager $manager) |
|
36 | |||
37 | /** |
||
38 | * Transform the given resource, and serialize the data with the given serializer. |
||
39 | * |
||
40 | * @param \League\Fractal\Resource\ResourceInterface $resource |
||
41 | * @param \League\Fractal\Serializer\SerializerAbstract $serializer |
||
42 | * @param array $options |
||
43 | * @return array|null |
||
44 | */ |
||
45 | 59 | public function make(ResourceInterface $resource, SerializerAbstract $serializer, array $options = []) |
|
56 | |||
57 | /** |
||
58 | * Parse the transformation options. |
||
59 | * |
||
60 | * @param array $options |
||
61 | * @param \League\Fractal\Resource\ResourceInterface $resource |
||
62 | * @return array |
||
63 | */ |
||
64 | 59 | protected function parseOptions(array $options, ResourceInterface $resource): array |
|
65 | { |
||
66 | 59 | $options = array_merge([ |
|
67 | 59 | 'includes' => [], |
|
68 | 'excludes' => [], |
||
69 | 'fieldsets' => [], |
||
70 | 59 | ], $options); |
|
71 | |||
72 | 59 | if (! empty($options['fieldsets'])) { |
|
73 | 7 | if (is_null($resourceKey = $resource->getResourceKey())) { |
|
74 | 1 | throw new LogicException('Filtering fields using sparse fieldsets require resource key to be set.'); |
|
75 | } |
||
76 | |||
77 | 6 | $options['fieldsets'] = $this->parseFieldsets($options['fieldsets'], $resourceKey, $options['includes']); |
|
78 | } |
||
79 | |||
80 | 58 | return $options; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Parse the fieldsets for Fractal. |
||
85 | * |
||
86 | * @param array $fieldsets |
||
87 | * @param string $resourceKey |
||
88 | * @param array $includes |
||
89 | * @return array |
||
90 | */ |
||
91 | 6 | protected function parseFieldsets(array $fieldsets, string $resourceKey, array $includes): array |
|
92 | { |
||
93 | $includes = array_map(function ($include) use ($resourceKey) { |
||
94 | 4 | return "$resourceKey.$include"; |
|
95 | 6 | }, $includes); |
|
96 | |||
97 | 6 | foreach ($fieldsets as $key => $fields) { |
|
98 | 6 | if (is_numeric($key)) { |
|
99 | 2 | unset($fieldsets[$key]); |
|
100 | 2 | $key = $resourceKey; |
|
101 | } |
||
102 | |||
103 | 6 | $fields = $this->parseFieldset($key, (array) $fields, $includes); |
|
104 | 6 | $fieldsets[$key] = array_unique(array_merge(key_exists($key, $fieldsets) ? (array) $fieldsets[$key] : [], $fields)); |
|
105 | } |
||
106 | |||
107 | return array_map(function ($fields) { |
||
108 | 6 | return implode(',', $fields); |
|
109 | 6 | }, $fieldsets); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * Parse the given fieldset and append any related resource keys. |
||
114 | * |
||
115 | * @param string $key |
||
116 | * @param array $fields |
||
117 | * @param array $includes |
||
118 | * @return array |
||
119 | */ |
||
120 | 6 | protected function parseFieldset(string $key, array $fields, array $includes): array |
|
128 | |||
129 | /** |
||
130 | * Resolve included segments that are a direct child to the given resource key. |
||
131 | * |
||
132 | * @param string $key |
||
133 | * @param string $include |
||
134 | * @return array |
||
135 | */ |
||
136 | 4 | protected function resolveChildIncludes($key, string $include): array |
|
146 | } |