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 | public function __construct(Container $container) |
||
41 | { |
||
42 | $this->container = $container; |
||
43 | } |
||
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 | public function bind($transformable, $transformer = null) |
||
53 | { |
||
54 | $this->bindings = array_merge($this->bindings, is_array($transformable) ? $transformable : [ |
||
55 | $transformable => $transformer, |
||
56 | ]); |
||
57 | } |
||
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 | public function resolve($transformer) |
||
67 | { |
||
68 | if (is_string($transformer)) { |
||
69 | return $this->container->make($transformer); |
||
70 | } |
||
71 | |||
72 | if (! is_callable($transformer) && ! $transformer instanceof Transformer) { |
||
73 | throw new InvalidTransformerException; |
||
74 | } |
||
75 | |||
76 | return $transformer; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Resolve a transformer from the given data. |
||
81 | * |
||
82 | * @param mixed $data |
||
83 | * @return \Flugg\Responder\Transformers\Transformer|callable|null |
||
84 | */ |
||
85 | public function resolveFromData($data) |
||
86 | { |
||
87 | $transformable = $this->resolveTransformable($data); |
||
88 | $transformer = $this->resolveTransformer($transformable); |
||
89 | |||
90 | return $this->resolve($transformer); |
||
|
|||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Resolve a transformable from the given data. |
||
95 | * |
||
96 | * @param mixed $data |
||
97 | * @return \Flugg\Responder\Contracts\Transformable|null |
||
98 | */ |
||
99 | protected function resolveTransformable($data) |
||
109 | |||
110 | /** |
||
111 | * Resolve a transformer from the transformable element. |
||
112 | * |
||
113 | * @param mixed $transformable |
||
114 | * @return \Flugg\Responder\Contracts\Transformable|callable|null |
||
115 | */ |
||
116 | 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.