|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Transformers; |
|
4
|
|
|
|
|
5
|
|
|
use Flugg\Responder\Contracts\Transformable; |
|
6
|
|
|
use Flugg\Responder\Contracts\Transformers\TransformerResolver as TransformerResolverContract; |
|
7
|
|
|
use Flugg\Responder\Exceptions\InvalidTransformerException; |
|
8
|
|
|
use Illuminate\Contracts\Container\Container; |
|
9
|
|
|
use Traversable; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This class is responsible for resolving transformers. |
|
13
|
|
|
* |
|
14
|
|
|
* @package flugger/laravel-responder |
|
15
|
|
|
* @author Alexander Tømmerås <[email protected]> |
|
16
|
|
|
* @license The MIT License |
|
17
|
|
|
*/ |
|
18
|
|
|
class TransformerResolver implements TransformerResolverContract |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Transformable to transformer mappings. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $bindings = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A container used to resolve transformers. |
|
29
|
|
|
* |
|
30
|
|
|
* @var \Illuminate\Contracts\Container\Container |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $container; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* A fallback transformer to return when no transformer can be resolved. |
|
36
|
|
|
* |
|
37
|
|
|
* @var \Flugg\Responder\Transformers\Transformer|string|callable |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $fallback; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Construct the resolver class. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Illuminate\Contracts\Container\Container $container |
|
45
|
|
|
* @param \Flugg\Responder\Transformers\Transformer|string|callable $fallback |
|
46
|
|
|
*/ |
|
47
|
57 |
|
public function __construct(Container $container, $fallback) |
|
48
|
|
|
{ |
|
49
|
57 |
|
$this->container = $container; |
|
50
|
57 |
|
$this->fallback = $fallback; |
|
51
|
57 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Register a transformable to transformer binding. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string|array $transformable |
|
57
|
|
|
* @param string|callback|null $transformer |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function bind($transformable, $transformer = null) |
|
61
|
|
|
{ |
|
62
|
2 |
|
$this->bindings = array_merge($this->bindings, is_array($transformable) ? $transformable : [ |
|
63
|
2 |
|
$transformable => $transformer, |
|
64
|
|
|
]); |
|
65
|
2 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Resolve a transformer. |
|
69
|
|
|
* |
|
70
|
|
|
* @param \Flugg\Responder\Transformers\Transformer|string|callable $transformer |
|
71
|
|
|
* @return \Flugg\Responder\Transformers\Transformer|callable |
|
72
|
|
|
* @throws \Flugg\Responder\Exceptions\InvalidTransformerException |
|
73
|
|
|
*/ |
|
74
|
46 |
|
public function resolve($transformer) |
|
75
|
|
|
{ |
|
76
|
46 |
|
if (is_string($transformer)) { |
|
77
|
36 |
|
return $this->container->make($transformer); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
33 |
|
if (! is_callable($transformer) && ! $transformer instanceof Transformer) { |
|
81
|
1 |
|
throw new InvalidTransformerException; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
32 |
|
return $transformer; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Resolve a transformer from the given data. |
|
89
|
|
|
* |
|
90
|
|
|
* @param mixed $data |
|
91
|
|
|
* @return \Flugg\Responder\Transformers\Transformer|callable |
|
92
|
|
|
*/ |
|
93
|
30 |
|
public function resolveFromData($data) |
|
94
|
|
|
{ |
|
95
|
30 |
|
$transformer = $this->resolveTransformer($this->resolveTransformableItem($data)); |
|
96
|
|
|
|
|
97
|
30 |
|
return $this->resolve($transformer); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Resolve a transformer from the transformable element. |
|
102
|
|
|
* |
|
103
|
|
|
* @param mixed $transformable |
|
104
|
|
|
* @return \Flugg\Responder\Contracts\Transformable|callable |
|
105
|
|
|
*/ |
|
106
|
30 |
View Code Duplication |
protected function resolveTransformer($transformable) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
30 |
|
if (is_object($transformable) && key_exists(get_class($transformable), $this->bindings)) { |
|
109
|
2 |
|
return $this->bindings[get_class($transformable)]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
28 |
|
if ($transformable instanceof Transformable) { |
|
113
|
5 |
|
return $transformable->transformer(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
23 |
|
return $this->resolve($this->fallback); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Resolve a transformable item from the given data. |
|
121
|
|
|
* |
|
122
|
|
|
* @param mixed $data |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
30 |
|
protected function resolveTransformableItem($data) |
|
126
|
|
|
{ |
|
127
|
30 |
|
if (is_array($data) || $data instanceof Traversable) { |
|
128
|
15 |
|
foreach ($data as $item) { |
|
129
|
15 |
|
return $item; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
15 |
|
return $data; |
|
134
|
|
|
} |
|
135
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.