Repository navigation
Expand file tree
/
Copy pathFactory.php
More file actions
248 lines (219 loc) · 5.94 KB
/
Copy pathFactory.php
File metadata and controls
248 lines (219 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace Dingo\Api\Transformer;
use Closure;
use RuntimeException;
use Dingo\Api\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Container\Container;
use Dingo\Api\Contract\Transformer\Adapter;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Http\Request as IlluminateRequest;
class Factory
{
/**
* Illuminate container instance.
*
* @var \Illuminate\Container\Container
*/
protected $container;
/**
* Array of registered transformer bindings.
*
* @var array
*/
protected $bindings = [];
/**
* Transformation layer adapter being used to transform responses.
*
* @var \Dingo\Api\Contract\Transformer\Adapter
*/
protected $adapter;
/**
* Create a new transformer factory instance.
*
* @param \Illuminate\Container\Container $container
* @param \Dingo\Api\Contract\Transformer\Adapter $adapter
*
* @return void
*/
public function __construct(Container $container, Adapter $adapter)
{
$this->container = $container;
$this->adapter = $adapter;
}
/**
* Register a transformer binding resolver for a class.
*
* @param $class
* @param $resolver
* @param array $parameters
* @param \Closure|null $after
*
* @return \Dingo\Api\Transformer\Binding
*/
public function register($class, $resolver, array $parameters = [], Closure $after = null)
{
return $this->bindings[$class] = $this->createBinding($resolver, $parameters, $after);
}
/**
* Transform a response.
*
* @param string|object $response
*
* @return mixed
*/
public function transform($response)
{
$binding = $this->getBinding($response);
return $this->adapter->transform($response, $binding->resolveTransformer(), $binding, $this->getRequest());
}
/**
* Determine if a response is transformable.
*
* @param mixed $response
*
* @return bool
*/
public function transformableResponse($response)
{
return $this->transformableType($response) && $this->hasBinding($response);
}
/**
* Determine if a value is of a transformable type.
*
* @param mixed $value
*
* @return bool
*/
public function transformableType($value)
{
return is_object($value) || is_string($value);
}
/**
* Get a registered transformer binding.
*
* @param string|object $class
*
* @throws \RuntimeException
*
* @return \Dingo\Api\Transformer\Binding
*/
protected function getBinding($class)
{
if ($this->isCollection($class) && ! $class->isEmpty()) {
return $this->getBindingFromCollection($class);
}
$class = is_object($class) ? get_class($class) : $class;
if (! $this->hasBinding($class)) {
throw new RuntimeException('Unable to find bound transformer for "'.$class.'" class.');
}
return $this->bindings[$class];
}
/**
* Create a new binding instance.
*
* @param string|callable|object $resolver
* @param array $parameters
* @param \Closure $callback
*
* @return \Dingo\Api\Transformer\Binding
*/
protected function createBinding($resolver, array $parameters = [], Closure $callback = null)
{
return new Binding($this->container, $resolver, $parameters, $callback);
}
/**
* Get a registered transformer binding from a collection of items.
*
* @param \Illuminate\Support\Collection $collection
*
* @return null|string|callable
*/
protected function getBindingFromCollection($collection)
{
return $this->getBinding($collection->first());
}
/**
* Determine if a class has a transformer binding.
*
* @param string|object $class
*
* @return bool
*/
protected function hasBinding($class)
{
if ($this->isCollection($class) && ! $class->isEmpty()) {
$class = $class->first();
}
$class = is_object($class) ? get_class($class) : $class;
return isset($this->bindings[$class]);
}
/**
* Determine if the instance is a collection.
*
* @param object $instance
*
* @return bool
*/
protected function isCollection($instance)
{
return $instance instanceof Collection || $instance instanceof Paginator;
}
/**
* Get the array of registered transformer bindings.
*
* @return array
*/
public function getTransformerBindings()
{
return $this->bindings;
}
/**
* Set the transformation layer at runtime.
*
* @param \Closure|\Dingo\Api\Contract\Transformer\Adapter $adapter
*
* @return void
*/
public function setAdapter($adapter)
{
if (is_callable($adapter)) {
$adapter = call_user_func($adapter, $this->container);
}
$this->adapter = $adapter;
}
/**
* Get the transformation layer adapter.
*
* @return \Dingo\Api\Contract\Transformer\Adapter
*/
public function getAdapter()
{
return $this->adapter;
}
/**
* Get the request from the container.
*
* @return \Dingo\Api\Http\Request
*/
public function getRequest()
{
$request = $this->container['request'];
if ($request instanceof IlluminateRequest && ! $request instanceof Request) {
$request = (new Request())->createFromIlluminate($request);
}
return $request;
}
/**
* Pass unknown method calls through to the adapter.
*
* @param string $method
* @param array $parameters
*
* @return mixed
*/
public function __call($method, $parameters)
{
return call_user_func_array([$this->adapter, $method], $parameters);
}
}