1 | <?php |
||
19 | class TransformerResolver implements TransformerResolverContract |
||
20 | { |
||
21 | /** |
||
22 | * A container used to resolve transformers. |
||
23 | * |
||
24 | * @var \Illuminate\Contracts\Container\Container |
||
25 | */ |
||
26 | protected $container; |
||
27 | |||
28 | /** |
||
29 | * Transformable to transformer mappings. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $bindings = []; |
||
34 | |||
35 | /** |
||
36 | * Construct the resolver class. |
||
37 | * |
||
38 | * @param \Illuminate\Contracts\Container\Container $container |
||
39 | */ |
||
40 | 8 | public function __construct(Container $container) |
|
44 | |||
45 | /** |
||
46 | * Register a transformable to transformer binding. |
||
47 | * |
||
48 | * @param string|array $transformable |
||
49 | * @param string|callback|null $transformer |
||
50 | * @return void |
||
51 | */ |
||
52 | 1 | public function bind($transformable, $transformer = null) |
|
58 | |||
59 | /** |
||
60 | * Resolve a transformer. |
||
61 | * |
||
62 | * @param \Flugg\Responder\Transformers\Transformer|string|callable $transformer |
||
63 | * @return \Flugg\Responder\Transformers\Transformer|callable |
||
64 | * @throws \Flugg\Responder\Exceptions\InvalidTransformerException |
||
65 | */ |
||
66 | 8 | public function resolve($transformer) |
|
78 | |||
79 | /** |
||
80 | * Resolve a transformer from the given data. |
||
81 | * |
||
82 | * @param mixed $data |
||
83 | * @return \Flugg\Responder\Transformers\Transformer|callable |
||
84 | */ |
||
85 | 4 | public function resolveFromData($data) |
|
92 | |||
93 | /** |
||
94 | * Resolve a transformable from the given data. |
||
95 | * |
||
96 | * @param mixed $data |
||
97 | * @return mixed |
||
98 | */ |
||
99 | 4 | protected function resolveTransformable($data) |
|
100 | { |
||
101 | 4 | if (is_array($data) || $data instanceof Traversable) { |
|
102 | 1 | foreach ($data as $item) { |
|
103 | 1 | return $item; |
|
104 | } |
||
105 | } |
||
106 | |||
107 | 3 | return $data; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Resolve a transformer from the transformable element. |
||
112 | * |
||
113 | * @param mixed $transformable |
||
114 | * @return \Flugg\Responder\Contracts\Transformable|callable |
||
115 | */ |
||
116 | 4 | protected function resolveTransformer($transformable) |
|
128 | |||
129 | /** |
||
130 | * Resolve a fallback closure transformer just returning the data directly. |
||
131 | * |
||
132 | * @return callable |
||
133 | */ |
||
134 | protected function resolveFallbackTransformer() |
||
140 | } |
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.